From 1bad2456752b3e68976509d09f16b84d0b1b03ba Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Thu, 10 Oct 2013 11:47:28 -0400 Subject: [PATCH] ENH: Use annualized returns for beta and alpha. So that the units match the other risk calculations, also use annualized returns for beat and alpha. Update answer key to match values calculated on the first day. Also, update performance tracker test so that the returns used are fractional instead of > 1, so that the annualized numbers are more in line with real world values. --- tests/risk/test_risk_cumulative.py | 16 ++++++++++++++++ tests/test_perf_tracking.py | 4 ++-- zipline/finance/risk/cumulative.py | 10 +++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/risk/test_risk_cumulative.py b/tests/risk/test_risk_cumulative.py index 6fcf96ab..7dd3f44e 100644 --- a/tests/risk/test_risk_cumulative.py +++ b/tests/risk/test_risk_cumulative.py @@ -94,3 +94,19 @@ class TestRisk(unittest.TestCase): value, decimal=2, err_msg="Mismatch at %s" % (dt,)) + + def test_alpha_06(self): + for dt, value in answer_key.RISK_CUMULATIVE.alpha.iterkv(): + np.testing.assert_almost_equal( + self.cumulative_metrics_06.metrics.alpha[dt], + value, + decimal=2, + err_msg="Mismatch at %s" % (dt,)) + + def test_beta_06(self): + for dt, value in answer_key.RISK_CUMULATIVE.beta.iterkv(): + np.testing.assert_almost_equal( + self.cumulative_metrics_06.metrics.beta[dt], + value, + decimal=2, + err_msg="Mismatch at %s" % (dt,)) diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index 6a62a184..ae59e1ed 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -1208,7 +1208,7 @@ class TestPerformanceTracker(unittest.TestCase): commission=0.50) benchmark_event_1 = Event({ 'dt': start_dt, - 'returns': 1.0, + 'returns': 0.01, 'type': DATASOURCE_TYPE.BENCHMARK }) @@ -1218,7 +1218,7 @@ class TestPerformanceTracker(unittest.TestCase): 'bar', 11.0, 20, start_dt + datetime.timedelta(minutes=1)) benchmark_event_2 = Event({ 'dt': start_dt + datetime.timedelta(minutes=1), - 'returns': 2.0, + 'returns': 0.02, 'type': DATASOURCE_TYPE.BENCHMARK }) diff --git a/zipline/finance/risk/cumulative.py b/zipline/finance/risk/cumulative.py index be59898e..7a9376fc 100644 --- a/zipline/finance/risk/cumulative.py +++ b/zipline/finance/risk/cumulative.py @@ -463,9 +463,9 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" """ http://en.wikipedia.org/wiki/Alpha_(investment) """ - return alpha(self.algorithm_period_returns[self.latest_dt], + return alpha(self.annualized_mean_returns[self.latest_dt], self.treasury_period_return, - self.benchmark_period_returns[self.latest_dt], + self.annualized_benchmark_returns[self.latest_dt], self.metrics.beta[dt]) def calculate_volatility(self, daily_returns): @@ -488,11 +488,11 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" """ # it doesn't make much sense to calculate beta for less than two days, # so return none. - if len(self.algorithm_returns) < 2: + if len(self.annualized_mean_returns) < 2: return 0.0 - returns_matrix = np.vstack([self.algorithm_returns, - self.benchmark_returns]) + returns_matrix = np.vstack([self.annualized_mean_returns, + self.annualized_benchmark_returns]) C = np.cov(returns_matrix, ddof=1) algorithm_covariance = C[0][1] benchmark_variance = C[1][1]