Skip to content

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

Open
realsamrat wants to merge 3 commits intoTheAlgorithms:masterfrom
realsamrat:feature/kelly-criterion-sharpe-ratio
Open

feat: Add Kelly Criterion and Sharpe Ratio financial algorithms#14346
realsamrat wants to merge 3 commits intoTheAlgorithms:masterfrom
realsamrat:feature/kelly-criterion-sharpe-ratio

Conversation

@realsamrat
Copy link

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-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Mar 7, 2026
@realsamrat realsamrat force-pushed the feature/kelly-criterion-sharpe-ratio branch from f6f082f to a78b261 Compare March 7, 2026 13:44
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants