Repository navigation
fix(returns): treat constant returns as zero volatility in Sharpe ratio - #904
Merged
polakowo merged 2 commits intoNov 11, 2026
Merged
Conversation
polakowo
requested changes
Nov 6, 2026
polakowo
left a comment
Owner
There was a problem hiding this comment.
Thanks for the fix! The contiguous Rust path returns infinity for constant +/-inf columns, while Numba, Fortran-layout Rust and rolling Sharpe return NaN. The new constant shortcut needs to exclude NaN standard deviation, as the other paths already do. Please add nonfinite/layout regression coverage.
Match sharpe_ratio_1d: only treat a column as zero-volatility when std is finite. Constant +/-inf columns have NaN std and must return NaN on the C-contiguous Rust path, same as Numba and Fortran-layout Rust. Adds nonfinite/layout regression coverage in test_sharpe_ratio_constant_returns.
Contributor
Author
|
Thanks — updated the contiguous Rust path to match |
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.
Closes #896 (the constant-returns part that #897 left open).
A constant return series can end up with a mean that is a few ULPs away from the constant, so the standard deviation comes out around 1e-19 instead of zero and the Sharpe ratio is about ±3e16. With 252 daily returns of 0.001,
sharpe_ratio()gives 2.93e16 on master.Rather than pick a tolerance, this checks whether all non-NaN (risk-adjusted) returns are exactly equal. If they are, the standard deviation is treated as zero and the existing #897 rule applies: NaN when the mean is zero, otherwise signed infinity. Series that only look constant (a real but tiny spread) are unchanged.
_is_constant_1d_nbinvectorbt/returns/nb.py, used bysharpe_ratio_1d_nb(and thereforesharpe_ratio_nbandrolling_sharpe_ratio_nb).is_constant_shiftedforsharpe_ratio_1d, and a per-column constant flag tracked in the existing first pass of the contiguoussharpe_ratio_2dpath, so no extra pass over the data.Sortino is not affected: its downside risk for constant returns is already exactly zero (positive constant) or clearly non-zero (negative constant).
Tests: new
test_sharpe_ratio_constant_returnscovers positive, negative and NaN-containing constant columns, plus a constant equal torisk_free(NaN), on both engines. It fails on master with[2.93e16, -2.93e16, 2.93e16].tests/test_returns.py,test_engine.py,test_records.py,test_portfolio.pyandtest_generic.pypass locally with the Rust extension built (maturin develop --release), and Numba and Rust give identical results for constant, zero, NaN-containing and random columns, including the rolling version.