Skip to main content

A python library for bankroll allocation strategies

Project description

Keeks

License: MIT

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:

St. Petersburg Paradox Strategy Comparison

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:

  1. Kelly Criterion: The mathematically optimal strategy for maximizing the logarithm of wealth

    kelly = KellyCriterion(payoff=1.0, loss=1.0, transaction_cost=0.01)
    
  2. 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)
    
  3. 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)
    
  4. 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)
    
  5. Fixed Fraction: Simple strategy that bets a constant percentage of the bankroll

    fixed_fraction = FixedFractionStrategy(fraction=0.05, min_probability=0.5)
    
  6. 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)
    
  7. Dynamic Bankroll Management: Adaptive strategy based on recent performance

    dynamic = DynamicBankrollManagement(base_fraction=0.1, payoff=1.0, loss=1.0, window_size=10)
    
  8. 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)
    
  9. 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:

  1. RepeatedBinarySimulator: For scenarios with a fixed probability
  2. RandomBinarySimulator: For scenarios with varying probabilities
  3. 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

License

MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keeks-0.3.0.tar.gz (651.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keeks-0.3.0-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

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

Hashes for keeks-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2ffd73fb128fadb70b5d3fa0ba123225756cb7c8729e3b7809ebdc02e2cb46cb
MD5 1b403ffb273e9a4be444464374237e7d
BLAKE2b-256 586f2fcd833b9626e8a401e228c395f6974e770da4777dd197daf41e6889a946

See more details on using hashes here.

Provenance

The following attestation bundles were made for keeks-0.3.0.tar.gz:

Publisher: publish-pypi.yml on wdm0006/keeks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for keeks-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b018171325ca9c884299e1d6cd4610187099aa467593696ef3f10586fae64ada
MD5 aeb38f9f493069dab9e6edd31cc04c51
BLAKE2b-256 da47b790dbeb5e5b468f953db2ea3e13d95f3dcfed21404d89204313602418ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for keeks-0.3.0-py3-none-any.whl:

Publisher: publish-pypi.yml on wdm0006/keeks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page