[BUG] BackTest.summary_stats divides the Sortino downside deviation by the count of losing days only
Describe the bug
BackTest.summary_stats in gs_quant/backtests/backtest_objects.py computes the Sortino denominator as the root mean square of the losing days only:
downside = daily_pnl[daily_pnl < 0]
downside_std = np.sqrt((downside**2).mean()) if len(downside) > 0 else 0.0Downside deviation is defined over all N periods, sqrt(sum(min(r - MAR, 0)^2) / N) (Sortino and van der Meer 1991; Rollinger and Hoffman, "Sortino: A Sharper Ratio", 2013, which names dividing by the losing periods only as the common error). Dividing by the count of losses inflates the denominator by sqrt(N / N_neg) and makes the ratio depend on how the losses are spread rather than on their size, so two strategies with identical losses and a different number of flat days get the same downside deviation.
To Reproduce
import math, numpy as np, pandas as pd
from gs_quant.backtests.backtest_objects import BackTest
pnl = np.array([1.0, 2.0, -1.0, 3.0, -2.0, 1.0, 0.5, -1.0, 2.0, 1.5])
total = pd.Series(np.concatenate([[0.0], np.cumsum(pnl)]), index=pd.bdate_range("2024-01-01", periods=11))
class Fake(BackTest): # just enough BackTest to call summary_stats on a known PnL path
result_summary = property(lambda self: pd.DataFrame({"Total": total}))
def trade_ledger(self): return pd.DataFrame()
s = object.__new__(Fake).summary_stats()
ann = pnl.mean() * 252
print(s["Sortino Ratio"]) # 7.857481
print(ann / (math.sqrt((np.minimum(pnl, 0) ** 2).mean()) * math.sqrt(252))) # 14.345731 all N
print(ann / (math.sqrt((pnl[pnl < 0] ** 2).mean()) * math.sqrt(252))) # 7.857481 losing days onlyThe two differ by exactly sqrt(10 / 3) = 1.8257. The Sharpe row on the same vector matches mean / sd * sqrt(252) (7.012378), so only the Sortino is off.
Expected behavior
Sortino Ratio of 14.345731 on that vector, from np.sqrt((np.minimum(daily_pnl, 0) ** 2).mean()).
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 The docstring says "annualised return / annualised downside deviation" and cites no other convention. The same point was part of the closed PR #362, which its author withdrew without review; there is no open issue for it.
Source: goldmansachs/gs-quant