|
8 | 8 | import pytest
|
9 | 9 |
|
10 | 10 | from pymc_extras.statespace.models import structural
|
| 11 | +from pymc_extras.statespace.models.structural import LevelTrendComponent |
11 | 12 | from pymc_extras.statespace.utils.constants import (
|
12 | 13 | FILTER_OUTPUT_DIMS,
|
13 | 14 | FILTER_OUTPUT_NAMES,
|
@@ -114,3 +115,67 @@ def test_data_index_is_coord(f, warning, create_model):
|
114 | 115 | with warning:
|
115 | 116 | pymc_model = create_model(f)
|
116 | 117 | assert TIME_DIM in pymc_model.coords
|
| 118 | + |
| 119 | + |
| 120 | +def make_model(index): |
| 121 | + n = len(index) |
| 122 | + a = pd.DataFrame(index=index, columns=["A", "B", "C", "D"], data=np.arange(n * 4).reshape(n, 4)) |
| 123 | + |
| 124 | + mod = LevelTrendComponent(order=2, innovations_order=[0, 1]) |
| 125 | + ss_mod = mod.build(name="a", verbose=False) |
| 126 | + |
| 127 | + initial_trend_dims, sigma_trend_dims, P0_dims = ss_mod.param_dims.values() |
| 128 | + coords = ss_mod.coords |
| 129 | + |
| 130 | + with pm.Model(coords=coords) as model: |
| 131 | + P0_diag = pm.Gamma("P0_diag", alpha=5, beta=5) |
| 132 | + P0 = pm.Deterministic("P0", pt.eye(ss_mod.k_states) * P0_diag, dims=P0_dims) |
| 133 | + |
| 134 | + initial_trend = pm.Normal("initial_trend", dims=initial_trend_dims) |
| 135 | + sigma_trend = pm.Gamma("sigma_trend", alpha=2, beta=50, dims=sigma_trend_dims) |
| 136 | + |
| 137 | + with pytest.warns(UserWarning, match="No time index found on the supplied data"): |
| 138 | + ss_mod.build_statespace_graph( |
| 139 | + a["A"], |
| 140 | + mode="JAX", |
| 141 | + ) |
| 142 | + return model |
| 143 | + |
| 144 | + |
| 145 | +def test_integer_index(): |
| 146 | + index = np.arange(8).astype(int) |
| 147 | + model = make_model(index) |
| 148 | + assert TIME_DIM in model.coords |
| 149 | + np.testing.assert_allclose(model.coords[TIME_DIM], index) |
| 150 | + |
| 151 | + |
| 152 | +def test_float_index_raises(): |
| 153 | + index = np.linspace(0, 1, 8) |
| 154 | + |
| 155 | + with pytest.raises(IndexError, match="Provided index is not an integer index"): |
| 156 | + make_model(index) |
| 157 | + |
| 158 | + |
| 159 | +def test_non_strictly_monotone_index_raises(): |
| 160 | + # Decreases |
| 161 | + index = [0, 1, 2, 1, 2, 3] |
| 162 | + with pytest.raises(IndexError, match="Provided index is not monotonic increasing"): |
| 163 | + make_model(index) |
| 164 | + |
| 165 | + # Has gaps |
| 166 | + index = [0, 1, 2, 3, 5, 6] |
| 167 | + with pytest.raises(IndexError, match="Provided index is not monotonic increasing"): |
| 168 | + make_model(index) |
| 169 | + |
| 170 | + # Has duplicates |
| 171 | + index = [0, 1, 1, 2, 3, 4] |
| 172 | + with pytest.raises(IndexError, match="Provided index is not monotonic increasing"): |
| 173 | + make_model(index) |
| 174 | + |
| 175 | + |
| 176 | +def test_multiindex_raises(): |
| 177 | + index = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2), (3, 3)]) |
| 178 | + with pytest.raises( |
| 179 | + NotImplementedError, match="MultiIndex panel data is not currently supported" |
| 180 | + ): |
| 181 | + make_model(index) |
0 commit comments