fix: use target downside deviation in Sortino ratio#285
Merged
Conversation
Replace std(ddof=1) with sqrt(mean(min(r,0)^2)) for the downside deviation in calc_sortino_ratio. The previous implementation used pandas .std() (sample standard deviation) on the clipped returns, which subtracts the mean of the clipped series before squaring and uses Bessel's correction (N-1). The standard Sortino ratio uses Target Downside Deviation (TDD), defined as sqrt(1/N * sum(min(r,0)^2)), which is the root mean square of the clipped returns. Fixes #254 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes the downside deviation calculation in
calc_sortino_ratioto use the standard Target Downside Deviation (TDD) formula instead of sample standard deviation.The bug: The previous implementation used
.std(ddof=1)(sample standard deviation) on the clipped return series. This is incorrect because:.std()subtracts the mean of the clipped series (a negative number) before squaring, so zero entries from positive returns contributemean^2each to the variance — they don't actually "zero out"The fix: Replace
.std(ddof=1)withsqrt(mean(min(r, 0)^2)), which is the standard Target Downside Deviation per Sortino, van der Meer & Plantinga (1999). This is the root mean square of the clipped (at zero) excess returns, where positive returns correctly contribute zero to the sum.The
.clip(upper=0.0)logic is unchanged and correct — zeros from positive-return periods belong in the formula (they increase N in the denominator of the mean, reflecting that calm periods reduce downside risk).Closes #254
Note on #169: Issue #169 suggests using geometric mean instead of arithmetic mean for the numerator. After review, the current arithmetic mean approach is the standard convention for the Sortino ratio (consistent with this library's Sharpe ratio implementation and with Investopedia/academic references). No change made for that issue.
Test plan
test_calc_sortino_ratioto verify against the corrected TDD formulaGenerated with Claude Code