From 618d554da10a7f26585daf23735e49a8d57f5a06 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Wed, 9 Apr 2014 15:39:05 -0400 Subject: [PATCH] TST: Use benchmark returns from spreadsheet. The risk unit tests were using the public Yahoo! data instead of the returns from the answer key spreadsheet, change the RiskPeriod's created in tests to use the values in the benchmark returns column of the answer key. Also, change the spreadsheet's benchmark volatility calculation to use sample. The use of population was exposed when the input values were corrected. --- tests/risk/risk-answer-key-checksums | 1 + tests/risk/test_risk_period.py | 29 +++++++++++++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tests/risk/risk-answer-key-checksums b/tests/risk/risk-answer-key-checksums index 61a76cda..ccae28b7 100644 --- a/tests/risk/risk-answer-key-checksums +++ b/tests/risk/risk-answer-key-checksums @@ -12,3 +12,4 @@ cc507b6fca18aabadac69657181edd4e 37e3ea4a1788f1aa6f3ee0986bc625ae 651e611e723e2a58b1ded91d0cd39b66 d62fce39ec78f032165d8f356bba5c2c +97632f6f64dfc4a2de09882419a79421 diff --git a/tests/risk/test_risk_period.py b/tests/risk/test_risk_period.py index b8a6a113..568c86a7 100644 --- a/tests/risk/test_risk_period.py +++ b/tests/risk/test_risk_period.py @@ -23,6 +23,7 @@ from zipline.utils import factory from zipline.finance.trading import SimulationParameters +import answer_key from . answer_key import AnswerKey ANSWER_KEY = AnswerKey() @@ -54,9 +55,13 @@ class TestRisk(unittest.TestCase): self.sim_params ) + self.benchmark_returns_06 = \ + answer_key.RETURNS_DATA['Benchmark Returns'] + self.metrics_06 = risk.RiskReport( self.algo_returns_06, - self.sim_params + self.sim_params, + benchmark_returns=self.benchmark_returns_06, ) start_08 = datetime.datetime( @@ -98,23 +103,22 @@ class TestRisk(unittest.TestCase): self.assertEqual(metrics.max_drawdown, 0.505) def test_benchmark_returns_06(self): - returns = factory.create_returns_from_range(self.sim_params) - metrics = risk.RiskReport(returns, self.sim_params) + np.testing.assert_almost_equal( [x.benchmark_period_returns - for x in metrics.month_periods], + for x in self.metrics_06.month_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['Monthly']) np.testing.assert_almost_equal( [x.benchmark_period_returns - for x in metrics.three_month_periods], + for x in self.metrics_06.three_month_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['3-Month']) np.testing.assert_almost_equal( [x.benchmark_period_returns - for x in metrics.six_month_periods], + for x in self.metrics_06.six_month_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['6-month']) np.testing.assert_almost_equal( [x.benchmark_period_returns - for x in metrics.year_periods], + for x in self.metrics_06.year_periods], ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['year']) def test_trading_days_06(self): @@ -126,23 +130,22 @@ class TestRisk(unittest.TestCase): [20, 19, 23, 19, 22, 22, 20, 23, 20, 22, 21, 20]) def test_benchmark_volatility_06(self): - returns = factory.create_returns_from_range(self.sim_params) - metrics = risk.RiskReport(returns, self.sim_params) + np.testing.assert_almost_equal( [x.benchmark_volatility - for x in metrics.month_periods], + for x in self.metrics_06.month_periods], ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['Monthly']) np.testing.assert_almost_equal( [x.benchmark_volatility - for x in metrics.three_month_periods], + for x in self.metrics_06.three_month_periods], ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['3-Month']) np.testing.assert_almost_equal( [x.benchmark_volatility - for x in metrics.six_month_periods], + for x in self.metrics_06.six_month_periods], ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['6-month']) np.testing.assert_almost_equal( [x.benchmark_volatility - for x in metrics.year_periods], + for x in self.metrics_06.year_periods], ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['year']) def test_algorithm_returns_06(self):