Skip to content

fix(returns): treat constant returns as zero volatility in Sharpe ratio - #904

Merged
polakowo merged 2 commits into
polakowo:masterfrom
gautamggg53:fix/sharpe-constant-returns
Nov 11, 2026
Merged

polakowo merged 2 commits into
polakowo:masterfrom
gautamggg53:fix/sharpe-constant-returns

Conversation

@gautamggg53

Copy link
Copy Markdown
Contributor

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.

  • Numba: _is_constant_1d_nb in vectorbt/returns/nb.py, used by sharpe_ratio_1d_nb (and therefore sharpe_ratio_nb and rolling_sharpe_ratio_nb).
  • Rust: is_constant_shifted for sharpe_ratio_1d, and a per-column constant flag tracked in the existing first pass of the contiguous sharpe_ratio_2d path, 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_returns covers positive, negative and NaN-containing constant columns, plus a constant equal to risk_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.py and test_generic.py pass 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.

@polakowo polakowo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@gautamggg53

Copy link
Copy Markdown
Contributor Author

Thanks — updated the contiguous Rust path to match sharpe_ratio_1d: the constant shortcut now only applies when std is finite (std == 0.0 || (!std.is_nan() && constant[col])). Added regression coverage for constant +/-inf columns on both C-contiguous and Fortran-layout inputs (numba + rust).

@polakowo polakowo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! The nonfinite/layout regression is fixed. The affected returns, portfolio and engine suites pass locally with the rebuilt Rust extension (507 tests, including #903). Looks good pending CI.

@polakowo
polakowo merged commit 10c0e3d into polakowo:master Nov 11, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sharpe_ratio returns +inf for all-zero returns and ~3e16 for constant returns

2 participants