Skip to content

Commit 18af117

Browse files
authored
Minor update
1 parent a05394c commit 18af117

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

backtesting/_stats.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,14 @@ def _round_timedelta(value, _period=_data_period(index)):
154154
s.loc['Calmar Ratio'] = annualized_return / (-max_dd or np.nan)
155155
equity_log_returns = np.log(equity[1:] / equity[:-1])
156156
market_log_returns = np.log(c[1:] / c[:-1])
157+
beta = np.nan
157158
if len(equity_log_returns) > 1 and len(market_log_returns) > 1:
159+
# len == 0 on dummy call `stats_keys = compute_stats(...)` pre optimization
158160
cov_matrix = np.cov(equity_log_returns, market_log_returns)
159161
beta = cov_matrix[0, 1] / cov_matrix[1, 1]
160-
# Jensen CAPM Alpha: can be strongly positive when beta is negative and B&H Return is large
161-
s.loc['Alpha [%]'] = s.loc['Return [%]'] - risk_free_rate * 100 - beta * (s.loc['Buy & Hold Return [%]'] - risk_free_rate * 100) # noqa: E501
162-
s.loc['Beta'] = beta
163-
else:
164-
s.loc['Alpha [%]'] = np.nan
165-
s.loc['Beta'] = np.nan
162+
# Jensen CAPM Alpha: can be strongly positive when beta is negative and B&H Return is large
163+
s.loc['Alpha [%]'] = s.loc['Return [%]'] - risk_free_rate * 100 - beta * (s.loc['Buy & Hold Return [%]'] - risk_free_rate * 100) # noqa: E501
164+
s.loc['Beta'] = beta
166165
s.loc['Max. Drawdown [%]'] = max_dd * 100
167166
s.loc['Avg. Drawdown [%]'] = -dd_peaks.mean() * 100
168167
s.loc['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max())

0 commit comments

Comments
 (0)