def test_seasonality_smoke(index, forecast_index): s = Seasonality(12) s.in_sample(index) steps = 83 if forecast_index is None else len(forecast_index) warn = None if ( is_int_index(index) and np.any(np.diff(index) != 1) or ( type(index) is pd.Index and max(index) > 2 ** 63 and forecast_index is None ) ): warn = UserWarning with pytest_warns(warn): s.out_of_sample(steps, index, forecast_index) assert isinstance(s.period, int) str(s) hash(s) if isinstance(index, (pd.DatetimeIndex, pd.PeriodIndex)) and index.freq: s = Seasonality.from_index(index) s.in_sample(index) s.out_of_sample(steps, index, forecast_index) Seasonality.from_index(list(index))
def test_seed(self, seed): if SP_LT_15 and seed in ("generator", 0): pytest.xfail(reason="Generator not supported for SciPy <= 1.3") if seed == "random_state": seed1 = np.random.RandomState(0) seed2 = np.random.RandomState(0) elif seed == "generator": seed1 = np.random.default_rng(0) seed2 = 0 elif seed is None: seed1 = None singleton = np.random.mtrand._rand seed2 = np.random.RandomState() seed2.set_state(singleton.get_state()) elif seed == "qmc": if not hasattr(stats, "qmc"): pytest.skip("QMC not available") else: pytest.xfail("QMC not working") seed1 = stats.qmc.Halton(2) seed2 = stats.qmc.Halton(2) else: seed1 = 0 seed2 = np.random.default_rng(0) nobs = 2000 expected_warn = None if seed1 is not None else FutureWarning with pytest_warns(expected_warn): rvs1 = self.copula.rvs(nobs, random_state=seed1) rvs2 = self.copula.rvs(nobs, random_state=seed2) assert_allclose(rvs1, rvs2)
def test_time_trend_smoke(index, forecast_index): tt = TimeTrend(True, 2) tt.in_sample(index) steps = 83 if forecast_index is None else len(forecast_index) warn = None if ( is_int_index(index) and np.any(np.diff(index) != 1) or ( type(index) is pd.Index and max(index) > 2 ** 63 and forecast_index is None ) ): warn = UserWarning with pytest_warns(warn): tt.out_of_sample(steps, index, forecast_index) str(tt) hash(tt) assert isinstance(tt.order, int) assert isinstance(tt._constant, bool) assert TimeTrend.from_string("ctt") == tt assert TimeTrend.from_string("ct") != tt assert TimeTrend.from_string("t") != tt assert TimeTrend.from_string("n") != tt assert Seasonality(12) != tt tt0 = TimeTrend(False, 0) tt0.in_sample(index) str(tt0)
def test_seasonality(index): s = Seasonality(period=12) exog = s.in_sample(index) assert s.is_dummy assert exog.shape == (index.shape[0], 12) pd.testing.assert_index_equal(exog.index, index) assert np.all(exog.sum(1) == 1.0) assert list(exog.columns) == [f"s({i},12)" for i in range(1, 13)] expected = np.zeros((index.shape[0], 12)) for i in range(12): expected[i::12, i] = 1.0 np.testing.assert_equal(expected, np.asarray(exog)) warn = None if (is_int_index(index) and np.any(np.diff(index) != 1)) or ( type(index) is pd.Index and max(index) > 2 ** 63 ): warn = UserWarning with pytest_warns(warn): fcast = s.out_of_sample(steps=12, index=index) assert fcast.iloc[0, len(index) % 12] == 1.0 assert np.all(fcast.sum(1) == 1) s = Seasonality(period=7, initial_period=3) exog = s.in_sample(index) assert exog.iloc[0, 2] == 1.0 assert exog.iloc[0].sum() == 1.0 assert s.initial_period == 3 with pytest.raises(ValueError, match="initial_period must be in"): Seasonality(period=12, initial_period=-3) with pytest.raises(ValueError, match="period must be >= 2"): Seasonality(period=1)
def test_autoreg_summary_corner(old_names): data = macrodata.load_pandas().data["cpi"].diff().dropna() dates = period_range(start="1959Q1", periods=len(data), freq="Q") data.index = dates warning = FutureWarning if old_names else None with pytest_warns(warning): res = AutoReg(data, lags=4, old_names=old_names).fit() summ = res.summary().as_text() assert "AutoReg(4)" in summ assert "cpi.L4" in summ assert "03-31-1960" in summ with pytest_warns(warning): res = AutoReg(data, lags=0, old_names=old_names).fit() summ = res.summary().as_text() if old_names: assert "intercept" in summ else: assert "const" in summ assert "AutoReg(0)" in summ
def test_time_trend(index): tt = TimeTrend(constant=True) const = tt.in_sample(index) assert const.shape == (index.shape[0], 1) assert np.all(const == 1) pd.testing.assert_index_equal(const.index, index) warn = None if (is_int_index(index) and np.any(np.diff(index) != 1)) or ( type(index) is pd.Index and max(index) > 2 ** 63 ): warn = UserWarning with pytest_warns(warn): const_fcast = tt.out_of_sample(23, index) assert np.all(const_fcast == 1) tt = TimeTrend(constant=False) empty = tt.in_sample(index) assert empty.shape == (index.shape[0], 0) tt = TimeTrend(constant=False, order=2) t2 = tt.in_sample(index) assert t2.shape == (index.shape[0], 2) assert list(t2.columns) == ["trend", "trend_squared"] tt = TimeTrend(constant=True, order=2) final = tt.in_sample(index) expected = pd.concat([const, t2], axis=1) pd.testing.assert_frame_equal(final, expected) tt = TimeTrend(constant=True, order=2) short = tt.in_sample(index[:-50]) with pytest_warns(warn): remainder = tt.out_of_sample(50, index[:-50]) direct = tt.out_of_sample( steps=50, index=index[:-50], forecast_index=index[-50:] ) combined = pd.concat([short, remainder], axis=0) if isinstance(index, (pd.DatetimeIndex, pd.RangeIndex)): pd.testing.assert_frame_equal(combined, final) combined = pd.concat([short, direct], axis=0) pd.testing.assert_frame_equal(combined, final, check_index_type=False)
def test_autoreg_named_series(reset_randomstate, old_names): warning = FutureWarning if old_names else None dates = period_range(start="2011-1", periods=72, freq="M") y = Series(np.random.randn(72), name="foobar", index=dates) with pytest_warns(warning): results = AutoReg(y, lags=2, old_names=old_names).fit() if old_names: idx = Index(["intercept", "foobar.L1", "foobar.L2"]) else: idx = Index(["const", "foobar.L1", "foobar.L2"]) assert results.params.index.equals(idx)
def load_results_statsmodels(dataset): results_per_deterministic_terms = dict.fromkeys(dt_s_list) for dt_s_tup in dt_s_list: endog = data[dataset] exog = generate_exog_from_season(dt_s_tup[1], len(endog)) warn_typ = FutureWarning if dt_s_tup[0] == "nc" else None model = VAR(endog, exog) with pytest_warns(warn_typ): results_per_deterministic_terms[dt_s_tup] = model.fit( maxlags=4, trend=dt_s_tup[0], method="ols" ) return results_per_deterministic_terms
def test_lag_order_selection(): if debug_mode: if "lag order" not in to_test: return else: print("\n\nLAG ORDER SELECTION", end="") for ds in datasets: for dt in dt_s_list: if debug_mode: print("\n" + dt_s_tup_to_string(dt) + ": ", end="") endog_tot = data[ds] exog = generate_exog_from_season(dt[1], len(endog_tot)) model = VAR(endog_tot, exog) warn_typ = FutureWarning if dt[0] == "nc" else None with pytest_warns(warn_typ): obtained_all = model.select_order(10, trend=dt[0]) for ic in ["aic", "fpe", "hqic", "bic"]: err_msg = build_err_msg( ds, dt, "LAG ORDER SELECTION - " + ic.upper() ) obtained = getattr(obtained_all, ic) desired = results_ref[ds][dt]["lagorder"][ic] assert_allclose(obtained, desired, rtol, atol, False, err_msg)
def test_fourier_smoke(index, forecast_index): f = Fourier(12, 2) f.in_sample(index) steps = 83 if forecast_index is None else len(forecast_index) warn = None if ( is_int_index(index) and np.any(np.diff(index) != 1) or ( type(index) is pd.Index and max(index) > 2 ** 63 and forecast_index is None ) ): warn = UserWarning with pytest_warns(warn): f.out_of_sample(steps, index, forecast_index) assert isinstance(f.period, float) assert isinstance(f.order, int) str(f) hash(f) with pytest.raises(ValueError, match=r"2 \* order must be <= period"): Fourier(12, 7)
def test_zero_collinear(self): # not completely generic yet if isinstance(self.results.model, (sm.GEE)): pytest.skip('Not completely generic yet') use_start_params = not isinstance(self.results.model, (sm.RLM, sm.OLS, sm.WLS, sm.GLM)) self.use_start_params = use_start_params # attach for _get_constrained keep_index = list(range(self.results.model.exog.shape[1])) # index for params might include extra params keep_index_p = list(range(self.results.params.shape[0])) drop_index = [] for i in drop_index: del keep_index[i] del keep_index_p[i] keep_index_p = list(range(self.results.params.shape[0])) # create collinear model mod2 = self.results.model mod_cls = mod2.__class__ init_kwds = mod2._get_init_kwds() ex = np.column_stack((mod2.exog, mod2.exog)) mod = mod_cls(mod2.endog, ex, **init_kwds) keep_index = list(range(self.results.model.exog.shape[1])) keep_index_p = list(range(self.results.model.exog.shape[1])) k_vars = ex.shape[1] k_extra = 0 if hasattr(mod, 'k_extra') and mod.k_extra > 0: keep_index_p += list(range(k_vars, k_vars + mod.k_extra)) k_extra = mod.k_extra # TODO: Can we choose a test case without this issue? # If not, should we be getting this warning for all # model subclasses? warn_cls = HessianInversionWarning if isinstance(mod, sm.GLM) else None cov_types = ['nonrobust', 'HC0'] for cov_type in cov_types: # Note: for RLM we only check default when cov_type is 'nonrobust' # cov_type is otherwise ignored if cov_type != 'nonrobust' and (isinstance(self.results.model, sm.RLM)): return if use_start_params: start_params = np.zeros(k_vars + k_extra) method = self.results.mle_settings['optimizer'] # string in `method` is not mutable, so no need for copy sp = self.results.mle_settings['start_params'].copy() if self.transform_index is not None: # work around internal transform_params, currently in NB sp[self.transform_index] = np.exp(sp[self.transform_index]) start_params[keep_index_p] = sp with pytest_warns(warn_cls): res1 = mod._fit_collinear(cov_type=cov_type, start_params=start_params, method=method, disp=0) if cov_type != 'nonrobust': # reestimate original model to get robust cov with pytest_warns(warn_cls): res2 = self.results.model.fit(cov_type=cov_type, start_params=sp, method=method, disp=0) else: with pytest_warns(warn_cls): # more special casing RLM if (isinstance(self.results.model, (sm.RLM))): res1 = mod._fit_collinear() else: res1 = mod._fit_collinear(cov_type=cov_type) if cov_type != 'nonrobust': # reestimate original model to get robust cov res2 = self.results.model.fit(cov_type=cov_type) if cov_type == 'nonrobust': res2 = self.results # check fit optimizer arguments, if mle_settings is available if hasattr(res2, 'mle_settings'): assert_equal(res1.results_constrained.mle_settings['optimizer'], res2.mle_settings['optimizer']) if 'start_params' in res2.mle_settings: spc = res1.results_constrained.mle_settings['start_params'] assert_allclose(spc, res2.mle_settings['start_params'], rtol=1e-10, atol=1e-20) assert_equal(res1.mle_settings['optimizer'], res2.mle_settings['optimizer']) assert_allclose(res1.mle_settings['start_params'], res2.mle_settings['start_params'], rtol=1e-10, atol=1e-20) # Poisson has reduced precision in params, difficult optimization? assert_allclose(res1.params[keep_index_p], res2.params, rtol=1e-6) assert_allclose(res1.params[drop_index], 0, rtol=1e-10) assert_allclose(res1.bse[keep_index_p], res2.bse, rtol=1e-8) assert_allclose(res1.bse[drop_index], 0, rtol=1e-10) tvals1 = res1.tvalues[keep_index_p] assert_allclose(tvals1, res2.tvalues, rtol=5e-8) # See gh5993 if PLATFORM_LINUX32 or SCIPY_GT_14: pvals1 = res1.pvalues[keep_index_p] else: pvals1 = res1.pvalues[keep_index_p] assert_allclose(pvals1, res2.pvalues, rtol=1e-6, atol=1e-30) if hasattr(res1, 'resid'): # discrete models, Logit do not have `resid` yet assert_allclose(res1.resid, res2.resid, rtol=1e-5, atol=1e-10) ex = res1.model.exog.mean(0) predicted1 = res1.predict(ex, **self.predict_kwds) predicted2 = res2.predict(ex[keep_index], **self.predict_kwds) assert_allclose(predicted1, predicted2, rtol=1e-8, atol=1e-11) ex = res1.model.exog[:5] kwds = getattr(self, 'predict_kwds_5', {}) predicted1 = res1.predict(ex, **kwds) predicted2 = res2.predict(ex[:, keep_index], **kwds) assert_allclose(predicted1, predicted2, rtol=1e-8, atol=1e-11)