Skip to content

Commit a3ba8a7

Browse files
committed
REF: Avoid RuntimeWarning division by zero in Sortino Ratio
Fixes #584
1 parent 881ddd0 commit a3ba8a7

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

backtesting/_stats.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ def _round_timedelta(value, _period=_data_period(index)):
147147
# and simple standard deviation
148148
s.loc['Sharpe Ratio'] = (s.loc['Return (Ann.) [%]'] - risk_free_rate * 100) / (s.loc['Volatility (Ann.) [%]'] or np.nan) # noqa: E501
149149
# Our Sortino mismatches `empyrical.sortino_ratio()` because they use arithmetic mean return
150-
s.loc['Sortino Ratio'] = (annualized_return - risk_free_rate) / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)) # noqa: E501
150+
with np.errstate(divide='ignore'):
151+
s.loc['Sortino Ratio'] = (annualized_return - risk_free_rate) / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)) # noqa: E501
151152
max_dd = -np.nan_to_num(dd.max())
152153
s.loc['Calmar Ratio'] = annualized_return / (-max_dd or np.nan)
153154
s.loc['Max. Drawdown [%]'] = max_dd * 100

0 commit comments

Comments
 (0)