12,348,894 members (67,406 online)
Technical Blog
alternative version

5.3K views
4 bookmarked
Posted

# The Kelly Criterion in C#

, 14 Aug 2014 CPOL
 Rate this:
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 maximise your winnings in the long run. http://en.wikipedia.org/wiki/Kelly_criterion   So, here&#8217;s what looks like a trivial piece o

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 maximise your winnings in the long run.

http://en.wikipedia.org/wiki/Kelly_criterion

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

```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&rsquo;ve alredy 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&rsquo;ve alredy 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 &ndash; 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 &ndash; 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 &ndash; 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&rsquo;ve staked nothing else so far, then 5:1
decimal b = potentialWinnings / (ourStake);
decimal f = ((b * ourWinPercentage) &ndash; (1 &ndash; ourWinPercentage)) / b;

return f * proportion;
}

}```

## Share

 Technical Lead Alpha Integralis Limited 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

## You may also be interested in...

 First Prev Next
 Small question Member 1058612514-Aug-14 22:40 Member 10586125 14-Aug-14 22:40
 Re: Small question CatchExAs15-Aug-14 1:53 CatchExAs 15-Aug-14 1:53
 Re: Small question Member 1058612515-Aug-14 2:31 Member 10586125 15-Aug-14 2:31
 Re: Small question CatchExAs15-Aug-14 3:05 CatchExAs 15-Aug-14 3:05
 Re: Small question Member 1058612515-Aug-14 3:18 Member 10586125 15-Aug-14 3:18
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jun-16 15:40 Refresh 1