[BUG] sharpe_ratio annualises numerator and denominator on different clocks with no override, and its rolling window is off by one
Describe the bug
sharpe_ratio in gs_quant/timeseries/econometrics.py annualises its two halves on different clocks. The numerator (_annualized_return, around line 120 to 140) is a geometric return raised to 365.25 / calendar days, which is right for any calendar. The denominator (volatility(er, w), around line 151) multiplies the standard deviation by the square root of a factor inferred from the mean gap between observations, and any gap under about 2.1 days maps to 252. On a seven day calendar (crypto, or any series with weekend observations) the two halves disagree by sqrt(365 / 252) = 1.2035. volatility accepts annualization_factor to correct this, but sharpe_ratio and _get_ratio do not expose it, so there is no way to get a consistent ratio.
Separately, with an integer window w the numerator takes its base level at t - w + 1, so it spans w - 1 returns, while volatility(er, w) spans w returns (levels t - w .. t). The docstring formula is inconsistent with itself in the same way: E_t / E_{t-w+1} in the base but D_t - D_{t-w} in the exponent.
To Reproduce
import math, numpy as np, pandas as pd
from gs_quant.timeseries import econometrics as ec
from gs_quant.timeseries.helper import CurveType
# 1. seven day calendar
r = np.tile([0.01, -0.005], 183)[:365]
p7 = pd.Series(100 * np.cumprod(1 + r), index=pd.date_range("2024-01-01", periods=365))
sr = ec.sharpe_ratio(p7, curve_type=CurveType.EXCESS_RETURNS).iloc[-1]
rv = p7.pct_change().dropna().values
days = (p7.index[-1] - p7.index[0]).days
num = (p7.iloc[-1] / p7.iloc[0]) ** (365.25 / days) - 1
print(sr) # 12.27908
print(num / (rv.std(ddof=1) * math.sqrt(252))) # 12.27908 what the code does
print(num / (rv.std(ddof=1) * math.sqrt(365))) # 10.202807 consistent with the numerator
# 2. rolling window off by one
w = 22
px = pd.Series(100 * np.cumprod(1 + np.random.default_rng(3).normal(0.0005, 0.012, 120)), index=pd.bdate_range("2024-01-01", periods=120))
srw = ec.sharpe_ratio(px, w=w, curve_type=CurveType.EXCESS_RETURNS).iloc[-1]
t = len(px) - 1; rets = px.pct_change()
n21 = (px.iloc[t] / px.iloc[t - w + 1]) ** (365.25 / (px.index[t] - px.index[t - w + 1]).days) - 1
n22 = (px.iloc[t] / px.iloc[t - w]) ** (365.25 / (px.index[t] - px.index[t - w]).days) - 1
v22 = rets.iloc[t - w + 1:t + 1].std(ddof=1) * math.sqrt(252)
v21 = rets.iloc[t - w + 2:t + 1].std(ddof=1) * math.sqrt(252)
print(srw) # 2.885145
print(n21 / v22) # 2.885145 21 returns in the numerator against 22 in the volatility
print(n21 / v21) # 2.835768 21 and 21
print(n22 / v22) # 3.671278 22 and 22Expected behavior Both halves annualised on the same clock, and both halves of a rolling ratio spanning the same returns.
Screenshots Not applicable.
Systems setup:
- OS: macOS 26 (Darwin 25.6.0), arm64
- Python version: 3.12
- GS-Quant version: 2.1.17, commit f2a505a; pandas 3.0.6, numpy 2.3.5
Additional context
Suggested fix: add annualization_factor: Optional[int] = None to sharpe_ratio and _get_ratio and pass it through to volatility, and either start the numerator at t - w or the volatility at t - w + 1. The pinned SR10 column in test/resources/Sharpe_SPX_0175.csv encodes the current off by one, so that fixture changes with the fix.
Source: goldmansachs/gs-quant