def test_var_invalid_trend_rasies(): # GH#2271 data = get_macrodata().view((float, 3), type=np.ndarray) model = VAR(data) with pytest.raises(ValueError): model.fit(4, trend='t')
def test_constructor(self): # make sure this works with no names ndarr = self.data.view((float, 3), type=np.ndarray) with warnings.catch_warnings(): warnings.simplefilter("ignore") model = VAR(ndarr) model.fit(self.p)
def test_var_constant(): # GH#2043 if one of the variables in a VAR is constant --> singularity index = pd.date_range('now', periods=5, freq='D') # TODO: remove the index since it is irrelevant to this test? data = np.array([[2., 2.], [1, 2.], [1, 2.], [1, 2.], [1., 2.]]) df = pd.DataFrame(data, index=index) model = VAR(df) with pytest.raises(ValueError): model.fit(1)
def test_select_order(self): with warnings.catch_warnings(): warnings.simplefilter("ignore") self.model.fit(10, ic='aic', verbose=True) self.model.fit(10, ic='fpe', verbose=True) # TODO: GH reference? upstream comment is just "bug" model = VAR(self.model.endog) model.select_order()
def test_lag_order_selection(case): res, expected, trend = case[:3] endog_tot, exog = case[-2:] with warnings.catch_warnings(): warnings.simplefilter("ignore") model = VAR(endog_tot, exog) obtained_all = model.select_order(10, trend=trend) for ic in ["aic", "fpe", "hqic", "bic"]: obtained = getattr(obtained_all, ic) desired = expected["lagorder"][ic] assert_allclose(obtained, desired, rtol, atol, False)
def test_reorder(self): # manually reorder data = self.data.view((float, 3), type=np.ndarray) names = self.names data2 = np.append(np.append(data[:, 2, None], data[:, 0, None], axis=1), data[:, 1, None], axis=1) names2 = [] names2.append(names[2]) names2.append(names[0]) names2.append(names[1]) with warnings.catch_warnings(): warnings.simplefilter("ignore") res2 = VAR(data2).fit(maxlags=self.p) # use reorder function res3 = self.res.reorder(['realinv', 'realgdp', 'realcons']) # check if the main results match assert_almost_equal(res2.params, res3.params) assert_almost_equal(res2.sigma_u, res3.sigma_u) assert_almost_equal(res2.bic, res3.bic) assert_almost_equal(res2.stderr, res3.stderr)
def case(request): dataset, trend, seasonal = request.param dtset = dataset.load_pandas() variables = dataset.variable_names loaded = dtset.data[variables].astype(float).values endog = loaded.reshape((-1, len(variables))) exog = generate_exog_from_season(seasonal, len(endog)) with warnings.catch_warnings(): # `rcond` parameter will change to the default of machine # precision times ``max(M, N)`` where M and N are the input # matrix dimensions.v warnings.simplefilter("ignore") model = VAR(endog, exog) res = model.fit(maxlags=4, trend=trend, method="ols") expected = load_results_jmulti(dataset, dt_s_list)[(trend, seasonal)] return res, expected, trend, seasonal, dataset, endog, exog
def test_cov_params_wrapping(): # GH#4463 (not the main issue) test that: # a) res.cov_params doesn't raise and # b) that the wrapping gets the order of the param index levels right data = sm.datasets.macrodata.load_pandas().data endog = data[['realgdp', 'realcons', 'realinv']].diff().iloc[1:] model = VAR(endog) res = model.fit(3) cp = res.cov_params z = res.endog_lagged v = scipy.linalg.inv(np.dot(z.T, z)) v = pd.DataFrame(v, index=res.params.index, columns=res.params.index) for eq1 in res.sigma_u.index: for eq2 in res.sigma_u.index: subdf = cp.loc[(slice(None), eq1), (slice(None), eq2)] expected = res.sigma_u.loc[eq1, eq2] * v # unfortunately subdf comes back with a MultiIndex constant in # its second level, so we have to compare values instead of frames. assert (expected.values == subdf.values).all()
def setup_class(cls): sdata, dates = get_lutkepohl_data('e1') data = data_util.struct_to_ndarray(sdata) adj_data = np.diff(np.log(data), axis=0) # est = VAR(adj_data, p=2, dates=dates[1:], names=names) cls.model = VAR(adj_data[:-16], dates=dates[1:-16], freq='BQ-MAR') cls.res = cls.model.fit(maxlags=cls.p) cls.irf = cls.res.irf(10) cls.lut = E1_Results()
def setup_class(cls): cls.data = get_macrodata() with warnings.catch_warnings(): warnings.simplefilter("ignore") cls.model = VAR(cls.data) cls.res = cls.model.fit(maxlags=cls.p) cls.names = cls.model.endog_names cls.ref = RResults() cls.k = len(cls.ref.names) cls.irf = cls.res.irf(cls.ref.nirfs) cls.nahead = cls.ref.nahead cls.fevd = cls.res.fevd()
def test_irf_trend(): # GH#1636 test for irf with different trend # this is a rough comparison by adding trend or subtracting mean to data # to get similar AR coefficients and IRF data = get_macrodata().view((float, 3), type=np.ndarray) with warnings.catch_warnings(): warnings.simplefilter("ignore") model = VAR(data) results = model.fit(4, trend='c') irf = results.irf(10) data_nc = data - data.mean(0) with warnings.catch_warnings(): warnings.simplefilter("ignore") model_nc = VAR(data_nc) results_nc = model_nc.fit(4, trend='nc') irf_nc = results_nc.irf(10) assert_allclose(irf_nc.stderr()[1:4], irf.stderr()[1:4], rtol=0.01) trend = 1e-3 * np.arange(len(data)) / (len(data) - 1) # for pandas version, currently not used, if data is a pd.DataFrame # data_t = pd.DataFrame(data.values + trend[:, None], # index=data.index, columns=data.columns) data_t = data + trend[:, None] with warnings.catch_warnings(): warnings.simplefilter("ignore") model_t = VAR(data_t) results_t = model_t.fit(4, trend='ct') irf_t = results_t.irf(10) assert_allclose(irf_t.stderr()[1:4], irf.stderr()[1:4], rtol=0.03)
def test_var_trend(): # GH#2271 data = get_macrodata().view((float, 3), type=np.ndarray) with warnings.catch_warnings(): warnings.simplefilter("ignore") model = VAR(data) results = model.fit(4, trend='c') results.irf(10) data_nc = data - data.mean(0) with warnings.catch_warnings(): warnings.simplefilter("ignore") model_nc = VAR(data_nc) model_nc.fit(4, trend='nc')
def test_var_process_mean(): # GH#4442, VARProcess.mean should be well-defined iff there is # a constant term and no other trend/exog terms data = get_macrodata().view((float, 3), type=np.ndarray) data -= data.mean(axis=0) np.random.seed(4719) intercept = np.random.randn(data.shape[1]) data += intercept.reshape(1, -1) model = VAR(data) res = model.fit(trend="c") assert res.k_trend == 1 assert not hasattr(model, "k_trend") assert_allclose(intercept, res.mean(), rtol=1e-3) # TODO: It would be nice to get higher precision than this for trend in ["nc", "ct"]: res = model.fit(trend=trend) with pytest.raises(NotImplementedError): res.mean() exog = np.zeros((len(data), 3)) exog[::4, 0] = 1 exog[1::4, 1] = 1 exog[2::4, 2] = 1 model = VAR(endog=data, exog=exog) res = model.fit(trend="c") with pytest.raises(NotImplementedError): res.mean() res = model.fit(trend="nc") with pytest.raises(NotImplementedError): res.mean()
def test_names(self): assert self.model.endog_names == list(self.ref.names) # TODO: self.ref.names should be a list in the first place model2 = VAR(self.data) assert model2.endog_names == list(self.ref.names)
def setup_class(cls): mdata = sm.datasets.macrodata.load_pandas().data mdata = mdata[['realgdp', 'realcons', 'realinv']] data = mdata.values data = np.diff(np.log(data), axis=0) * 400 cls.res0 = VAR(data).fit(maxlags=2)