def test_no_param_volatility(self): cm = ConstantMean(self.y) cm.volatility = EWMAVariance() cm.fit(update_freq=0) cm.volatility = RiskMetrics2006() cm.fit(update_freq=0) ar = ARX(self.y, lags=5) ar.volatility = EWMAVariance() ar.fit(update_freq=0) ar.volatility = RiskMetrics2006() ar.fit(update_freq=0)
def test_no_param_volatility(self): cm = ConstantMean(self.y) cm.volatility = EWMAVariance() cm.fit(update_freq=0, disp=DISPLAY) cm.volatility = RiskMetrics2006() cm.fit(update_freq=0, disp=DISPLAY) ar = ARX(self.y, lags=5) ar.volatility = EWMAVariance() ar.fit(update_freq=0, disp=DISPLAY) ar.volatility = RiskMetrics2006() ar.fit(update_freq=0, disp=DISPLAY) assert 'tau0' in str(ar.volatility) assert 'tau1' in str(ar.volatility) assert 'kmax' in str(ar.volatility)
def test_rm_2006(self): rm2006 = RiskMetrics2006() params = np.empty(0) resids = self.resids sigma2 = self.sigma2 nobs = self.nobs backcast = rm2006.backcast(self.resids) rm2006.compute_variance(params, resids, sigma2, backcast, self.var_bounds) sigma2_ref = sigma2.copy() sigma2[:] = np.nan cw = rm2006._ewma_combination_weights() sp = rm2006._ewma_smoothing_parameters() ru = recpy.RiskMetrics2006Updater(rm2006.kmax, cw, sp) ru.initialize_update(None, backcast, None) for t in range(nobs): ru._update_tester(t, params, resids, sigma2, self.var_bounds) if t == nobs // 2: ru = pickle.loads(pickle.dumps(ru)) ru.initialize_update(None, backcast, None) assert_allclose(sigma2, sigma2_ref) sigma2_py = sigma2.copy() sigma2[:] = np.nan ru = rec.RiskMetrics2006Updater(rm2006.kmax, cw, sp) ru.initialize_update(None, backcast, None) for t in range(nobs): ru._update_tester(t, params, resids, sigma2, self.var_bounds) if t == nobs // 2: ru = pickle.loads(pickle.dumps(ru)) ru.initialize_update(None, backcast, None) ru.initialize_update(None, 3.0, None) assert_allclose(sigma2, sigma2_py)
def test_backcast_error(simulated_data): zm = ZeroMean(simulated_data, volatility=GARCH()) with pytest.raises(ValueError): zm.fit(backcast=-1, disp=DISPLAY) zm = ZeroMean(simulated_data, volatility=RiskMetrics2006()) with pytest.raises(ValueError): zm.fit(backcast=np.ones(100), disp=DISPLAY)
def test_riskmetrics(self): rm06 = RiskMetrics2006() sv = rm06.starting_values(self.resids) assert_equal(sv.shape[0], rm06.num_params) bounds = rm06.bounds(self.resids) assert_equal(len(bounds), 0) var_bounds = rm06.variance_bounds(self.resids) backcast = rm06.backcast(self.resids) assert_equal(backcast.shape[0], 14) parameters = np.array([]) names = rm06.parameter_names() names_target = [] assert_equal(names, names_target) # TODO: Test variance fit by RM06 rm06.compute_variance(parameters, self.resids, self.sigma2, backcast, var_bounds) A, b = rm06.constraints() A_target = np.empty((0, 0)) b_target = np.empty((0, )) assert_array_equal(A, A_target) assert_array_equal(b, b_target) # TODO: Test RM06 Simulation state = np.random.get_state() rng = Normal() sim_data = rm06.simulate(parameters, self.T, rng.simulate([])) assert_equal(rm06.num_params, 0) assert_equal(rm06.name, 'RiskMetrics2006')
def test_errors(self): with pytest.raises(ValueError): GARCH(p=-1) with pytest.raises(ValueError): GARCH(o=-1) with pytest.raises(ValueError): GARCH(q=-1) with pytest.raises(ValueError): GARCH(p=0, q=0) with pytest.raises(ValueError): GARCH(power=-0.5) with pytest.raises(ValueError): EWMAVariance(lam=-0.5) with pytest.raises(ValueError): RiskMetrics2006(tau0=1, tau1=10) with pytest.raises(ValueError): RiskMetrics2006(tau0=1, tau1=10) with pytest.raises(ValueError): RiskMetrics2006(tau1=-10) with pytest.raises(ValueError): RiskMetrics2006(tau0=10, tau1=8, rho=1.5) with pytest.raises(ValueError): RiskMetrics2006(kmax=0) with pytest.raises(ValueError): RiskMetrics2006(rho=0.5)
y[::29] = np.nan with pytest.raises(ValueError, match="NaN or inf values"): arch_model(y) y = np.random.standard_normal(1000) y[::53] = np.inf with pytest.raises(ValueError, match="NaN or inf values"): arch_model(y) y[::29] = np.nan y[::53] = np.inf with pytest.raises(ValueError, match="NaN or inf values"): arch_model(y) @pytest.mark.parametrize("first_obs", [None, 250]) @pytest.mark.parametrize("last_obs", [None, 2750]) @pytest.mark.parametrize("vol", [RiskMetrics2006(), EWMAVariance()]) def test_parameterless_fit(first_obs, last_obs, vol): base = ConstantMean(SP500, volatility=vol) base_res = base.fit(first_obs=first_obs, last_obs=last_obs, disp="off") mod = ZeroMean(SP500, volatility=vol) res = mod.fit(first_obs=first_obs, last_obs=last_obs, disp="off") assert res.conditional_volatility.shape == base_res.conditional_volatility.shape def test_invalid_vol_dist(): with pytest.raises(TypeError, match="volatility must inherit"): ConstantMean(SP500, volatility="GARCH") with pytest.raises(TypeError, match="distribution must inherit"): ConstantMean(SP500, distribution="Skew-t")