Skip to content

Commit

Permalink
MAINT: Make beta calculation robust to missing values.
Browse files Browse the repository at this point in the history
Risk calculations are robust to nans, except for
beta which calls numpy with the complete list of
algorithm_returns. If nans are present the result
of covar will be nan.

This is fixed by filtering out nans in
algorithm_returns.
  • Loading branch information
twiecki committed Dec 29, 2014
1 parent a3d7483 commit c1216c8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions zipline/finance/risk/cumulative.py
Expand Up @@ -441,8 +441,13 @@ def calculate_beta(self):
if len(self.annualized_mean_returns) < 2:
return 0.0

returns_matrix = np.vstack([self.algorithm_returns,
self.benchmark_returns])
# Drop nans if there are gaps in the data
algorithm_returns = self.algorithm_returns.dropna()
benchmark_returns = \
self.benchmark_returns.loc[algorithm_returns.index]

returns_matrix = np.vstack([algorithm_returns,
benchmark_returns])
C = np.cov(returns_matrix, ddof=1)
algorithm_covariance = C[0][1]
benchmark_variance = C[1][1]
Expand Down

0 comments on commit c1216c8

Please sign in to comment.