<s> import unittest <EOL> import datetime <EOL> import calendar <EOL> import numpy as np <EOL> import pytz <EOL> from itertools import chain <EOL> from six import itervalues <EOL> import zipline . finance . risk as risk <EOL> from zipline . utils import factory <EOL> from zipline . finance . trading import SimulationParameters , TradingEnvironment <EOL> from . import answer_key <EOL> from . answer_key import AnswerKey <EOL> ANSWER_KEY = AnswerKey ( ) <EOL> RETURNS = ANSWER_KEY . RETURNS <EOL> class TestRisk ( unittest . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . env = TradingEnvironment ( ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> del cls . env <EOL> def setUp ( self ) : <EOL> start_date = datetime . datetime ( <EOL> year = <NUM_LIT> , <EOL> month = <NUM_LIT:1> , <EOL> day = <NUM_LIT:1> , <EOL> hour = <NUM_LIT:0> , <EOL> minute = <NUM_LIT:0> , <EOL> tzinfo = pytz . utc ) <EOL> end_date = datetime . datetime ( <EOL> year = <NUM_LIT> , month = <NUM_LIT:12> , day = <NUM_LIT> , tzinfo = pytz . utc ) <EOL> self . sim_params = SimulationParameters ( <EOL> period_start = start_date , <EOL> period_end = end_date , <EOL> env = self . env , <EOL> ) <EOL> self . algo_returns_06 = factory . create_returns_from_list ( <EOL> RETURNS , <EOL> self . sim_params <EOL> ) <EOL> self . benchmark_returns_06 = answer_key . RETURNS_DATA [ '<STR_LIT>' ] <EOL> self . metrics_06 = risk . RiskReport ( <EOL> self . algo_returns_06 , <EOL> self . sim_params , <EOL> benchmark_returns = self . benchmark_returns_06 , <EOL> env = self . env , <EOL> ) <EOL> start_08 = datetime . datetime ( <EOL> year = <NUM_LIT> , <EOL> month = <NUM_LIT:1> , <EOL> day = <NUM_LIT:1> , <EOL> hour = <NUM_LIT:0> , <EOL> minute = <NUM_LIT:0> , <EOL> tzinfo = pytz . utc ) <EOL> end_08 = datetime . datetime ( <EOL> year = <NUM_LIT> , <EOL> month = <NUM_LIT:12> , <EOL> day = <NUM_LIT> , <EOL> tzinfo = pytz . utc <EOL> ) <EOL> self . sim_params08 = SimulationParameters ( <EOL> period_start = start_08 , <EOL> period_end = end_08 , <EOL> env = self . env , <EOL> ) <EOL> def tearDown ( self ) : <EOL> return <EOL> def test_factory ( self ) : <EOL> returns = [ <NUM_LIT:0.1> ] * <NUM_LIT:100> <EOL> r_objects = factory . create_returns_from_list ( returns , self . sim_params ) <EOL> self . assertTrue ( r_objects . index [ - <NUM_LIT:1> ] <= <EOL> datetime . datetime ( <EOL> year = <NUM_LIT> , month = <NUM_LIT:12> , day = <NUM_LIT> , tzinfo = pytz . utc ) ) <EOL> def test_drawdown ( self ) : <EOL> returns = factory . create_returns_from_list ( <EOL> [ <NUM_LIT:1.0> , - <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> , - <NUM_LIT:0.1> , - <NUM_LIT> ] , self . sim_params ) <EOL> metrics = risk . RiskMetricsPeriod ( <EOL> returns . index [ <NUM_LIT:0> ] , <EOL> returns . index [ - <NUM_LIT:1> ] , <EOL> returns , <EOL> env = self . env , <EOL> benchmark_returns = self . env . benchmark_returns , <EOL> ) <EOL> self . assertEqual ( metrics . max_drawdown , <NUM_LIT> ) <EOL> def test_benchmark_returns_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_period_returns <EOL> for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_period_returns <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_period_returns <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_period_returns <EOL> for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> def test_trading_days_06 ( self ) : <EOL> returns = factory . create_returns_from_range ( self . sim_params ) <EOL> metrics = risk . RiskReport ( returns , self . sim_params , env = self . env ) <EOL> self . assertEqual ( [ x . num_trading_days for x in metrics . year_periods ] , <EOL> [ <NUM_LIT> ] ) <EOL> self . assertEqual ( [ x . num_trading_days for x in metrics . month_periods ] , <EOL> [ <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:20> ] ) <EOL> def test_benchmark_volatility_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_volatility <EOL> for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_volatility <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_volatility <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_volatility <EOL> for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . BENCHMARK_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> def test_algorithm_returns_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_period_returns <EOL> for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_period_returns <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_period_returns <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_period_returns <EOL> for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_RETURNS [ '<STR_LIT>' ] ) <EOL> def test_algorithm_volatility_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_volatility <EOL> for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_volatility <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_volatility <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_volatility <EOL> for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_VOLATILITY [ '<STR_LIT>' ] ) <EOL> def test_algorithm_sharpe_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sharpe for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SHARPE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sharpe for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SHARPE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sharpe for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SHARPE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sharpe for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SHARPE [ '<STR_LIT>' ] ) <EOL> def test_algorithm_downside_risk_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . downside_risk for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_DOWNSIDE_RISK [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:4> ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . downside_risk for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_DOWNSIDE_RISK [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:4> ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . downside_risk for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_DOWNSIDE_RISK [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:4> ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . downside_risk for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_DOWNSIDE_RISK [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:4> ) <EOL> def test_algorithm_sortino_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sortino for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SORTINO [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:3> ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sortino for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SORTINO [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:3> ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sortino for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SORTINO [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:3> ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . sortino for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_SORTINO [ '<STR_LIT>' ] , <EOL> decimal = <NUM_LIT:3> ) <EOL> def test_algorithm_information_06 ( self ) : <EOL> self . assertEqual ( [ round ( x . information , <NUM_LIT:3> ) <EOL> for x in self . metrics_06 . month_periods ] , <EOL> [ <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . information , <NUM_LIT:3> ) <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> [ - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . information , <NUM_LIT:3> ) <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> [ - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . information , <NUM_LIT:3> ) <EOL> for x in self . metrics_06 . year_periods ] , <EOL> [ - <NUM_LIT> ] ) <EOL> def test_algorithm_beta_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . beta for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BETA [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . beta for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BETA [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . beta for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BETA [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . beta for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BETA [ '<STR_LIT>' ] ) <EOL> def test_algorithm_alpha_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . alpha for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_ALPHA [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . alpha for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_ALPHA [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . alpha for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_ALPHA [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . alpha for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_ALPHA [ '<STR_LIT>' ] ) <EOL> def test_algorithm_covariance_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_covariance for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_COVARIANCE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_covariance <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_COVARIANCE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_covariance <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_COVARIANCE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . algorithm_covariance <EOL> for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_COVARIANCE [ '<STR_LIT>' ] ) <EOL> def test_benchmark_variance_06 ( self ) : <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_variance <EOL> for x in self . metrics_06 . month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BENCHMARK_VARIANCE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_variance <EOL> for x in self . metrics_06 . three_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BENCHMARK_VARIANCE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_variance <EOL> for x in self . metrics_06 . six_month_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BENCHMARK_VARIANCE [ '<STR_LIT>' ] ) <EOL> np . testing . assert_almost_equal ( <EOL> [ x . benchmark_variance <EOL> for x in self . metrics_06 . year_periods ] , <EOL> ANSWER_KEY . ALGORITHM_PERIOD_BENCHMARK_VARIANCE [ '<STR_LIT>' ] ) <EOL> def test_benchmark_returns_08 ( self ) : <EOL> returns = factory . create_returns_from_range ( self . sim_params08 ) <EOL> metrics = risk . RiskReport ( returns , self . sim_params08 , env = self . env ) <EOL> self . assertEqual ( [ round ( x . benchmark_period_returns , <NUM_LIT:3> ) <EOL> for x in metrics . month_periods ] , <EOL> [ - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . benchmark_period_returns , <NUM_LIT:3> ) <EOL> for x in metrics . three_month_periods ] , <EOL> [ - <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . benchmark_period_returns , <NUM_LIT:3> ) <EOL> for x in metrics . six_month_periods ] , <EOL> [ - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> , <EOL> - <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . benchmark_period_returns , <NUM_LIT:3> ) <EOL> for x in metrics . year_periods ] , <EOL> [ - <NUM_LIT> ] ) <EOL> def test_trading_days_08 ( self ) : <EOL> returns = factory . create_returns_from_range ( self . sim_params08 ) <EOL> metrics = risk . RiskReport ( returns , self . sim_params08 , env = self . env ) <EOL> self . assertEqual ( [ x . num_trading_days for x in metrics . year_periods ] , <EOL> [ <NUM_LIT> ] ) <EOL> self . assertEqual ( [ x . num_trading_days for x in metrics . month_periods ] , <EOL> [ <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> def test_benchmark_volatility_08 ( self ) : <EOL> returns = factory . create_returns_from_range ( self . sim_params08 ) <EOL> metrics = risk . RiskReport ( returns , self . sim_params08 , env = self . env ) <EOL> self . assertEqual ( [ round ( x . benchmark_volatility , <NUM_LIT:3> ) <EOL> for x in metrics . month_periods ] , <EOL> [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . benchmark_volatility , <NUM_LIT:3> ) <EOL> for x in metrics . three_month_periods ] , <EOL> [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . benchmark_volatility , <NUM_LIT:3> ) <EOL> for x in metrics . six_month_periods ] , <EOL> [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . benchmark_volatility , <NUM_LIT:3> ) <EOL> for x in metrics . year_periods ] , <EOL> [ <NUM_LIT> ] ) <EOL> def test_treasury_returns_06 ( self ) : <EOL> returns = factory . create_returns_from_range ( self . sim_params ) <EOL> metrics = risk . RiskReport ( returns , self . sim_params , env = self . env ) <EOL> self . assertEqual ( [ round ( x . treasury_period_return , <NUM_LIT:4> ) <EOL> for x in metrics . month_periods ] , <EOL> [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . treasury_period_return , <NUM_LIT:4> ) <EOL> for x in metrics . three_month_periods ] , <EOL> [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . treasury_period_return , <NUM_LIT:4> ) <EOL> for x in metrics . six_month_periods ] , <EOL> [ <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> self . assertEqual ( [ round ( x . treasury_period_return , <NUM_LIT:4> ) <EOL> for x in metrics . year_periods ] , <EOL> [ <NUM_LIT> ] ) <EOL> def test_benchmarkrange ( self ) : <EOL> self . check_year_range ( <EOL> datetime . datetime ( <EOL> year = <NUM_LIT> , month = <NUM_LIT:1> , day = <NUM_LIT:1> , tzinfo = pytz . utc ) , <EOL> <NUM_LIT:2> ) <EOL> def test_partial_month ( self ) : <EOL> start = datetime . datetime ( <EOL> year = <NUM_LIT> , <EOL> month = <NUM_LIT:1> , <EOL> day = <NUM_LIT:1> , <EOL> hour = <NUM_LIT:0> , <EOL> minute = <NUM_LIT:0> , <EOL> tzinfo = pytz . utc ) <EOL> total_days = <NUM_LIT> * <NUM_LIT:5> + <NUM_LIT:2> <EOL> end = start + datetime . timedelta ( days = total_days ) <EOL> sim_params90s = SimulationParameters ( <EOL> period_start = start , <EOL> period_end = end , <EOL> env = self . env , <EOL> ) <EOL> returns = factory . create_returns_from_range ( sim_params90s ) <EOL> returns = returns [ : - <NUM_LIT:10> ] <EOL> metrics = risk . RiskReport ( returns , sim_params90s , env = self . env ) <EOL> total_months = <NUM_LIT> <EOL> self . check_metrics ( metrics , total_months , start ) <EOL> def check_year_range ( self , start_date , years ) : <EOL> sim_params = SimulationParameters ( <EOL> period_start = start_date , <EOL> period_end = start_date . replace ( year = ( start_date . year + years ) ) , <EOL> env = self . env , <EOL> ) <EOL> returns = factory . create_returns_from_range ( sim_params ) <EOL> metrics = risk . RiskReport ( returns , self . sim_params , env = self . env ) <EOL> total_months = years * <NUM_LIT:12> <EOL> self . check_metrics ( metrics , total_months , start_date ) <EOL> def check_metrics ( self , metrics , total_months , start_date ) : <EOL> """<STR_LIT>""" <EOL> self . assert_range_length ( <EOL> metrics . month_periods , <EOL> total_months , <EOL> <NUM_LIT:1> , <EOL> start_date <EOL> ) <EOL> self . assert_range_length ( <EOL> metrics . three_month_periods , <EOL> total_months , <EOL> <NUM_LIT:3> , <EOL> start_date <EOL> ) <EOL> self . assert_range_length ( <EOL> metrics . six_month_periods , <EOL> total_months , <EOL> <NUM_LIT:6> , <EOL> start_date <EOL> ) <EOL> self . assert_range_length ( <EOL> metrics . year_periods , <EOL> total_months , <EOL> <NUM_LIT:12> , <EOL> start_date <EOL> ) <EOL> def assert_last_day ( self , period_end ) : <EOL> if period_end . month in [ <NUM_LIT:9> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:11> ] : <EOL> self . assertEqual ( period_end . day , <NUM_LIT:30> ) <EOL> elif ( period_end . month != <NUM_LIT:2> ) : <EOL> self . assertEqual ( period_end . day , <NUM_LIT> ) <EOL> else : <EOL> if calendar . isleap ( period_end . year ) : <EOL> self . assertEqual ( period_end . day , <NUM_LIT> ) <EOL> else : <EOL> self . assertEqual ( period_end . day , <NUM_LIT> ) <EOL> def assert_month ( self , start_month , actual_end_month ) : <EOL> if start_month == <NUM_LIT:1> : <EOL> expected_end_month = <NUM_LIT:12> <EOL> else : <EOL> expected_end_month = start_month - <NUM_LIT:1> <EOL> self . assertEqual ( expected_end_month , actual_end_month ) <EOL> def assert_range_length ( self , col , total_months , <EOL> period_length , start_date ) : <EOL> if ( period_length > total_months ) : <EOL> self . assertEqual ( len ( col ) , <NUM_LIT:0> ) <EOL> else : <EOL> self . assertEqual ( <EOL> len ( col ) , <EOL> total_months - ( period_length - <NUM_LIT:1> ) , <EOL> "<STR_LIT>" . format ( total_months = total_months , <EOL> period_length = period_length , <EOL> start_date = start_date , <EOL> end = col [ - <NUM_LIT:1> ] . end_date , <EOL> actual = len ( col ) ) <EOL> ) <EOL> self . assert_month ( start_date . month , col [ - <NUM_LIT:1> ] . end_date . month ) <EOL> self . assert_last_day ( col [ - <NUM_LIT:1> ] . end_date ) <EOL> def test_sparse_benchmark ( self ) : <EOL> benchmark_returns = self . benchmark_returns_06 . copy ( ) <EOL> benchmark_returns . iloc [ : : <NUM_LIT:2> ] = np . nan <EOL> report = risk . RiskReport ( <EOL> self . algo_returns_06 , <EOL> self . sim_params , <EOL> benchmark_returns = benchmark_returns , <EOL> env = self . env , <EOL> ) <EOL> for risk_period in chain . from_iterable ( itervalues ( report . to_dict ( ) ) ) : <EOL> self . assertIsNone ( risk_period [ '<STR_LIT>' ] ) </s>
