From 7c01d3985805298d58a4239e3a0a70ec1d01ae72 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Tue, 23 Jul 2013 12:07:24 -0400 Subject: [PATCH] BUG: Fix beta calculation. Use recent change to benchmark variance in the beta calculation, instead of referring to the 4th quadrant of the covariance. Also, read answers from answer key for corroboration of beta values. --- tests/risk/answer_key.py | 7 +++++ tests/risk/test_risk.py | 65 ++++++++++++++++------------------------ zipline/finance/risk.py | 2 +- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/tests/risk/answer_key.py b/tests/risk/answer_key.py index 22ddbd55..1cc4bfe3 100644 --- a/tests/risk/answer_key.py +++ b/tests/risk/answer_key.py @@ -166,6 +166,13 @@ class AnswerKey(object): 'year': DataIndex('Sim', 'AG', 34, 34), } + ALGORITHM_PERIOD_BETA = { + 'Monthly': DataIndex('Sim', 'AH', 23, 34), + '3-Month': DataIndex('Sim', 'AI', 25, 34), + '6-month': DataIndex('Sim', 'AJ', 28, 34), + 'year': DataIndex('Sim', 'AK', 34, 34), + } + ALGORITHM_PERIOD_BENCHMARK_VARIANCE = { 'Monthly': DataIndex('Sim', 'BB', 23, 34), '3-Month': DataIndex('Sim', 'BC', 25, 34), diff --git a/tests/risk/test_risk.py b/tests/risk/test_risk.py index 56760447..ff01a118 100644 --- a/tests/risk/test_risk.py +++ b/tests/risk/test_risk.py @@ -332,49 +332,36 @@ class TestRisk(unittest.TestCase): for x in self.metrics_06.year_periods], [-0.001]) - def dtest_algorithm_beta_06(self): - self.assertEqual([round(x.beta, 3) + def test_algorithm_beta_06(self): + answer_key_month_periods = ANSWER_KEY.get_values( + AnswerKey.ALGORITHM_PERIOD_BETA['Monthly'], + decimal=7) + self.assertEqual([np.round(x.beta, 7) for x in self.metrics_06.month_periods], - [0.553, - 0.583, - -2.168, - -0.548, - 1.463, - -0.322, - -1.38, - 1.473, - -1.315, - -0.7, - 0.352, - -2.002]) + answer_key_month_periods) - self.assertEqual([round(x.beta, 3) + answer_key_three_month_periods = ANSWER_KEY.get_values( + AnswerKey.ALGORITHM_PERIOD_BETA['3-Month'], + decimal=7) + self.assertEqual([np.round(x.beta, 7) for x in self.metrics_06.three_month_periods], - [-0.075, - -0.637, - 0.124, - 0.186, - -0.204, - -0.497, - -0.867, - -0.173, - -0.499, - -0.563]) + answer_key_three_month_periods) - self.assertEqual([round(x.beta, 3) - for x in self.metrics_06.six_month_periods], - [-0.075, - -0.637, - 0.124, - 0.186, - -0.204, - -0.497, - -0.867, - -0.173, - -0.499, - -0.563]) - self.assertEqual([round(x.beta, 3) - for x in self.metrics_06.year_periods], [-0.219]) + answer_key_six_month_periods = ANSWER_KEY.get_values( + AnswerKey.ALGORITHM_PERIOD_BETA['6-month'], + decimal=7) + results_six_month_periods = [ + np.round(x.beta, 7) + for x in self.metrics_06.six_month_periods] + self.assertEqual(results_six_month_periods, + answer_key_six_month_periods) + + answer_key_year_periods = ANSWER_KEY.get_values( + AnswerKey.ALGORITHM_PERIOD_BETA['year'], + decimal=7) + self.assertEqual([np.round(x.beta, 7) + for x in self.metrics_06.year_periods], + answer_key_year_periods) def dtest_algorithm_alpha_06(self): self.assertEqual([round(x.alpha, 3) diff --git a/zipline/finance/risk.py b/zipline/finance/risk.py index fd246704..f2ea6edd 100644 --- a/zipline/finance/risk.py +++ b/zipline/finance/risk.py @@ -487,7 +487,7 @@ class RiskMetricsBase(object): condition_number = max(eigen_values) / min(eigen_values) algorithm_covariance = C[0][1] benchmark_variance = np.var(self.benchmark_returns, ddof=1) - beta = C[0][1] / C[1][1] + beta = algorithm_covariance / benchmark_variance return ( beta,