Click here to Skip to main content
15,886,362 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 
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.