def test_summary(self): am = arch_model(self.y, mean='ar', lags=[1, 3, 5]) res = am.fit(update_freq=0, disp=DISPLAY) res.summary() am = arch_model(self.y, mean='ar', lags=[1, 3, 5], dist='studentst') res = am.fit(update_freq=0, disp=DISPLAY) res.summary()
def test_1d_exog(): y = np.random.standard_normal((300)) x = np.random.standard_normal((300)) am = arch_model(y, x, mean="ARX", lags=2, vol="ARCH", q=0) res = am.fit() am = arch_model(y, x[:, None], mean="ARX", lags=2, vol="ARCH", q=0) res2 = am.fit() assert_series_equal(res.params, res2.params)
def test_starting_values(self): am = arch_model(self.y, mean='ar', lags=[1, 3, 5]) res = am.fit(cov_type='mle', update_freq=0) res2 = am.fit(starting_values=res.params, update_freq=0) am = arch_model(self.y, mean='zero') sv = np.array([1.0, 0.3, 0.8]) with warnings.catch_warnings(record=True) as w: am.fit(starting_values=sv, update_freq=0) assert_equal(len(w), 1)
def test_optimization_options(self): am = arch_model(None) data = am.simulate([0.0, 0.1, 0.1, 0.85], 2500) am = arch_model(data.data) std = am.fit(disp='off') loose = am.fit(tol=1e-2, disp='off') assert std.loglikelihood != loose.loglikelihood with warnings.catch_warnings(record=True) as w: short = am.fit(options={'maxiter': 3}, disp='off') assert len(w) == 1 assert std.loglikelihood != short.loglikelihood assert short.convergence_flag != 0
def test_backcast_restricted(simulated_data): # GH 440 mod = arch_model(simulated_data) res = mod.fit(disp="off") subset = (simulated_data[100:600] if isinstance(simulated_data, np.ndarray) else simulated_data.iloc[100:600]) mod_restricted = arch_model(subset) res_restricted = mod_restricted.fit(disp="off") res_limited = mod.fit(first_obs=100, last_obs=600, disp="off") assert_almost_equal(res_restricted.model._backcast, res_restricted.model._backcast) assert np.abs(res.model._backcast - res_limited.model._backcast) > 1e-8
def test_model_obs_equivalence(self): """Tests models that should use the same observation""" am = arch_model(self.y_series.iloc[100:900]) res = am.fit(disp='off') am = arch_model(self.y_series) res2 = am.fit(disp='off', first_obs=100, last_obs=900) index = self.y_series.index res3 = am.fit(disp='off', first_obs=index[100], last_obs=index[900]) assert_equal(res.params.values, res2.params.values) assert_equal(res2.params.values, res3.params.values) am = arch_model(self.y_series, hold_back=100) res4 = am.fit(disp='off', last_obs=900) assert_equal(res.params.values, res4.params.values)
def test_starting_values(self): am = arch_model(self.y, mean='ar', lags=[1, 3, 5]) res = am.fit(cov_type='mle', update_freq=0, disp=DISPLAY) res2 = am.fit(starting_values=res.params, update_freq=0, disp=DISPLAY) assert isinstance(res, ARCHModelResult) assert isinstance(res2, ARCHModelResult) assert len(res.params) == 7 assert len(res2.params) == 7 am = arch_model(self.y, mean='zero') sv = np.array([1.0, 0.3, 0.8]) with warnings.catch_warnings(record=True) as w: am.fit(starting_values=sv, update_freq=0, disp=DISPLAY) assert_equal(len(w), 1)
def test_optimization_options(self): norm = Normal(random_state=RandomState([12891298, 843084])) am = arch_model(None) am.distribution = norm data = am.simulate(np.array([0.0, 0.1, 0.1, 0.85]), 2500) am = arch_model(data.data) std = am.fit(disp=DISPLAY) loose = am.fit(tol=1e-2, disp=DISPLAY) assert std.loglikelihood >= loose.loglikelihood with warnings.catch_warnings(record=True) as w: short = am.fit(options={"maxiter": 3}, disp=DISPLAY) assert len(w) == 1 assert std.loglikelihood >= short.loglikelihood assert short.convergence_flag != 0
def test_model_obs_equivalence_ar(self): """Tests models that should use the same observation""" am = arch_model(self.y_series.iloc[100:900], mean="AR", lags=[1, 2, 4]) res = am.fit(disp=DISPLAY) am = arch_model(self.y_series, mean="AR", lags=[1, 2, 4]) res2 = am.fit(disp=DISPLAY, first_obs=100, last_obs=900) index = self.y_series.index res3 = am.fit(disp=DISPLAY, first_obs=index[100], last_obs=index[900]) assert_almost_equal(res.params.values, res2.params.values, decimal=4) assert_almost_equal(res2.params.values, res3.params.values, decimal=4) am = arch_model(self.y_series, mean="AR", lags=[1, 2, 4], hold_back=100) res4 = am.fit(disp=DISPLAY, first_obs=4, last_obs=900) assert_almost_equal(res.params.values, res4.params.values, decimal=4) assert am.hold_back == 100
def test_sample_adjustment(self): am = arch_model(self.y_series, vol='Constant') res = am.fit(disp='off') res_adj = am.fit(disp='off', first_obs=0, last_obs=self.y_series.shape[0] + 1) assert_equal(res.resid.values, res_adj.resid.values) assert_equal(res.params.values, res_adj.params.values) res = am.fit(disp='off', first_obs=100) assert res.fit_start == 100 res_adj = am.fit(disp='off', first_obs=self.y_series.index[100]) assert_equal(res.params.values, res_adj.params.values) assert_equal(res.resid.values, res_adj.resid.values) res = am.fit(disp='off', last_obs=900) res2 = am.fit(disp='off', last_obs=self.y_series.index[900]) assert_equal(res.params.values, res2.params.values) assert_equal(res.resid.values, res2.resid.values) res = am.fit(disp='off', first_obs=100, last_obs=900) res2 = am.fit(disp='off', first_obs=self.y_series.index[100], last_obs=self.y_series.index[900]) assert_equal(res.params.values, res2.params.values) assert_equal(res.resid.values, res2.resid.values)
def test_model_obs_equivalence_ar(self): """Tests models that should use the same observation""" am = arch_model(self.y_series.iloc[100:900], mean='AR', lags=[1, 2, 4]) res = am.fit(disp='off') am = arch_model(self.y_series, mean='AR', lags=[1, 2, 4]) res2 = am.fit(disp='off', first_obs=100, last_obs=900) index = self.y_series.index res3 = am.fit(disp='off', first_obs=index[100], last_obs=index[900]) assert_almost_equal(res.params.values, res2.params.values) assert_almost_equal(res2.params.values, res3.params.values) am = arch_model(self.y_series, mean='AR', lags=[1, 2, 4], hold_back=100) res4 = am.fit(disp='off', first_obs=4, last_obs=900) assert_almost_equal(res.params.values, res4.params.values, decimal=4) assert am.hold_back == 100
def test_first_after_last(self): am = arch_model(self.y_series) with pytest.raises(ValueError): am.fit(disp='off', first_obs=500, last_obs=480) with pytest.raises(ValueError): am.fit(disp='off', first_obs=self.y_series.index[500], last_obs=self.y_series.index[480])
def test_summary(self): am = arch_model(self.y, mean='ar', lags=[1, 3, 5]) res = am.fit(update_freq=0, disp=DISPLAY) res.summary() am = arch_model(self.y, mean='ar', lags=[1, 3, 5], dist='studentst') assert isinstance(am.distribution, StudentsT) res = am.fit(update_freq=0, disp=DISPLAY) res.summary() am = arch_model(self.y, mean='ar', lags=[1, 3, 5], dist='ged') assert isinstance(am.distribution, GeneralizedError) res = am.fit(update_freq=0, disp=DISPLAY) res.summary() am = arch_model(self.y, mean='ar', lags=[1, 3, 5], dist='skewt') res = am.fit(update_freq=0, disp=DISPLAY) assert isinstance(am.distribution, SkewStudent) res.summary()
def test_autoscale(): rs = np.random.RandomState(34254321) dist = Normal(random_state=rs) am = arch_model(None) am.distribution = dist data = am.simulate([0, 0.0001, 0.05, 0.94], nobs=1000) am = arch_model(data.data) with pytest.warns(DataScaleWarning): res = am.fit(disp=DISPLAY) assert_almost_equal(res.scale, 1.0) am = arch_model(data.data, rescale=True) res_auto = am.fit(disp=DISPLAY) assert_almost_equal(res_auto.scale, 10.0) am = arch_model(10 * data.data) res_manual = am.fit(disp=DISPLAY) assert_series_equal(res_auto.params, res_manual.params) res_no = arch_model(data.data, rescale=False).fit(disp=DISPLAY) assert res_no.scale == 1.0 am = arch_model(10000 * data.data, rescale=True) res_big = am.fit(disp=DISPLAY) assert_almost_equal(res_big.scale, 0.1)
def test_fixed_user_parameters(self): am = arch_model(self.y_series) res = am.fit(disp=DISPLAY) fixed_res = am.fix(res.params) assert_series_equal(res.conditional_volatility, fixed_res.conditional_volatility) assert_series_equal(res.params, fixed_res.params) assert_equal(res.aic, fixed_res.aic) assert_equal(res.bic, fixed_res.bic) assert_equal(res.loglikelihood, fixed_res.loglikelihood) assert_equal(res.num_params, fixed_res.num_params) # Smoke for summary fixed_res.summary()
def test_output_options(self): am = arch_model(self.y_series) orig_stdout = sys.stdout try: sio = StringIO() sys.stdout = sio am.fit(disp=DISPLAY) sio.seek(0) output = sio.read() assert len(output) == 0 finally: sys.stdout = orig_stdout
def test_fixed_user_parameters_new_model(self): am = arch_model(self.y_series) res = am.fit(disp=DISPLAY) new_am = arch_model(self.y_series) fixed_res = new_am.fix(res.params) assert_series_equal(res.conditional_volatility, fixed_res.conditional_volatility) assert_series_equal(res.params, fixed_res.params) assert_equal(res.aic, fixed_res.aic) assert_equal(res.bic, fixed_res.bic) assert_equal(res.loglikelihood, fixed_res.loglikelihood) assert_equal(res.num_params, fixed_res.num_params) # Test first and last dates am = arch_model(self.y_series) res = am.fit(disp=DISPLAY, first_obs=100, last_obs=900) new_am = arch_model(self.y_series) fixed_res = new_am.fix(res.params, first_obs=100, last_obs=900) assert_series_equal(res.params, fixed_res.params) assert_equal(res.aic, fixed_res.aic) assert_equal(res.bic, fixed_res.bic) assert_equal(res.loglikelihood, fixed_res.loglikelihood) assert_equal(res.num_params, fixed_res.num_params)
def test_fixed_user_parameters_new_model(self): am = arch_model(self.y_series) res = am.fit(disp=DISPLAY) new_am = arch_model(self.y_series) fixed_res = new_am .fix(res.params) assert_series_equal(res.conditional_volatility, fixed_res.conditional_volatility) assert_series_equal(res.params, fixed_res.params) assert_equal(res.aic, fixed_res.aic) assert_equal(res.bic, fixed_res.bic) assert_equal(res.loglikelihood, fixed_res.loglikelihood) assert_equal(res.num_params, fixed_res.num_params) # Test first and last dates am = arch_model(self.y_series) res = am.fit(disp=DISPLAY, first_obs=100, last_obs=900) new_am = arch_model(self.y_series) fixed_res = new_am .fix(res.params, first_obs=100, last_obs=900) assert_series_equal(res.params, fixed_res.params) assert_equal(res.aic, fixed_res.aic) assert_equal(res.bic, fixed_res.bic) assert_equal(res.loglikelihood, fixed_res.loglikelihood) assert_equal(res.num_params, fixed_res.num_params)
def test_output_options(self): import sys from arch.compat.python import StringIO am = arch_model(self.y_series) orig_stdout = sys.stdout try: sio = StringIO() sys.stdout = sio res = am.fit(disp='final') sio.seek(0) print('SIO!') print(sio.read()) finally: sys.stdout = orig_stdout res = am.fit(disp='off')
def test_convergence_warning(self): y = np.array([0.83277114, 0.45194014, -0.33475561, -0.49463896, 0.54715787, 1.11895382, 1.31280266, 0.81464021, 0.8532107, 1.0967188, 0.9346354, 0.92289249, 1.01339085, 1.071065, 1.42413486, 1.15392453, 1.10929691, 0.96162061, 0.96489515, 0.93250153, 1.34509807, 1.80951607, 1.66313783, 1.38610821, 1.26381761]) am = arch_model(y, mean='ARX', lags=10, p=5, q=0) with pytest.warns(ConvergenceWarning): am.fit(disp=DISPLAY) with pytest.warns(ConvergenceWarning): am.fit(show_warning=True, disp=DISPLAY) with pytest.warns(DataScaleWarning): am.fit(show_warning=False, disp=DISPLAY)
def test_convergence_warning(self): y = np.array([0.83277114, 0.45194014, -0.33475561, -0.49463896, 0.54715787, 1.11895382, 1.31280266, 0.81464021, 0.8532107, 1.0967188, 0.9346354, 0.92289249, 1.01339085, 1.071065, 1.42413486, 1.15392453, 1.10929691, 0.96162061, 0.96489515, 0.93250153, 1.34509807, 1.80951607, 1.66313783, 1.38610821, 1.26381761]) am = arch_model(y, mean='ARX', lags=10, p=5, q=0) with warnings.catch_warnings(record=True) as w: am.fit() assert_equal(len(w), 1) with warnings.catch_warnings(record=True) as w: am.fit(show_warning=False) assert_equal(len(w), 0) with warnings.catch_warnings(record=True) as w: am.fit(show_warning=True) assert_equal(len(w), 1)
def test_missing_data_exception(): y = np.random.standard_normal(1000) 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)
def test_convergence_warning(self): y = np.array([0.83277114, 0.45194014, -0.33475561, -0.49463896, 0.54715787, 1.11895382, 1.31280266, 0.81464021, 0.8532107, 1.0967188, 0.9346354, 0.92289249, 1.01339085, 1.071065, 1.42413486, 1.15392453, 1.10929691, 0.96162061, 0.96489515, 0.93250153, 1.34509807, 1.80951607, 1.66313783, 1.38610821, 1.26381761]) am = arch_model(y, mean='ARX', lags=10, p=5, q=0) with warnings.catch_warnings(record=True) as w: am.fit(disp=DISPLAY) assert_equal(len(w), 1) with warnings.catch_warnings(record=True) as w: am.fit(show_warning=False, disp=DISPLAY) assert_equal(len(w), 0) with warnings.catch_warnings(record=True) as w: am.fit(show_warning=True, disp=DISPLAY) assert_equal(len(w), 1)
def test_output_options(self): am = arch_model(self.y_series) orig_stdout = sys.stdout try: sio = StringIO() sys.stdout = sio am.fit(disp='final') sio.seek(0) print('SIO!') print(sio.read()) finally: sys.stdout = orig_stdout try: sio = StringIO() sys.stdout = sio am.fit(disp='off') sio.seek(0) output = sio.read() assert len(output) == 0 finally: sys.stdout = orig_stdout
def test_arch_model(self): am = arch_model(self.y) assert isinstance(am, ConstantMean) assert isinstance(am.volatility, GARCH) assert isinstance(am.distribution, Normal) am = arch_model(self.y, mean='harx', lags=[1, 5, 22]) assert isinstance(am, HARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean='har', lags=[1, 5, 22]) assert isinstance(am, HARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, self.x, mean='ls') assert isinstance(am, LS) assert isinstance(am.volatility, GARCH) am.__repr__() am = arch_model(self.y, mean='arx', lags=[1, 5, 22]) assert isinstance(am, ARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean='ar', lags=[1, 5, 22]) assert isinstance(am, ARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean='ar', lags=None) assert isinstance(am, ARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean='zero') assert isinstance(am, ZeroMean) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, vol='Harch') assert isinstance(am, ConstantMean) assert isinstance(am.volatility, HARCH) am = arch_model(self.y, vol='Constant') assert isinstance(am, ConstantMean) assert isinstance(am.volatility, ConstantVariance) am = arch_model(self.y, vol='arch') assert isinstance(am.volatility, ARCH) am = arch_model(self.y, vol='egarch') assert isinstance(am.volatility, EGARCH) with pytest.raises(ValueError): arch_model(self.y, mean='unknown') with pytest.raises(ValueError): arch_model(self.y, vol='unknown') with pytest.raises(ValueError): arch_model(self.y, dist='unknown') am.fit(disp=DISPLAY)
def test_pandas(self): am = arch_model(self.y_df, self.x_df, mean='ls') assert isinstance(am, LS)
def test_invalid_arch_model(): with pytest.raises(TypeError, match="p must be"): arch_model(SP500, p="3")
def test_no_variance(): mod = arch_model(np.ones(100)) with pytest.warns(ConvergenceWarning): mod.fit(disp=DISPLAY)
def test_arch_model(self): am = arch_model(self.y) assert isinstance(am, ConstantMean) assert isinstance(am.volatility, GARCH) assert isinstance(am.distribution, Normal) am = arch_model(self.y, mean="harx", lags=[1, 5, 22]) assert isinstance(am, HARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean="har", lags=[1, 5, 22]) assert isinstance(am, HARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, self.x, mean="ls") assert isinstance(am, LS) assert isinstance(am.volatility, GARCH) am.__repr__() am = arch_model(self.y, mean="arx", lags=[1, 5, 22]) assert isinstance(am, ARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean="ar", lags=[1, 5, 22]) assert isinstance(am, ARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean="ar", lags=None) assert isinstance(am, ARX) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, mean="zero") assert isinstance(am, ZeroMean) assert isinstance(am.volatility, GARCH) am = arch_model(self.y, vol="Harch") assert isinstance(am, ConstantMean) assert isinstance(am.volatility, HARCH) am = arch_model(self.y, vol="Constant") assert isinstance(am, ConstantMean) assert isinstance(am.volatility, ConstantVariance) am = arch_model(self.y, vol="arch") assert isinstance(am.volatility, ARCH) am = arch_model(self.y, vol="egarch") assert isinstance(am.volatility, EGARCH) am = arch_model(self.y, vol="figarch") assert isinstance(am.volatility, FIGARCH) with pytest.raises(ValueError): arch_model(self.y, mean="unknown") with pytest.raises(ValueError): arch_model(self.y, vol="unknown") with pytest.raises(ValueError): arch_model(self.y, dist="unknown") am.fit(disp=DISPLAY)