feat: Add Kelly Criterion and Sharpe Ratio financial algorithms#14346
Open
realsamrat wants to merge 3 commits intoTheAlgorithms:masterfrom
Open
feat: Add Kelly Criterion and Sharpe Ratio financial algorithms#14346realsamrat wants to merge 3 commits intoTheAlgorithms:masterfrom
realsamrat wants to merge 3 commits intoTheAlgorithms:masterfrom
Conversation
f6f082f to
a78b261
Compare
mindaugl
approved these changes
Mar 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 formulakelly_criterion_extended(win_probability, win_amount, loss_amount)- General form with explicit amountsfractional_kelly(win_probability, win_loss_ratio, fraction)- Fractional Kelly (e.g., half-Kelly) for reduced volatilityApplications:
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 calculationannualized_sharpe_ratio(returns, risk_free_rate, periods_per_year)- Annualized version for different time periodsApplications:
References
Testing
✅ All doctests pass:
python -m doctest financial/kelly_criterion.py -v- 22/22 tests passedpython -m doctest financial/sharpe_ratio.py -v- 14/14 tests passedBoth files follow TheAlgorithms style guide:
Checklist
financial/directory