From 3732c105b8687c1b6f6ccd56ad63cf6867faaf1d Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Wed, 14 Aug 2013 15:29:54 -0400 Subject: [PATCH] TST: Improve answer key interface. Instead of using the indexes defined in the answer key class to index back into the answer key object, populate the answers so that they are available as members of the answer key object. Update period risk test to use new answer key structure. Also, remove the rounding behavior from the answer sheet, leaving the rounding to the consumer of the answer key values, so that the values can be retrieved from the spreadsheet during answer key __init__ without knowledge of the decimal point that the calling code expects. Correspondingly, change period risk tests to use np.testing.assert_almost_equal when doing floating point comparison. --- tests/risk/answer_key.py | 133 ++++++------ tests/risk/test_risk_period.py | 384 ++++++++++++--------------------- 2 files changed, 204 insertions(+), 313 deletions(-) diff --git a/tests/risk/answer_key.py b/tests/risk/answer_key.py index 8be0a766..d38f15e5 100644 --- a/tests/risk/answer_key.py +++ b/tests/risk/answer_key.py @@ -16,7 +16,6 @@ import hashlib import os -import numpy as np import xlrd import requests @@ -147,70 +146,72 @@ class DataIndex(object): class AnswerKey(object): - RETURNS = DataIndex('Sim Period', 'D', 4, 255) + INDEXES = { + 'RETURNS': DataIndex('Sim Period', 'D', 4, 255), - # Below matches the inconsistent capitalization in spreadsheet - BENCHMARK_PERIOD_RETURNS = { - 'Monthly': DataIndex('s_p', 'P', 8, 19), - '3-Month': DataIndex('s_p', 'Q', 10, 19), - '6-month': DataIndex('s_p', 'R', 13, 19), - 'year': DataIndex('s_p', 'S', 19, 19), - } + # Below matches the inconsistent capitalization in spreadsheet + 'BENCHMARK_PERIOD_RETURNS': { + 'Monthly': DataIndex('s_p', 'P', 8, 19), + '3-Month': DataIndex('s_p', 'Q', 10, 19), + '6-month': DataIndex('s_p', 'R', 13, 19), + 'year': DataIndex('s_p', 'S', 19, 19), + }, - BENCHMARK_PERIOD_VOLATILITY = { - 'Monthly': DataIndex('s_p', 'T', 8, 19), - '3-Month': DataIndex('s_p', 'U', 10, 19), - '6-month': DataIndex('s_p', 'V', 13, 19), - 'year': DataIndex('s_p', 'W', 19, 19), - } + 'BENCHMARK_PERIOD_VOLATILITY': { + 'Monthly': DataIndex('s_p', 'T', 8, 19), + '3-Month': DataIndex('s_p', 'U', 10, 19), + '6-month': DataIndex('s_p', 'V', 13, 19), + 'year': DataIndex('s_p', 'W', 19, 19), + }, - ALGORITHM_PERIOD_RETURNS = { - 'Monthly': DataIndex('Sim Period', 'V', 23, 34), - '3-Month': DataIndex('Sim Period', 'W', 25, 34), - '6-month': DataIndex('Sim Period', 'X', 28, 34), - 'year': DataIndex('Sim Period', 'Y', 34, 34), - } + 'ALGORITHM_PERIOD_RETURNS': { + 'Monthly': DataIndex('Sim Period', 'V', 23, 34), + '3-Month': DataIndex('Sim Period', 'W', 25, 34), + '6-month': DataIndex('Sim Period', 'X', 28, 34), + 'year': DataIndex('Sim Period', 'Y', 34, 34), + }, - ALGORITHM_PERIOD_VOLATILITY = { - 'Monthly': DataIndex('Sim Period', 'Z', 23, 34), - '3-Month': DataIndex('Sim Period', 'AA', 25, 34), - '6-month': DataIndex('Sim Period', 'AB', 28, 34), - 'year': DataIndex('Sim Period', 'AC', 34, 34), - } + 'ALGORITHM_PERIOD_VOLATILITY': { + 'Monthly': DataIndex('Sim Period', 'Z', 23, 34), + '3-Month': DataIndex('Sim Period', 'AA', 25, 34), + '6-month': DataIndex('Sim Period', 'AB', 28, 34), + 'year': DataIndex('Sim Period', 'AC', 34, 34), + }, - ALGORITHM_PERIOD_SHARPE = { - 'Monthly': DataIndex('Sim Period', 'AD', 23, 34), - '3-Month': DataIndex('Sim Period', 'AE', 25, 34), - '6-month': DataIndex('Sim Period', 'AF', 28, 34), - 'year': DataIndex('Sim Period', 'AG', 34, 34), - } + 'ALGORITHM_PERIOD_SHARPE': { + 'Monthly': DataIndex('Sim Period', 'AD', 23, 34), + '3-Month': DataIndex('Sim Period', 'AE', 25, 34), + '6-month': DataIndex('Sim Period', 'AF', 28, 34), + 'year': DataIndex('Sim Period', 'AG', 34, 34), + }, - ALGORITHM_PERIOD_BETA = { - 'Monthly': DataIndex('Sim Period', 'AH', 23, 34), - '3-Month': DataIndex('Sim Period', 'AI', 25, 34), - '6-month': DataIndex('Sim Period', 'AJ', 28, 34), - 'year': DataIndex('Sim Period', 'AK', 34, 34), - } + 'ALGORITHM_PERIOD_BETA': { + 'Monthly': DataIndex('Sim Period', 'AH', 23, 34), + '3-Month': DataIndex('Sim Period', 'AI', 25, 34), + '6-month': DataIndex('Sim Period', 'AJ', 28, 34), + 'year': DataIndex('Sim Period', 'AK', 34, 34), + }, - ALGORITHM_PERIOD_ALPHA = { - 'Monthly': DataIndex('Sim Period', 'AL', 23, 34), - '3-Month': DataIndex('Sim Period', 'AM', 25, 34), - '6-month': DataIndex('Sim Period', 'AN', 28, 34), - 'year': DataIndex('Sim Period', 'AO', 34, 34), - } + 'ALGORITHM_PERIOD_ALPHA': { + 'Monthly': DataIndex('Sim Period', 'AL', 23, 34), + '3-Month': DataIndex('Sim Period', 'AM', 25, 34), + '6-month': DataIndex('Sim Period', 'AN', 28, 34), + 'year': DataIndex('Sim Period', 'AO', 34, 34), + }, - ALGORITHM_PERIOD_BENCHMARK_VARIANCE = { - 'Monthly': DataIndex('Sim Period', 'BB', 23, 34), - '3-Month': DataIndex('Sim Period', 'BC', 25, 34), - '6-month': DataIndex('Sim Period', 'BD', 28, 34), - 'year': DataIndex('Sim Period', 'BE', 34, 34), - } + 'ALGORITHM_PERIOD_BENCHMARK_VARIANCE': { + 'Monthly': DataIndex('Sim Period', 'BB', 23, 34), + '3-Month': DataIndex('Sim Period', 'BC', 25, 34), + '6-month': DataIndex('Sim Period', 'BD', 28, 34), + 'year': DataIndex('Sim Period', 'BE', 34, 34), + }, - ALGORITHM_PERIOD_COVARIANCE = { - 'Monthly': DataIndex('Sim Period', 'AX', 23, 34), - '3-Month': DataIndex('Sim Period', 'AY', 25, 34), - '6-month': DataIndex('Sim Period', 'AZ', 28, 34), - 'year': DataIndex('Sim Period', 'BA', 34, 34), + 'ALGORITHM_PERIOD_COVARIANCE': { + 'Monthly': DataIndex('Sim Period', 'AX', 23, 34), + '3-Month': DataIndex('Sim Period', 'AY', 25, 34), + '6-month': DataIndex('Sim Period', 'AZ', 28, 34), + 'year': DataIndex('Sim Period', 'BA', 34, 34), + } } def __init__(self): @@ -220,9 +221,17 @@ class AnswerKey(object): self.sheets['Sim Period'] = self.workbook.sheet_by_name('Sim Period') self.sheets['s_p'] = self.workbook.sheet_by_name('s_p') - def get_values(self, data_index, decimal=4): - return [np.round(x, decimal) for x in - self.sheets[data_index.sheet_name].col_values( - data_index.col_index, - data_index.row_start_index, - data_index.row_end_index + 1)] + for name, index in self.INDEXES.items(): + if isinstance(index, dict): + subvalues = {} + for subkey, subindex in index.items(): + subvalues[subkey] = self.get_values(subindex) + setattr(self, name, subvalues) + else: + setattr(self, name, self.get_values(index)) + + def get_values(self, data_index): + return self.sheets[data_index.sheet_name].col_values( + data_index.col_index, + data_index.row_start_index, + data_index.row_end_index + 1) diff --git a/tests/risk/test_risk_period.py b/tests/risk/test_risk_period.py index b850ec64..5ea9c392 100644 --- a/tests/risk/test_risk_period.py +++ b/tests/risk/test_risk_period.py @@ -27,7 +27,7 @@ from . answer_key import AnswerKey ANSWER_KEY = AnswerKey() -RETURNS = ANSWER_KEY.get_values(AnswerKey.RETURNS) +RETURNS = ANSWER_KEY.RETURNS class TestRisk(unittest.TestCase): @@ -100,26 +100,22 @@ class TestRisk(unittest.TestCase): def test_benchmark_returns_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params) - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_RETURNS['Monthly']) - self.assertEqual([round(x.benchmark_period_returns, 4) - for x in metrics.month_periods], - answer_key_month_periods) - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_RETURNS['3-Month']) - self.assertEqual([round(x.benchmark_period_returns, 4) - for x in metrics.three_month_periods], - answer_key_three_month_periods) - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_RETURNS['6-month']) - self.assertEqual([round(x.benchmark_period_returns, 4) - for x in metrics.six_month_periods], - answer_key_six_month_periods) - answer_key_year_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_RETURNS['year']) - self.assertEqual([round(x.benchmark_period_returns, 4) - for x in metrics.year_periods], - answer_key_year_periods) + np.testing.assert_almost_equal( + [x.benchmark_period_returns + for x in metrics.month_periods], + ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['Monthly']) + np.testing.assert_almost_equal( + [x.benchmark_period_returns + for x in metrics.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], + ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['6-month']) + np.testing.assert_almost_equal( + [x.benchmark_period_returns + for x in metrics.year_periods], + ANSWER_KEY.BENCHMARK_PERIOD_RETURNS['year']) def test_trading_days_06(self): returns = factory.create_returns_from_range(self.sim_params) @@ -132,125 +128,72 @@ class TestRisk(unittest.TestCase): def test_benchmark_volatility_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params) - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_VOLATILITY['Monthly'], - decimal=3) - self.assertEqual([np.round(x.benchmark_volatility, 3) - for x in metrics.month_periods], - answer_key_month_periods) - - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_VOLATILITY['3-Month'], - decimal=3) - self.assertEqual([np.round(x.benchmark_volatility, 3) - for x in metrics.three_month_periods], - answer_key_three_month_periods) - - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_VOLATILITY['6-month'], - decimal=3) - self.assertEqual([np.round(x.benchmark_volatility, 3) - for x in metrics.six_month_periods], - answer_key_six_month_periods) - - answer_key_year_periods = ANSWER_KEY.get_values( - AnswerKey.BENCHMARK_PERIOD_VOLATILITY['year'], - decimal=3) - self.assertEqual([np.round(x.benchmark_volatility, 3) - for x in metrics.year_periods], - answer_key_year_periods) + np.testing.assert_almost_equal( + [x.benchmark_volatility + for x in metrics.month_periods], + ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['Monthly']) + np.testing.assert_almost_equal( + [x.benchmark_volatility + for x in metrics.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], + ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['6-month']) + np.testing.assert_almost_equal( + [x.benchmark_volatility + for x in metrics.year_periods], + ANSWER_KEY.BENCHMARK_PERIOD_VOLATILITY['year']) def test_algorithm_returns_06(self): - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_RETURNS['Monthly'], - decimal=3) - self.assertEqual([np.round(x.algorithm_period_returns, 3) - for x in self.metrics_06.month_periods], - answer_key_month_periods) - - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_RETURNS['3-Month'], - decimal=3) - self.assertEqual([np.round(x.algorithm_period_returns, 3) - for x in self.metrics_06.three_month_periods], - answer_key_three_month_periods) - - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_RETURNS['6-month'], - decimal=3) - self.assertEqual([np.round(x.algorithm_period_returns, 3) - for x in self.metrics_06.six_month_periods], - answer_key_six_month_periods) - - answer_key_year_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_RETURNS['year'], - decimal=3) - self.assertEqual([np.round(x.algorithm_period_returns, 3) - for x in self.metrics_06.year_periods], - answer_key_year_periods) + np.testing.assert_almost_equal( + [x.algorithm_period_returns + for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_RETURNS['Monthly']) + np.testing.assert_almost_equal( + [x.algorithm_period_returns + for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_RETURNS['3-Month']) + np.testing.assert_almost_equal( + [x.algorithm_period_returns + for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_RETURNS['6-month']) + np.testing.assert_almost_equal( + [x.algorithm_period_returns + for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_RETURNS['year']) def test_algorithm_volatility_06(self): - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_VOLATILITY['Monthly'], - decimal=3) - self.assertEqual([np.round(x.algorithm_volatility, 3) - for x in self.metrics_06.month_periods], - answer_key_month_periods) + np.testing.assert_almost_equal( + [x.algorithm_volatility + for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_VOLATILITY['Monthly']) + np.testing.assert_almost_equal( + [x.algorithm_volatility + for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_VOLATILITY['3-Month']) + np.testing.assert_almost_equal( + [x.algorithm_volatility + for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_VOLATILITY['6-month']) + np.testing.assert_almost_equal( + [x.algorithm_volatility + for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_VOLATILITY['year']) - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_VOLATILITY['3-Month'], - decimal=3) - self.assertEqual([np.round(x.algorithm_volatility, 3) - for x in self.metrics_06.three_month_periods], - answer_key_three_month_periods) - - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_VOLATILITY['6-month'], - decimal=3) - self.assertEqual([np.round(x.algorithm_volatility, 3) - for x in self.metrics_06.six_month_periods], - answer_key_six_month_periods) - - answer_key_year_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_VOLATILITY['year'], - decimal=3) - self.assertEqual([np.round(x.algorithm_volatility, 3) - for x in self.metrics_06.year_periods], - answer_key_year_periods) - - def test_algorithm_sharpe_06_monthly(self): - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_SHARPE['Monthly'], - decimal=3) - self.assertEqual([np.round(x.sharpe, 3) - for x in self.metrics_06.month_periods], - answer_key_month_periods) - - def test_algorithm_sharpe_06_three_month(self): - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_SHARPE['3-Month'], - decimal=3) - self.assertEqual([np.round(x.sharpe, 3) - for x in self.metrics_06.three_month_periods], - answer_key_three_month_periods) - - def test_algorithm_sharpe_06_six_month(self): - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_SHARPE['6-month'], - decimal=3) - results_six_month_periods = [ - np.round(x.sharpe, 3) - for x in self.metrics_06.six_month_periods] - self.assertEqual(results_six_month_periods, - answer_key_six_month_periods) - - def test_algorithm_sharpe_06_year(self): - answer_key_year_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_SHARPE['year'], - decimal=3) - self.assertEqual([np.round(x.sharpe, 3) - for x in self.metrics_06.year_periods], - answer_key_year_periods) + def test_algorithm_sharpe_06(self): + np.testing.assert_almost_equal( + [x.sharpe for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_SHARPE['Monthly']) + np.testing.assert_almost_equal( + [x.sharpe for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_SHARPE['3-Month']) + np.testing.assert_almost_equal( + [x.sharpe for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_SHARPE['6-month']) + np.testing.assert_almost_equal( + [x.sharpe for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_SHARPE['year']) def test_algorithm_sortino_06(self): self.assertEqual([round(x.sortino, 3) @@ -333,66 +276,32 @@ class TestRisk(unittest.TestCase): [-0.001]) 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], - answer_key_month_periods) - - 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], - answer_key_three_month_periods) - - 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) + np.testing.assert_almost_equal( + [x.beta for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BETA['Monthly']) + np.testing.assert_almost_equal( + [x.beta for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BETA['3-Month']) + np.testing.assert_almost_equal( + [x.beta for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BETA['6-month']) + np.testing.assert_almost_equal( + [x.beta for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BETA['year']) def test_algorithm_alpha_06(self): - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_ALPHA['Monthly'], - decimal=7) - self.assertEqual([np.round(x.alpha, 7) - for x in self.metrics_06.month_periods], - answer_key_month_periods) - - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_ALPHA['3-Month'], - decimal=7) - self.assertEqual([np.round(x.alpha, 7) - for x in self.metrics_06.three_month_periods], - answer_key_three_month_periods) - - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_ALPHA['6-month'], - decimal=7) - results_six_month_periods = [ - np.round(x.alpha, 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_ALPHA['year'], - decimal=7) - self.assertEqual([np.round(x.alpha, 7) - for x in self.metrics_06.year_periods], - answer_key_year_periods) + np.testing.assert_almost_equal( + [x.alpha for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_ALPHA['Monthly']) + np.testing.assert_almost_equal( + [x.alpha for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_ALPHA['3-Month']) + np.testing.assert_almost_equal( + [x.alpha for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_ALPHA['6-month']) + np.testing.assert_almost_equal( + [x.alpha for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_ALPHA['year']) # FIXME: Covariance is not matching excel precisely enough to run the test. # Month 4 seems to be the problem. Variance is disabled @@ -400,66 +309,39 @@ class TestRisk(unittest.TestCase): # and can probably pass with 6 significant digits instead of 7. # re-enable variance, alpha, and beta tests once this is resolved def test_algorithm_covariance_06(self): - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_COVARIANCE['Monthly'], - decimal=7) - self.assertEqual([np.round(x.algorithm_covariance, 7) - for x in self.metrics_06.month_periods], - answer_key_month_periods) - - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_COVARIANCE['3-Month'], - decimal=7) - self.assertEqual([np.round(x.algorithm_covariance, 7) - for x in self.metrics_06.three_month_periods], - answer_key_three_month_periods) - - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_COVARIANCE['6-month'], - decimal=7) - results_six_month_periods = [ - np.round(x.algorithm_covariance, 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_COVARIANCE['year'], - decimal=7) - self.assertEqual([np.round(x.algorithm_covariance, 7) - for x in self.metrics_06.year_periods], - answer_key_year_periods) + np.testing.assert_almost_equal( + [x.algorithm_covariance for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_COVARIANCE['Monthly']) + np.testing.assert_almost_equal( + [x.algorithm_covariance + for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_COVARIANCE['3-Month']) + np.testing.assert_almost_equal( + [x.algorithm_covariance + for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_COVARIANCE['6-month']) + np.testing.assert_almost_equal( + [x.algorithm_covariance + for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_COVARIANCE['year']) def test_benchmark_variance_06(self): - answer_key_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['Monthly'], - decimal=7) - self.assertEqual([np.round(x.benchmark_variance, 7) - for x in self.metrics_06.month_periods], - answer_key_month_periods) - - answer_key_three_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['3-Month'], - decimal=7) - self.assertEqual([np.round(x.benchmark_variance, 7) - for x in self.metrics_06.three_month_periods], - answer_key_three_month_periods) - - answer_key_six_month_periods = ANSWER_KEY.get_values( - AnswerKey.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['6-month'], - decimal=7) - results_six_month_periods = [ - np.round(x.benchmark_variance, 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_BENCHMARK_VARIANCE['year'], - decimal=7) - self.assertEqual([np.round(x.benchmark_variance, 7) - for x in self.metrics_06.year_periods], - answer_key_year_periods) + np.testing.assert_almost_equal( + [x.benchmark_variance + for x in self.metrics_06.month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['Monthly']) + np.testing.assert_almost_equal( + [x.benchmark_variance + for x in self.metrics_06.three_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['3-Month']) + np.testing.assert_almost_equal( + [x.benchmark_variance + for x in self.metrics_06.six_month_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['6-month']) + np.testing.assert_almost_equal( + [x.benchmark_variance + for x in self.metrics_06.year_periods], + ANSWER_KEY.ALGORITHM_PERIOD_BENCHMARK_VARIANCE['year']) def test_benchmark_returns_08(self): returns = factory.create_returns_from_range(self.sim_params08)