Kelly Calculator for Multiple Outcomes

Previously I wrote about a script that, given a set of binary bets, calculates the optimal ways to allocate capital among these bets. In this post, I will write about another kelly calculator. One that deals with a single bet that has more than two possible outcomes. Such bets are common in the investing world, where infinite many ways that an investment can turn out. For example, one year from now, a stock could have made a profit, stayed flat, or lost money.

If the reader is interested, she could read Mohish Pabrai’s The Dhandho Investor, where Pabrai talked extensively about applying Kelly criteria to size potential investment opportunities.

To use the script, specify several parameters:

Once these are specified, the script will use Stochastic Gradient Ascent to find the optimal bet size for this bet. By default, the script will iteriate 20000 times, which I found adequate for the algorithm to converge.

import torch

t = torch.tensor

probs = t([0.45,0.45,0.05,0.05])
payouts = t([4,3,1.5,0])
bet_size = t([0.0], requires_grad = True)
lr = 0.001

def exp_wealth_log(bet_size):
    log_payout_matrix = torch.log(bet_size*payouts+1-torch.sum(bet_size))
    log_wealth = torch.mul(probs,log_payout_matrix).sum()
    return log_wealth

def apply_step(bet_size):
    result = exp_wealth_log(bet_size)
    result.backward()
    bet_size.data += lr * bet_size.grad.data
    bet_size.data = torch.clamp(bet_size.data, min=0, max=1)
    bet_size.grad = None

for i in range(20000): apply_step(bet_size)

print(bet_size.data)