# Testing Kelly Criterion and Optimal f in R

Kelly Criterion and Optimal f are very similar models for geometric growth optimization. Ralph Vince’s article Optimal f and the Kelly Criterion  has explained their differences in detail and here are main takeaways.

1. Kelly Criterion does not yield the optimal fraction to risk in trading, Optimal f does
2. Kelly Criterion only generates a leverage factor which could go infinitely large; Optimal f is bounded between 0 and 1
3. The reconciliation between two models could be written as Optimal f = Kelly * (-W/Px), where W is the possible maximum loss on each trade, Px is the price per share. Both are in dollar amount

Inspired by Ralph’s article, I did a test in R to compare these two models. Considering a 50/50 coin flipping game that pays $2 on heads and -$0.5 on tails for every $1 you bet. Through optimizing Kelly’s objective function $\sum_{i=1}^{n}(\ln(1+R_i*f)*P_i)$ we should get optimal f = 0.75. While Optimal f, with objective function $\prod_{i=1}^{n}(1+f*(Px_i/-W))^{P_i}$, will give a different optimal f = 0.375. Let’s see if they can be consistent with observations in R. # kelly formula test kelly.opt <- function(win, loss, p, obs, lev) { # win = payout for a win # loss = payout for a loss # p = probability of winning # obs = number of observations # lev = maximum leverage allowed # set up different bet sizes for test f <- seq(0, lev, 1 / (obs - 1)) # generate trading results according to given win, loss and p ret <- rep(win, length(f)) ret[which(rbinom(length(f), 1, 1 - p) == 1)] <- loss #calculate accumulative pnl for different bet sizes respectively pnl <- f for (i in 1:length(f)) { pnl[i] <- sum(log(1 + ret * f[i])) } # find the optimal point of f results <- cbind(f, pnl) opt.kelly <- results[which(results[, 2] == max(results[, 2])), 1] # wrap up output <- list() output$opt.kelly <- opt.kelly
output$results <- results output } # optimal f test opt.f <- function(win, loss, p, obs, lev) { # similar as Kelly except using a different objective function f <- seq(0, lev, 1 / (obs - 1)) ret <- rep(win, length(f)) ret[which(rbinom(length(f), 1, 1 - p) == 1)] <- loss pnl <- f for (i in 1:length(f)) { pnl[i] <- prod(1 + ret / (-loss / f[i])) } results <- cbind(f, pnl) opt.f <- results[(which(results[, 2] == max(results[, 2]))), 1] output <- list() output$opt.f <- opt.f
output$results <- results output } # get statistics for kelly compare <- data.frame(kelly=1:5000, opt.f=1:5000) for (i in 1:5000) { compare$kelly[i] <- kelly.opt(2, -.5, .5, 500, 1)$opt.kelly } # get statistics for optimal f for (i in 1:5000) { compare$opt.f[i] <- opt.f(2, -.5, .5, 500, 1)\$opt.f
}

# generate graph
require(ggplot)
m <- ggplot(compare, aes(colour=compare)) + xlim(c(0, 1)) + xlab('distribution')
m + stat_density(aes(x=kelly, colour='kelly'), adjust = 2, fill=NA) +
stat_density(aes(x=opt.f, colour='opt.f'), adjust = 2, fill=NA)


It’s always a beauty to see test results and mathematical models showing consistency. And here we are happy to see two distributions nice and tightly surrounding their means: 0.75068 for Kelly Criterion, and 0.37558 for Optimal f. To wrap things up.

roy

## 7 thoughts on “Testing Kelly Criterion and Optimal f in R”

1. Andrew says:

I’m perplexed by the Ralph Vince article. For cases where the maximum capital loss is less than 100% optimal f suggests a lower proportion to invest than Kelly (which may suggest leverage).

However RV’s paper did not “prove”, as far as I could see, that optimal f was better than the Kelly Criterion. One practical way of doing that would be to do some simulations.

You’ve done this and show that the “KC” pdf lies entirely to the right of the “RV” pdf. To this naive reader this seems to suggest that KC is superior, despite the “fraction of over 1” i.e. leverage. This also comes through in my own suimulations. What am I missing?

• hi Andrew. The purpose of this test is just to see if statistical simulation based on different objective models (RV and KC) agrees with results derived from calculus.

imo, the most important difference between the two is KC implies the game will last forever while RV takes more short-term compounding effect into consideration. I believe you can think RV as a conservative variation of KC. None of them is necessarily better than the other. One can easily design a game and purposefully make one model performs better than the other.

• Andrew says:

I see (and strongly agree with!) your point on the simulations versus calculus. RV is definitely a more conservative approach than KC (as would be lots of utility fcuntions other than the Kelly log). RV / optimal f recommends a genuine fraction 0 < f < 1 – but that isn't really how I read the paper; RV seems almost to be claiming that KC is "wrong". And the "proportions" are often massively different; 47.5% (RV) versus 475% (KC) in one example RV gives. I'm not even really sure of the motivation behind RV's objective function.

You say "KC implies the game will last forever while RV takes more short-term compounding effect into consideration." I think the optimality of KC does indeed only apply "in the limit". Other than a general conservatism (realtive to KC) which could ghave been achieved through e.g. a "half Kelly" approach, the way in which RV takes the short term into account seems opaque to me. There is no mention of an explicit tiem horizon of

My key point is still that you have shown that the RV pdf is massively to the left of the KC pdf, seemingly giveing a "stochastic dominance" decision criterion firmly in favour of KC. This is not at all the flavour of the RV paper – quite the reverse; as I read it, RV seems to be claiming optimality with respect to actual long term performance where leverage is involved, but I have failed to see his point.

Thanks again.

Andrew

• Yeah…it’s one of those things I said before and look back it’s just not correct. You caught me there.

To your key question, the distributions are not PnL, they are optimal bet size found though simulation. Purely empirical.

2. Antonio says: