fix: prevent benchmark alignment look-ahead - #557
Open
JohnDReynolds wants to merge 1 commit into
Open
JohnDReynolds wants to merge 1 commit into
JohnDReynolds wants to merge 1 commit into
Conversation
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
Prevent benchmark alignment from moving a future benchmark return onto an earlier strategy date.
When a strategy calendar contains dates missing from the benchmark,
_prepare_benchmark()reconstructs benchmark prices on a daily index before converting them back to returns. Back-filling that price index allowed a later observed benchmark price to propagate backward, creating look-ahead in reports, plots, and statistics that consume the aligned benchmark.This changes the alignment from backward-fill to forward-fill so each benchmark return remains on its observation date.
Behavior
Consider a benchmark observed on Friday and Monday, with a 10% return on Monday, aligned to a daily strategy calendar:
Back-filling assigned Monday's price to earlier weekend dates, causing the return to appear before it occurred. Forward-filling carries Friday's last known price through the weekend and preserves the return on Monday.
The change preserves:
Regression evidence
[0, 0.10, 0, 0]instead of[0, 0, 0, 0.10]. Through the public statistics path,information_ratio()returned-0.261116483933547instead of the independently calculated-0.3100868364730212.0.32; timezone-aware DataFrame alignment preserves labels and caller state; andinformation_ratio()matches its independent active-return calculation.Verification
git diff --checkpassed.Scope
Changed files:
quantstats/utils.pytests/test_regressions.pyThe production change is limited to replacing backward price filling with forward price filling inside
_prepare_benchmark().Out of scope: