From 00ea7b04d10f095d064256f4dedff29c095466f3 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Sun, 26 Apr 2015 13:02:52 -0400 Subject: [PATCH] PERF: Reduce memory usage during risk metric calculations. For beta calculation: Remove `.dropna` , since it was creating a new Series and Index which inflated memory usage as algorithm run time progressed. For downside risk calculations: Instead of using pd.Series calculations, pass the underlying numpy array which have already been sliced to the exact dt, so that the call to `round` does not create a new Series. --- zipline/finance/risk/cumulative.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/zipline/finance/risk/cumulative.py b/zipline/finance/risk/cumulative.py index 943ba4a9..579fe064 100644 --- a/zipline/finance/risk/cumulative.py +++ b/zipline/finance/risk/cumulative.py @@ -449,8 +449,8 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" return np.std(daily_returns, ddof=1) * math.sqrt(252) def calculate_downside_risk(self): - return downside_risk(self.algorithm_returns, - self.mean_returns, + return downside_risk(self.algorithm_returns.values, + self.mean_returns.values, 252) def calculate_beta(self): @@ -462,19 +462,13 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" http://en.wikipedia.org/wiki/Beta_(finance) """ - - # Drop nans if there are gaps in the data - algorithm_returns = self.algorithm_returns.dropna() - benchmark_returns = \ - self.benchmark_returns.loc[algorithm_returns.index] - - # it doesn't make much sense to calculate beta for less than two days, - # so return none. - if len(algorithm_returns) < 2: + # it doesn't make much sense to calculate beta for less than two + # values, so return none. + if len(self.algorithm_returns) < 2: return 0.0 - returns_matrix = np.vstack([algorithm_returns, - benchmark_returns]) + returns_matrix = np.vstack([self.algorithm_returns, + self.benchmark_returns]) C = np.cov(returns_matrix, ddof=1) algorithm_covariance = C[0][1] benchmark_variance = C[1][1]