Click here to Skip to main content
15,887,175 members
Articles / Programming Languages / C#

The Kelly Criterion in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Aug 2014CPOL 10K   4   5
The Kelly Criterion in C#

Given a bank roll, a stake you have already bet and a potential pot of winnings, the Kelly Criterion should calculate for you the optimal amount you should bet to maximize your winnings in the long run.

So, here’s what looks like a trivial piece of code, but is actually quite powerful:

C#
public class KellyEvaluator
{
private decimal proportion;
public KellyEvaluator(decimal proportion = 1.0M)
{
this.proportion = proportion;
}

public decimal MaxBankRoll(decimal bankRoll, decimal pot, decimal stake, decimal ourWinPercentage)
{
return bankRoll * MaxFraction(bankRoll, pot, stake, ourWinPercentage);
}

//given our current bankroll, a pot, an amount we’ve already staked, 
//our win percentage and the mean for the # of players,
//what is the maximum value we should allow ourselves to bet?
public decimal MaxNormalisedBankRoll(decimal bankRoll, decimal pot, decimal stake,
decimal ourWinPercentage, decimal meanWinPercentage)
{
return bankRoll * 
MaxNormalisedFraction(bankRoll, pot, stake, ourWinPercentage, meanWinPercentage);
}

//given a win% and an amount we’ve already staked, whatis the max we can bet
public decimal MaxNormalisedFraction(decimal bankRoll, decimal pot, decimal totalStaked,
decimal ourWinPercentage, decimal meanWinPercentage)
{
decimal f = 0.0M;
decimal proposedBet = bankRoll;
decimal max = 0.0M;
while (proposedBet > 0.0M)
{
f = NormalisedFraction
(bankRoll, pot – totalStaked, proposedBet, ourWinPercentage, meanWinPercentage);
if (f >= 0.0M)
max = f;
proposedBet -= 1.0M;
}

return max;
}

//given a win% whatis the max we can bet
public decimal MaxFraction
(decimal bankRoll, decimal pot, decimal totalStaked, decimal ourWinPercentage)
{
decimal f = 0.0M;
decimal proposedBet = bankRoll;
decimal max = 0.0M;
while(proposedBet > 0.0M)
{
f = Fraction(bankRoll, pot – totalStaked, proposedBet, ourWinPercentage);
if (f >= 0.0M)
max = f;
proposedBet -= 1.0M;
}

return max;
}

private decimal NormalisedFraction(decimal bankRoll, decimal potentialWinnings, decimal ourStake,
decimal ourWinPercentage, decimal meanWinPercentage)
{
decimal delta = ourWinPercentage – meanWinPercentage;
decimal edgePercentage = delta / meanWinPercentage;

return Fraction(bankRoll, potentialWinnings, ourStake, edgePercentage);
}

private decimal Fraction(decimal bankRoll, decimal potentialWinnings, 
                         decimal ourStake, decimal ourWinPercentage)
{

//b:1 i.e., if pot is 100 and call value is 20 and we’ve staked nothing else so far, then 5:1
decimal b = potentialWinnings / (ourStake);
decimal f = ((b * ourWinPercentage) – (1 – ourWinPercentage)) / b;

return f * proportion;
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Alpha Integralis Limited
United Kingdom United Kingdom
CatchExAs aka Nick Wilton runs Alpha Integralis, a software development consultancy to companies in the City of London.

Main interests include exploring Algo-Trading, Quant Development and Physics using C# and Java.

www.nickwilton.info/Blog

Comments and Discussions

 
QuestionSmall question Pin
Alexey KK14-Aug-14 22:40
professionalAlexey KK14-Aug-14 22:40 
AnswerRe: Small question Pin
CatchExAs15-Aug-14 1:53
professionalCatchExAs15-Aug-14 1:53 
Privet Alexey, good to hear from you again!

Do I understand correctly that you are asking why not use Sharpe Ratio over the Kelly Ratio? Mainly because in Poker, your decisions can only be made in a discrete manner where your win probability can be directly estimated (you know all possible combinations of card hands and what proportion you will win). When managing your portfolio however, you usually do so in a continuous manner (i.e. can cash in your stock at any time) with no direct measure of 'winning' just indirect measures of what your return will be over time.

I think this post explains it better:
http://epchan.blogspot.co.uk/2006/10/how-much-leverage-should-you-use.html[^]

Anyway, the reason for this and the Poisson article was as background for the next instalment of the Pokerbot article I wrote. The idea being that I would match up several forms of strategy (with variable inputs) and then pick a winner.

-Nick

Ps. I am in Moscow and St.Petersburg at the end of the month, is that anywhere near where you are?
GeneralRe: Small question Pin
Alexey KK15-Aug-14 2:31
professionalAlexey KK15-Aug-14 2:31 
GeneralRe: Small question Pin
CatchExAs15-Aug-14 3:05
professionalCatchExAs15-Aug-14 3:05 
GeneralRe: Small question Pin
Alexey KK15-Aug-14 3:18
professionalAlexey KK15-Aug-14 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.