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_zero_mean(self): zm = ZeroMean(self.y) parameters = np.array([1.0]) data = zm.simulate(parameters, self.T) assert_equal(data.shape, (self.T, 3)) assert_equal(data['data'].shape[0], self.T) assert_equal(zm.num_params, 0) bounds = zm.bounds() assert_equal(bounds, []) assert_equal(zm.constant, False) a, b = zm.constraints() assert_equal(a, np.empty((0, 0))) assert_equal(b, np.empty((0,))) assert isinstance(zm.volatility, ConstantVariance) assert isinstance(zm.distribution, Normal) assert_equal(zm.lags, None) res = zm.fit(disp='off') assert_almost_equal(res.params, np.array([np.mean(self.y ** 2)])) forecasts = res.forecast(horizon=99) direct = pd.DataFrame(index=np.arange(self.y.shape[0]), columns=['h.{0:>02d}'.format(i + 1) for i in range(99)], dtype=np.float64) direct.iloc[:, :] = 0.0 assert isinstance(forecasts, ARCHModelForecast) # TODO # assert_frame_equal(direct, forecasts) garch = GARCH() zm.volatility = garch zm.fit(update_freq=0, disp=DISPLAY) assert isinstance(zm.__repr__(), str) assert isinstance(zm.__str__(), str) assert '<strong>' in zm._repr_html_()
def test_zero_mean(self): zm = ZeroMean(self.y) parameters = np.array([1.0]) data = zm.simulate(parameters, self.T) assert_equal(data.shape, (self.T, 3)) assert_equal(data['data'].shape[0], self.T) assert_equal(zm.num_params, 0) bounds = zm.bounds() assert_equal(bounds, []) assert_equal(zm.constant, False) a, b = zm.constraints() assert_equal(a, np.empty((0, 0))) assert_equal(b, np.empty((0,))) assert isinstance(zm.volatility, ConstantVariance) assert isinstance(zm.distribution, Normal) assert_equal(zm.lags, None) res = zm.fit(disp='off') assert_almost_equal(res.params, np.array([np.mean(self.y ** 2)])) forecasts = res.forecast(horizon=99) direct = pd.DataFrame(index=np.arange(self.y.shape[0]), columns=['h.{0:>02d}'.format(i + 1) for i in range(99)], dtype=np.float64) direct.iloc[:, :] = 0.0 assert isinstance(forecasts, ARCHModelForecast) # TODO # assert_frame_equal(direct, forecasts) garch = GARCH() zm.volatility = garch zm.fit(update_freq=0, disp=DISPLAY)
def test_backcast(volatility, simulated_data): zm = ZeroMean(simulated_data, volatility=volatility()) res = zm.fit(disp=DISPLAY) bc = zm.volatility.backcast(np.asarray(res.resid)) if volatility is EGARCH: bc = np.exp(bc) res2 = zm.fit(backcast=bc, disp=DISPLAY) assert_array_almost_equal(res.params, res2.params) if volatility is RiskMetrics2006: zm.fit(backcast=bc[0], disp=DISPLAY)
def test_arch_lm(simulated_data): zm = ZeroMean(simulated_data, volatility=GARCH()) res = zm.fit(disp=DISPLAY) wald = res.arch_lm_test() nobs = simulated_data.shape[0] df = int(np.ceil(12.0 * np.power(nobs / 100.0, 1 / 4.0))) assert wald.df == df assert "Standardized" not in wald.null assert "Standardized" not in wald.alternative assert "H0: Standardized" not in wald.__repr__() assert "heteroskedastic" in wald.__repr__() resids2 = pd.Series(res.resid ** 2) data = [resids2.shift(i) for i in range(df + 1)] data = pd.concat(data, 1).dropna() lhs = data.iloc[:, 0] rhs = smtools.add_constant(data.iloc[:, 1:]) ols_res = smlm.OLS(lhs, rhs).fit() assert_almost_equal(wald.stat, nobs * ols_res.rsquared) assert len(wald.critical_values) == 3 assert "10%" in wald.critical_values wald = res.arch_lm_test(lags=5) assert wald.df == 5 assert_almost_equal(wald.pval, 1 - stats.chi2(5).cdf(wald.stat)) wald = res.arch_lm_test(standardized=True) assert wald.df == df assert "Standardized" in wald.null assert "Standardized" in wald.alternative assert_almost_equal(wald.pval, 1 - stats.chi2(df).cdf(wald.stat)) assert "H0: Standardized" in wald.__repr__()
def test_fit_smoke(simulated_data, volatility): zm = ZeroMean(simulated_data, volatility=volatility()) zm.fit(disp=DISPLAY)
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