Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sortino ratio on short time series -> divition by zero #296

Unanswered
emiliobasualdo asked this question in Q&A
Discussion options

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!

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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.

You must be logged in to vote
1 reply
Comment options

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /