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 setup_class(cls): cls.rng = RandomState(1234) cls.T = 1000 cls.resids = cls.rng.standard_normal(cls.T) zm = ZeroMean() zm.volatility = GARCH() seed = 12345 random_state = np.random.RandomState(seed) zm.distribution = Normal(random_state=random_state) sim_data = zm.simulate(np.array([0.1, 0.1, 0.8]), 1000) with pytest.raises(ValueError): zm.simulate(np.array([0.1, 0.1, 0.8]), 1000, initial_value=3.0) date_index = pd.date_range("2000-12-31", periods=1000, freq="W") cls.y = sim_data.data.values cls.y_df = pd.DataFrame( cls.y[:, None], columns=["LongVariableName"], index=date_index ) cls.y_series = pd.Series( cls.y, name="VeryVeryLongLongVariableName", index=date_index ) x = cls.resids + cls.rng.standard_normal(cls.T) cls.x = x[:, None] cls.x_df = pd.DataFrame(cls.x, columns=["LongExogenousName"]) cls.resid_var = np.var(cls.resids) cls.sigma2 = np.zeros_like(cls.resids) cls.backcast = 1.0
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_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_empty_mean(self): mod = HARX(self.y, None, None, False, volatility=ConstantVariance(), distribution=Normal()) res = mod.fit() mod = ZeroMean(self.y, volatility=ConstantVariance(), distribution=Normal()) res_z = mod.fit() assert res.num_params == res_z.num_params assert_series_equal(res.params, res_z.params) assert res.loglikelihood == res_z.loglikelihood
def setup_class(cls): cls.rng = RandomState(1234) cls.T = 1000 cls.resids = cls.rng.randn(cls.T) zm = ZeroMean() zm.volatility = GARCH() sim_data = zm.simulate(np.array([0.1, 0.1, 0.8]), 1000) date_index = pd.date_range('2000-12-31', periods=1000, freq='W') cls.y = sim_data.data.values cls.y_df = pd.DataFrame(cls.y[:, None], columns=['LongVariableName'], index=date_index) cls.y_series = pd.Series(cls.y, name='VeryVeryLongLongVariableName', index=date_index) x = cls.resids + cls.rng.randn(cls.T) cls.x = x[:, None] cls.x_df = pd.DataFrame(cls.x, columns=['LongExogenousName']) cls.resid_var = np.var(cls.resids) cls.sigma2 = np.zeros_like(cls.resids) cls.backcast = 1.0
def simulated_data(request): rs = np.random.RandomState(1) zm = ZeroMean(volatility=GARCH(), distribution=Normal(rs)) sim_data = zm.simulate(np.array([0.1, 0.1, 0.88]), 1000) return np.asarray(sim_data.data) if request.param else sim_data.data
def test_fit_smoke(simulated_data, volatility): zm = ZeroMean(simulated_data, volatility=volatility()) zm.fit(disp=DISPLAY)
def simulated_data(): rs = np.random.RandomState(1) zm = ZeroMean(volatility=GARCH(), distribution=Normal(rs)) sim_data = zm.simulate(np.array([0.1, 0.1, 0.88]), 1000) return sim_data.data
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