A python library for bankroll allocation strategies
Project description
Keeks
A Python library for optimal bankroll allocation and betting strategies, with a focus on the Kelly Criterion and its variants.
Full documentation at keeks.mcginniscommawill.com.
What is Keeks?
Keeks is a specialized Python library designed to help you implement and test various betting and investment strategies. It provides tools for:
- Bankroll management: Track and manage your funds with built-in protection against excessive losses
- Betting strategies: Implement mathematically optimal strategies like the Kelly Criterion
- Simulation: Test your strategies under different conditions before risking real money
Whether you're a sports bettor, a financial trader, or a researcher in decision theory, Keeks provides the tools to make more informed decisions about capital allocation.
Why Use Keeks?
- Mathematically sound: Based on proven mathematical principles like the Kelly Criterion
- Risk management: Built-in protection against ruin with configurable drawdown limits
- Simulation-driven: Test strategies in various scenarios before applying them with real money
- Flexible: Supports different types of betting scenarios and probability distributions
- Educational: Learn about optimal betting strategies through practical implementation
Disclaimer: This library is for educational purposes only. It is not intended to provide investment, legal, or tax advice. Always be responsible and consult with a professional before applying these strategies to real-world betting or investment scenarios. The authors and contributors of this library are not liable for any financial losses or damages that may result from the use of this software.
Installation
pip install keeks
Quick Start
Here's a simple example of how to use Keeks to simulate a betting strategy:
from keeks.bankroll import BankRoll
from keeks.binary_strategies.kelly import KellyCriterion
from keeks.simulators.repeated_binary import RepeatedBinarySimulator
# Create a bankroll with initial funds
bankroll = BankRoll(initial_funds=1000.0, max_draw_down=0.3)
# Create a Kelly Criterion strategy
# Parameters: payoff, loss, transaction_cost
strategy = KellyCriterion(payoff=1.0, loss=1.0, transaction_cost=0.01)
# Create a simulator with a fixed probability
simulator = RepeatedBinarySimulator(
payoff=1.0,
loss=1.0,
transaction_costs=0.01,
probability=0.55, # 55% chance of winning
trials=1000
)
# Run the simulation
simulator.evaluate_strategy(strategy, bankroll)
# Plot the results
bankroll.plot_history()
Examples
St. Petersburg Paradox Simulation
The St. Petersburg paradox is a theoretical game with infinite expected value but finite practical outcomes. Our example compares all binary strategies using a simplified binary model with favorable odds.
python -m examples.st_petersburg_comparison
The example simulates various strategies under favorable betting conditions and visualizes the results:
This chart shows the distribution of final bankrolls across different betting strategies after multiple simulations. Notice how Optimal-F and Kelly Criterion achieved the highest returns but with greater volatility, while more conservative strategies like Quarter Kelly had more consistent (but lower) returns.
For more examples, check the examples directory.
Key Features
Bankroll Management
The BankRoll class provides a way to track your funds and enforce risk management:
# Create a bankroll with $1000, allowing only 80% to be bet, and a 30% max drawdown limit
bankroll = BankRoll(initial_funds=1000.0, percent_bettable=0.8, max_draw_down=0.3)
Betting Strategies
Keeks implements several betting strategies:
-
Kelly Criterion: The mathematically optimal strategy for maximizing the logarithm of wealth
kelly = KellyCriterion(payoff=1.0, loss=1.0, transaction_cost=0.01)
-
Fractional Kelly: A more conservative version of Kelly that reduces volatility
fractional_kelly = FractionalKellyCriterion(payoff=1.0, loss=1.0, transaction_cost=0.01, fraction=0.5)
-
Drawdown-Adjusted Kelly: A Kelly variant that adjusts bet sizing based on risk tolerance
drawdown_kelly = DrawdownAdjustedKelly(payoff=1.0, loss=1.0, transaction_cost=0.01, max_acceptable_drawdown=0.2)
-
OptimalF (Ralph Vince): Strategy that maximizes geometric growth rate
from keeks.binary_strategies.simple import OptimalF optimal_f = OptimalF(payoff=1.0, loss=1.0, transaction_cost=0.01, win_rate=0.55, max_risk_fraction=0.2)
-
Fixed Fraction: Simple strategy that bets a constant percentage of the bankroll
fixed_fraction = FixedFractionStrategy(fraction=0.05, min_probability=0.5)
-
CPPI (Constant Proportion Portfolio Insurance): Strategy that protects a floor value while allowing upside exposure
cppi = CPPIStrategy(floor_fraction=0.5, multiplier=2.0, initial_bankroll=1000.0)
-
Dynamic Bankroll Management: Adaptive strategy based on recent performance
dynamic = DynamicBankrollManagement(base_fraction=0.1, payoff=1.0, loss=1.0, window_size=10)
-
Merton Share (CRRA Utility): Based on Merton's portfolio problem with constant relative risk aversion
from keeks.binary_strategies.simple import MertonShare merton = MertonShare(payoff=1.0, loss=1.0, transaction_cost=0.01, risk_aversion=2.0)
-
Naive Strategy: A simple strategy that bets the full amount when expected value is positive
naive = NaiveStrategy(payoff=1.0, loss=1.0, transaction_cost=0.01)
Utility Functions
For one-time decision problems (e.g., "What should I pay for this opportunity?"), keeks provides CRRA utility functions:
from keeks.utils import find_indifference_price
# Calculate maximum price you'd pay for a gamble
# Example: St. Petersburg paradox
outcomes = [2**n for n in range(1, 31)]
probabilities = [(0.5)**n for n in range(1, 31)]
max_price = find_indifference_price(
outcomes=outcomes,
probabilities=probabilities,
current_wealth=10000,
risk_aversion=2.0 # 1.0=Kelly, 2.0=moderate, 5.0=conservative
)
# Returns: ~$12.80 despite infinite expected value!
See examples/st_petersburg_paradox.py for a complete demonstration.
Simulators
Test your strategies with different simulators:
- RepeatedBinarySimulator: For scenarios with a fixed probability
- RandomBinarySimulator: For scenarios with varying probabilities
- RandomUncertainBinarySimulator: For scenarios where your probability estimate has uncertainty
Applications
Keeks can be applied to various domains:
- Sports Betting: Optimize your bet sizing based on your edge
- Financial Trading: Apply Kelly principles to portfolio management
- Gambling: Understand the mathematics behind optimal betting
- Research: Study the behavior of different betting strategies
- Education: Learn about probability, statistics, and risk management
Documentation
To build the docs locally:
git clone https://github.com/wdm0006/keeks.git
cd keeks
pip install -e ".[dev]"
make docs
Development
To set up the development environment:
git clone https://github.com/wdm0006/keeks.git
cd keeks
make setup
make install-dev
Run tests:
make test
References
- [1] A New Interpretation of Information Rate - The original Kelly Criterion paper
- [2] The Kelly Criterion in Blackjack, Sports Betting, and the Stock Market - A practical guide to applying the Kelly Criterion
- [3] Fortune's Formula - The untold story of the scientific betting system that beat the casinos and Wall Street
License
MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file keeks-0.3.0.tar.gz.
File metadata
- Download URL: keeks-0.3.0.tar.gz
- Upload date:
- Size: 651.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ffd73fb128fadb70b5d3fa0ba123225756cb7c8729e3b7809ebdc02e2cb46cb
|
|
| MD5 |
1b403ffb273e9a4be444464374237e7d
|
|
| BLAKE2b-256 |
586f2fcd833b9626e8a401e228c395f6974e770da4777dd197daf41e6889a946
|
Provenance
The following attestation bundles were made for keeks-0.3.0.tar.gz:
Publisher:
publish-pypi.yml on wdm0006/keeks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
keeks-0.3.0.tar.gz -
Subject digest:
2ffd73fb128fadb70b5d3fa0ba123225756cb7c8729e3b7809ebdc02e2cb46cb - Sigstore transparency entry: 601029757
- Sigstore integration time:
-
Permalink:
wdm0006/keeks@01414840af3fe6baf72f4d8bfb7f7eee0dda7619 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/wdm0006
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@01414840af3fe6baf72f4d8bfb7f7eee0dda7619 -
Trigger Event:
push
-
Statement type:
File details
Details for the file keeks-0.3.0-py3-none-any.whl.
File metadata
- Download URL: keeks-0.3.0-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b018171325ca9c884299e1d6cd4610187099aa467593696ef3f10586fae64ada
|
|
| MD5 |
aeb38f9f493069dab9e6edd31cc04c51
|
|
| BLAKE2b-256 |
da47b790dbeb5e5b468f953db2ea3e13d95f3dcfed21404d89204313602418ce
|
Provenance
The following attestation bundles were made for keeks-0.3.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on wdm0006/keeks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
keeks-0.3.0-py3-none-any.whl -
Subject digest:
b018171325ca9c884299e1d6cd4610187099aa467593696ef3f10586fae64ada - Sigstore transparency entry: 601029762
- Sigstore integration time:
-
Permalink:
wdm0006/keeks@01414840af3fe6baf72f4d8bfb7f7eee0dda7619 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/wdm0006
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@01414840af3fe6baf72f4d8bfb7f7eee0dda7619 -
Trigger Event:
push
-
Statement type: