How to compute the Kelly Criterion using Python

1 min

The Kelly Criterion is a mathematical formula used to determine the optimal amount to bet on a positive expected value wager.

Here is an example of how to compute the Kelly criterion in Python:

import numpy as np

def kelly_criterion(p, b, q):
    p: probability of winning
    b: payout ratio
    q: probability of losing
    return (b*p - q) / b

p = 0.6 # probability of winning
b = 2 # payout ratio
q = 1-p # probability of losing

fraction = kelly_criterion(p, b, q)

print("The optimal fraction to bet is:", fraction)

In this example, the function kelly_criterion takes three parameters: p, which is the probability of winning; b, which is the payout ratio; and q, which is the probability of losing. The function returns the optimal fraction of the bankroll to bet, calculated using the Kelly Criterion formula (b*p - q) / b.

In this example, we have assigned the probability of winning to be 0.6, the payout ratio to be 2, and the probability of losing to be 1-p. The script prints the optimal fraction to bet, which is 0.6 in this case.

It's important to note that kelly criterion can be risky strategy, it's better to use it with caution and be aware of the risks involved.