How to compute the Kelly Criterion using Python

1 min readAlgorithmic TradingFinanceAI
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

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.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.