BUG: Fix CAGR and annualized return calculations#1346
Open
blue-int wants to merge 1 commit intokernc:masterfrom
Open
BUG: Fix CAGR and annualized return calculations#1346blue-int wants to merge 1 commit intokernc:masterfrom
blue-int wants to merge 1 commit intokernc:masterfrom
Conversation
CAGR was dividing calendar days by `annual_trading_days` (e.g. 252) instead of 365.25, inflating the time period and underreporting CAGR. For weekly/monthly data this was drastically wrong (e.g. 365/52=7 years instead of 1 year). Return (Ann.) was slightly underestimated because `pct_change()` produces a leading NaN that `geometric_mean()` counted in the denominator via `fillna(0)`, diluting the geometric mean. 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
time_in_yearswas computed by dividing calendar days byannual_trading_days(e.g. 252, 52, 12) instead of 365.25. This inflated the time period and underreported CAGR. For weekly/monthly data, results were drastically wrong (e.g. 365/52 = 7 years instead of ~1 year).pct_change()produces a leading NaN thatgeometric_mean()counted in the denominator viafillna(0), slightly diluting the geometric mean and underestimating annualized return.After the fix, CAGR and Return (Ann.) are now consistent with each other as expected (21.16% vs 21.19% on GOOG SmaCross, previously 14.16% vs 21.18%).
Test plan
test_compute_statstest_FractionalBacktestis a pre-existing failure unrelated to this change)🤖 Generated with Claude Code