Skip to content

feat: Add Kelly Criterion and Sharpe Ratio financial algorithms - #14346

Merged
cclauss merged 6 commits into
TheAlgorithms:masterfrom
realsamrat:feature/kelly-criterion-sharpe-ratio
Sep 5, 2026
Merged

feat: Add Kelly Criterion and Sharpe Ratio financial algorithms#14346
cclauss merged 6 commits into
TheAlgorithms:masterfrom
realsamrat:feature/kelly-criterion-sharpe-ratio

Conversation

@realsamrat

Copy link
Copy Markdown
Contributor

Description

This PR adds two new financial algorithm implementations to the financial/ directory:

1. Kelly Criterion (kelly_criterion.py)

The Kelly Criterion is a formula for optimal position sizing in betting and trading, developed by John L. Kelly Jr. in 1956. It determines the optimal fraction of capital to risk on a bet or investment to maximize long-term growth.

Functions implemented:

  • kelly_criterion(win_probability, win_loss_ratio) - Basic Kelly formula
  • kelly_criterion_extended(win_probability, win_amount, loss_amount) - General form with explicit amounts
  • fractional_kelly(win_probability, win_loss_ratio, fraction) - Fractional Kelly (e.g., half-Kelly) for reduced volatility

Applications:

  • Sports betting and gambling strategy
  • Investment portfolio sizing
  • Trading position management

2. Sharpe Ratio (sharpe_ratio.py)

The Sharpe Ratio measures risk-adjusted returns, developed by Nobel laureate William F. Sharpe. It calculates excess return per unit of risk (standard deviation).

Functions implemented:

  • sharpe_ratio(returns, risk_free_rate) - Basic Sharpe Ratio calculation
  • annualized_sharpe_ratio(returns, risk_free_rate, periods_per_year) - Annualized version for different time periods

Applications:

  • Portfolio performance comparison
  • Mutual fund and hedge fund evaluation
  • Trading strategy assessment

References

Testing

✅ All doctests pass:

  • python -m doctest financial/kelly_criterion.py -v - 22/22 tests passed
  • python -m doctest financial/sharpe_ratio.py -v - 14/14 tests passed

Both files follow TheAlgorithms style guide:

  • Complete docstrings with formula explanations
  • Comprehensive doctests including edge cases
  • Type hints on all functions
  • Pure Python (no external dependencies)
  • Input validation with appropriate error messages

Checklist

  • Added comprehensive doctests with edge cases
  • Type hints on all parameters and return values
  • No external dependencies (pure Python stdlib)
  • Follows existing code style in financial/ directory
  • All doctests pass
  • Includes Wikipedia and Investopedia references

@algorithms-keeperalgorithms-keeperBot added the tests are failing Do not merge until tests pass label Mar 7, 2026
@realsamrat
realsamratforce-pushed the feature/kelly-criterion-sharpe-ratio branch from f6f082f to a78b261CompareMarch 7, 2026 13:44
@algorithms-keeperalgorithms-keeperBot removed the tests are failing Do not merge until tests pass label Mar 7, 2026
Comment threaddata_structures/hashing/hash_table_with_linked_list.py
Comment threadmachine_learning/linear_discriminant_analysis.py
Comment threadsearches/jump_search.py
@cclauss
cclauss merged commit e5cca54 into TheAlgorithms:masterSep 5, 2026
4 of 5 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@realsamrat@cclauss@mindaugl