-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Sortino ratio on short time series -> divition by zero #296
-
Hi guys. I don't want to place an issue because I might not understand well the Sortino Ratio.
Basically, I was optimizing with bt.optimize
and set the logger toroot.setLevel(logging.INFO)
and a warning appeared:
.venv/lib/python3.8/site-packages/backtesting/backtesting.py:1584: RuntimeWarning: divide by zero encountered in double_scalars
s.loc['Sortino Ratio'] = np.clip(annualized_return / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)), 0, np.inf) # noqa: E501
I'm optimizing with a time series that has minute candles and lasts almost 4 days.
I can confirm with the debugger that:
(np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days))
= 0
and that
day_returns=
(Timestamp('2021-03-16 00:00:00', freq='D'), nan)
(Timestamp('2021-03-17 00:00:00', freq='D'), 0.0209088078795423)
(Timestamp('2021-03-18 00:00:00', freq='D'), 0.0032580727254232222)
(Timestamp('2021-03-19 00:00:00', freq='D'), 0.00014225292238778664)
and that
day_returns.clip(-np.inf, 0)=
(Timestamp('2021-03-16 00:00:00', freq='D'), nan)
(Timestamp('2021-03-17 00:00:00', freq='D'), 0.0)
(Timestamp('2021-03-18 00:00:00', freq='D'), 0.0)
(Timestamp('2021-03-19 00:00:00', freq='D'), 0.0)
My questions are:
I this a normal expected outcome?
Does it make sense to study the Sortino Ratio over this series?
Thank you for your help.
Regards!
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
I'm optimizing with a time series that has minute candles and lasts almost 4 days.
Well, you only have 4 days and none of those days' returns are negative. Since this is the same as in Quantopian empyrical:
https://github.com/quantopian/empyrical/blob/b026b4e08b7147ee7e71ff5622f13913cdcaa99d/empyrical/stats.py#L870-L877
I think we're in clear, and Sortino is indeed just not suited to your sample of data.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks!
Beta Was this translation helpful? Give feedback.