|
10 | 10 |
|
11 | 11 | from numpy.testing import assert_allclose, assert_array_less |
12 | 12 | from pymc.model.transform.optimization import freeze_dims_and_data |
| 13 | +from pymc.testing import mock_sample_setup_and_teardown |
13 | 14 |
|
14 | 15 | from pymc_extras.statespace import BayesianVARMAX |
15 | 16 | from pymc_extras.statespace.utils.constants import SHORT_NAME_TO_LONG |
16 | 17 | from tests.statespace.shared_fixtures import ( # pylint: disable=unused-import |
17 | 18 | rng, |
18 | 19 | ) |
19 | 20 |
|
| 21 | +mock_sample = pytest.fixture(scope="function")(mock_sample_setup_and_teardown) |
| 22 | + |
20 | 23 | floatX = pytensor.config.floatX |
21 | 24 | ps = [0, 1, 2, 3] |
22 | 25 | qs = [0, 1, 2, 3] |
@@ -200,6 +203,48 @@ def test_forecast(varma_mod, idata, rng): |
200 | 203 | assert np.isfinite(forecast.forecast_observed.values).all() |
201 | 204 |
|
202 | 205 |
|
| 206 | +def test_varmax_workflow(rng, mock_sample): |
| 207 | + df = pd.read_csv( |
| 208 | + "tests/statespace/_data/statsmodels_macrodata_processed.csv", |
| 209 | + index_col=0, |
| 210 | + parse_dates=True, |
| 211 | + ).astype(floatX) |
| 212 | + df.index.freq = df.index.inferred_freq |
| 213 | + |
| 214 | + ss_mod = BayesianVARMAX( |
| 215 | + endog_names=df.columns, |
| 216 | + order=(1, 0), |
| 217 | + stationary_initialization=True, |
| 218 | + measurement_error=True, |
| 219 | + verbose=False, |
| 220 | + ) |
| 221 | + |
| 222 | + with pm.Model(coords=ss_mod.coords) as m: |
| 223 | + state_cov_diag = pm.Exponential("state_cov_diag", 1, dims=["shock"]) |
| 224 | + pm.Deterministic("state_cov", pt.diag(state_cov_diag), dims=["shock", "shock_aux"]) |
| 225 | + pm.Normal("ar_params", sigma=0.1, dims=["observed_state", "lag_ar", "observed_state_aux"]) |
| 226 | + pm.Exponential("sigma_obs", 1, dims=["observed_state"]) |
| 227 | + |
| 228 | + ss_mod.build_statespace_graph(df) |
| 229 | + |
| 230 | + idata = pm.sample() |
| 231 | + |
| 232 | + post = ss_mod.sample_conditional_posterior(idata, mvn_method="svd") |
| 233 | + assert "filtered_posterior" in post |
| 234 | + assert "smoothed_posterior" in post |
| 235 | + assert "predicted_posterior" in post |
| 236 | + |
| 237 | + forecast = ss_mod.forecast(idata, periods=10, random_seed=rng) |
| 238 | + assert "forecast_latent" in forecast |
| 239 | + assert "forecast_observed" in forecast |
| 240 | + assert np.isfinite(forecast.forecast_latent.values).all() |
| 241 | + assert np.isfinite(forecast.forecast_observed.values).all() |
| 242 | + |
| 243 | + irf = ss_mod.impulse_response_function(idata, n_steps=10, random_seed=rng) |
| 244 | + assert "irf" in irf |
| 245 | + assert np.isfinite(irf.irf.values).all() |
| 246 | + |
| 247 | + |
203 | 248 | class TestVARMAXWithExogenous: |
204 | 249 | def test_create_varmax_with_exogenous_list_of_names(self, data): |
205 | 250 | mod = BayesianVARMAX( |
|
0 commit comments