Kelly Criterion (Part 1)

Kelly Criterion (House of the Rising Sum)
https://en.wikipedia.org/wiki/Kelly_criterion

There is a criterion in Mathematics
They call the Rising Sum
And it's been the ruin of many a poor investors
Dear God, I know I was one

My mother was a Mathematician
She bought me new shiny HP48SX
And my father was a gamblin' man
Way down in New Orleans

And the only thing a investor needs
Is a suitcase or a trunk of cash
And the only time he's satisfied
Is when he's a drunk

Oh, mother, tell your children
Not to do what I have done
To spend your lives in sums and misery
In the house of the rising sum

The best way to understand Kelly Criterion is to imagine that
you have a choice to buy a lottery ticket
1. The cost of the lottery ticket is "cost of ticket" which is usually $1
2. The chance that the ticket will win is p
3. If the ticket wins, you get back "cost of ticket" plus "profit"
4. Thus the gain of the ticket is "profit"/"cost of ticket"

Kelly says

To get the Maximum Rate of Return
1. you need a bankroll (or a trunk of cash)
2. you should invest a fraction f of your bankroll each time
specifically

gain = profit_per_ticket/cost_of_ticket
f = p - (1-p)/gain

or as wikipedia puts it

If you ask the investors what they want, they would say they
absolutely want to avoid a loss

So Here is the source to find the chances/probability of making a loss

But Beware because this is where almost ALL Real World Investors trip up:

The real problem with Kelly Criterion in the Real World is that it expects that you know the TRUE probability of winning. But you do not. You will NEVER know the TRUE probability. You can only estimate the probability of winning based on the data you have. So your estimate of the TRUE probability of winning has a degree of uncertainty. See the wikipedia article of estimating the probability of a coin toss.

Another problem is that the TRUE probability of winning could be changing over time in the real world as the real world environment changes. Ohanian (talk) 06:02, 18 May 2024 (UTC)

using StableRNGs,Printf

rng = StableRNG(1234)

mypath = "/Users/ssiew/juliascript/KellyConstrain"

function calculate_loss(f,p,g;numtimes=10)
    times = numtimes
    losses = 0
    while times > 0
        times -= 1
        # Actual loop
        games = 100
        bankroll = 1.0
        while games > 0
            games -= 1
            # actual loop
            stake = f * bankroll
            if rand(rng) > p
                bankroll -= stake
            else
                bankroll += g * stake
            end
        end
        if bankroll < 1.0
            losses += 1
        end
    end
    return losses/numtimes
end

function output_gain_table(a_profit_per_ticket,a_cost_of_ticket;numtimes=1_000_000)
    global mypath
    println("Start of result")
    cost_of_ticket = a_cost_of_ticket
    profit_per_ticket = a_profit_per_ticket
    gain = profit_per_ticket/cost_of_ticket
    rounded_gain = round(gain,digits=1)
    str_rounded_gain = string(rounded_gain)
    fid = open("$(mypath)/perfectinfo_gain_$(str_rounded_gain).txt","w")

    for percentage = 0:99
        # Calculate f for Kelly Criterion
        p = percentage/100.0
        cost_of_ticket = a_cost_of_ticket
        profit_per_ticket = a_profit_per_ticket
        gain = profit_per_ticket/cost_of_ticket
        f = p - (1-p)/gain
        # Check if f is negative
        if f < 0
            prob_loss = 0.0
            # set f to zero
            f = 0.0
        else
            prob_loss=calculate_loss(f,p,gain,numtimes=numtimes)
        end
        rounded_f = round(f,sigdigits=4)
        rounded_prob_loss = round(prob_loss,sigdigits=4)
        # println(p,",",rounded_f,",",rounded_prob_loss)
        @printf("%-4.2f,%-6.4f,%-6.4f\n",p,rounded_f,rounded_prob_loss)
        @printf(fid,"%-4.2f,%-6.4f,%-6.4f\n",p,rounded_f,rounded_prob_loss)
        flush(fid)
    end

    close(fid)
end

println("profit per ticket = 3.0  cost of ticket = 1.0")
println("p,f,probability_of_loss")
output_gain_table(3.0,1.0)

Another thing I forgot to mention.

Kelly assumes that to get the maximum rate of return, you need to play the lottery sequentially an infinite number of times. In reality, most investors can only handle a finite number of games. You can still use Kelly Criterion if you play say 100 sequential games.

I mention the lottery but Kelly Criterion applys to any kind of investment with predictable returns as long as they have fixed binary outcomes, either win or lose.

If the fraction f predicted by Kelly Criterion is less than ZERO then what you have is a bum game and the only way to win is “to not to play”.

All real world lotteries has a negative f according to Kelly Criterion. So I let you figure out if you should play the lottery.

And here is a sample output of the run. Note that if p < 0.26 then f is negative so DONT PLAY so f is set to zero which means the probability of loss is also zero.

profit per ticket = 3.0  cost of ticket = 1.0
p,f,probability_of_loss
0.00,0.0000,0.0000
0.01,0.0000,0.0000
0.02,0.0000,0.0000
0.03,0.0000,0.0000
0.04,0.0000,0.0000
0.05,0.0000,0.0000
0.06,0.0000,0.0000
0.07,0.0000,0.0000
0.08,0.0000,0.0000
0.09,0.0000,0.0000
0.10,0.0000,0.0000
0.11,0.0000,0.0000
0.12,0.0000,0.0000
0.13,0.0000,0.0000
0.14,0.0000,0.0000
0.15,0.0000,0.0000
0.16,0.0000,0.0000
0.17,0.0000,0.0000
0.18,0.0000,0.0000
0.19,0.0000,0.0000
0.20,0.0000,0.0000
0.21,0.0000,0.0000
0.22,0.0000,0.0000
0.23,0.0000,0.0000
0.24,0.0000,0.0000
0.25,0.0000,0.0000
0.26,0.0133,0.4615
0.27,0.0267,0.3748
0.28,0.0400,0.3743
0.29,0.0533,0.2945
0.30,0.0667,0.2956
0.31,0.0800,0.2271
0.32,0.0933,0.2289
0.33,0.1067,0.1697
0.34,0.1200,0.1706
0.35,0.1333,0.1241
0.36,0.1467,0.1257
0.37,0.1600,0.0877
0.38,0.1733,0.0892
0.39,0.1867,0.0606
0.40,0.2000,0.0616
0.41,0.2133,0.0411
0.42,0.2267,0.0416
0.43,0.2400,0.0266
0.44,0.2533,0.0264
0.45,0.2667,0.0168
0.46,0.2800,0.0171
0.47,0.2933,0.0100
0.48,0.3067,0.0059
0.49,0.3200,0.0059
0.50,0.3333,0.0032
0.51,0.3467,0.0034
0.52,0.3600,0.0017
0.53,0.3733,0.0018
0.54,0.3867,0.0009
0.55,0.4000,0.0009
0.56,0.4133,0.0005
0.57,0.4267,0.0005
0.58,0.4400,0.0002
0.59,0.4533,0.0002
0.60,0.4667,0.0001
0.61,0.4800,0.0001
0.62,0.4933,0.0000
0.63,0.5067,0.0000
0.64,0.5200,0.0000
0.65,0.5333,0.0000
0.66,0.5467,0.0000
0.67,0.5600,0.0000
0.68,0.5733,0.0000
0.69,0.5867,0.0000
0.70,0.6000,0.0000
0.71,0.6133,0.0000
0.72,0.6267,0.0000
0.73,0.6400,0.0000
0.74,0.6533,0.0000
0.75,0.6667,0.0000
0.76,0.6800,0.0000
0.77,0.6933,0.0000
0.78,0.7067,0.0000
0.79,0.7200,0.0000
0.80,0.7333,0.0000
0.81,0.7467,0.0000
0.82,0.7600,0.0000
0.83,0.7733,0.0000
0.84,0.7867,0.0000
0.85,0.8000,0.0000
0.86,0.8133,0.0000
0.87,0.8267,0.0000
0.88,0.8400,0.0000
0.89,0.8533,0.0000
0.90,0.8667,0.0000
0.91,0.8800,0.0000
0.92,0.8933,0.0000
0.93,0.9067,0.0000
0.94,0.9200,0.0000
0.95,0.9333,0.0000
0.96,0.9467,0.0000
0.97,0.9600,0.0000
0.98,0.9733,0.0000
0.99,0.9867,0.0000