pymc-extras 0.2.7__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. pymc_extras/inference/__init__.py +2 -2
  2. pymc_extras/inference/fit.py +1 -1
  3. pymc_extras/inference/laplace_approx/__init__.py +0 -0
  4. pymc_extras/inference/laplace_approx/find_map.py +354 -0
  5. pymc_extras/inference/laplace_approx/idata.py +393 -0
  6. pymc_extras/inference/laplace_approx/laplace.py +453 -0
  7. pymc_extras/inference/laplace_approx/scipy_interface.py +242 -0
  8. pymc_extras/inference/pathfinder/pathfinder.py +3 -4
  9. pymc_extras/linearmodel.py +3 -1
  10. pymc_extras/model/marginal/graph_analysis.py +4 -0
  11. pymc_extras/prior.py +38 -6
  12. pymc_extras/statespace/core/statespace.py +78 -52
  13. pymc_extras/statespace/filters/kalman_smoother.py +1 -1
  14. pymc_extras/statespace/models/structural/__init__.py +21 -0
  15. pymc_extras/statespace/models/structural/components/__init__.py +0 -0
  16. pymc_extras/statespace/models/structural/components/autoregressive.py +188 -0
  17. pymc_extras/statespace/models/structural/components/cycle.py +305 -0
  18. pymc_extras/statespace/models/structural/components/level_trend.py +257 -0
  19. pymc_extras/statespace/models/structural/components/measurement_error.py +137 -0
  20. pymc_extras/statespace/models/structural/components/regression.py +228 -0
  21. pymc_extras/statespace/models/structural/components/seasonality.py +445 -0
  22. pymc_extras/statespace/models/structural/core.py +900 -0
  23. pymc_extras/statespace/models/structural/utils.py +16 -0
  24. pymc_extras/statespace/models/utilities.py +285 -0
  25. pymc_extras/statespace/utils/constants.py +4 -4
  26. pymc_extras/statespace/utils/data_tools.py +3 -2
  27. {pymc_extras-0.2.7.dist-info → pymc_extras-0.4.0.dist-info}/METADATA +6 -6
  28. {pymc_extras-0.2.7.dist-info → pymc_extras-0.4.0.dist-info}/RECORD +30 -18
  29. pymc_extras/inference/find_map.py +0 -496
  30. pymc_extras/inference/laplace.py +0 -583
  31. pymc_extras/statespace/models/structural.py +0 -1679
  32. {pymc_extras-0.2.7.dist-info → pymc_extras-0.4.0.dist-info}/WHEEL +0 -0
  33. {pymc_extras-0.2.7.dist-info → pymc_extras-0.4.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,305 @@
1
+ import numpy as np
2
+
3
+ from pytensor import tensor as pt
4
+ from pytensor.tensor.slinalg import block_diag
5
+
6
+ from pymc_extras.statespace.models.structural.core import Component
7
+ from pymc_extras.statespace.models.structural.utils import _frequency_transition_block
8
+
9
+
10
+ class CycleComponent(Component):
11
+ r"""
12
+ A component for modeling longer-term cyclical effects
13
+
14
+ Supports both univariate and multivariate time series. For multivariate time series,
15
+ each endogenous variable gets its own independent cycle component with separate
16
+ cosine/sine states and optional variable-specific innovation variances.
17
+
18
+ Parameters
19
+ ----------
20
+ name: str
21
+ Name of the component. Used in generated coordinates and state names. If None, a descriptive name will be
22
+ used.
23
+
24
+ cycle_length: int, optional
25
+ The length of the cycle, in the calendar units of your data. For example, if your data is monthly, and you
26
+ want to model a 12-month cycle, use ``cycle_length=12``. You cannot specify both ``cycle_length`` and
27
+ ``estimate_cycle_length``.
28
+
29
+ estimate_cycle_length: bool, default False
30
+ Whether to estimate the cycle length. If True, an additional parameter, ``cycle_length`` will be added to the
31
+ model. You cannot specify both ``cycle_length`` and ``estimate_cycle_length``.
32
+
33
+ dampen: bool, default False
34
+ Whether to dampen the cycle by multiplying by a dampening factor :math:`\rho` at every timestep. If true,
35
+ an additional parameter, ``dampening_factor`` will be added to the model.
36
+
37
+ innovations: bool, default True
38
+ Whether to include stochastic innovations in the strength of the seasonal effect. If True, an additional
39
+ parameter, ``sigma_{name}`` will be added to the model.
40
+ For multivariate time series, this is a vector (variable-specific innovation variances).
41
+
42
+ observed_state_names: list[str], optional
43
+ Names of the observed state variables. For univariate time series, defaults to ``["data"]``.
44
+ For multivariate time series, specify a list of names for each endogenous variable.
45
+
46
+ Notes
47
+ -----
48
+ The cycle component is very similar in implementation to the frequency domain seasonal component, expect that it
49
+ is restricted to n=1. The cycle component can be expressed:
50
+
51
+ .. math::
52
+ \begin{align}
53
+ \gamma_t &= \rho \gamma_{t-1} \cos \lambda + \rho \gamma_{t-1}^\star \sin \lambda + \omega_{t} \\
54
+ \gamma_{t}^\star &= -\rho \gamma_{t-1} \sin \lambda + \rho \gamma_{t-1}^\star \cos \lambda + \omega_{t}^\star \\
55
+ \lambda &= \frac{2\pi}{s}
56
+ \end{align}
57
+
58
+ Where :math:`s` is the ``cycle_length``. [1] recommend that this component be used for longer term cyclical
59
+ effects, such as business cycles, and that the seasonal component be used for shorter term effects, such as
60
+ weekly or monthly seasonality.
61
+
62
+ Unlike a FrequencySeasonality component, the length of a CycleComponent can be estimated.
63
+
64
+ **Multivariate Support:**
65
+ For multivariate time series with k endogenous variables, the component creates:
66
+ - 2k states (cosine and sine components for each variable)
67
+ - Block diagonal transition and selection matrices
68
+ - Variable-specific innovation variances (optional)
69
+ - Proper parameter shapes: (k, 2) for initial states, (k,) for innovation variances
70
+
71
+ Examples
72
+ --------
73
+ **Univariate Example:**
74
+ Estimate a business cycle with length between 6 and 12 years:
75
+
76
+ .. code:: python
77
+
78
+ from pymc_extras.statespace import structural as st
79
+ import pymc as pm
80
+ import pytensor.tensor as pt
81
+ import pandas as pd
82
+ import numpy as np
83
+
84
+ data = np.random.normal(size=(100, 1))
85
+
86
+ # Build the structural model
87
+ grw = st.LevelTrendComponent(order=1, innovations_order=1)
88
+ cycle = st.CycleComponent(
89
+ "business_cycle", cycle_length=12, estimate_cycle_length=False, innovations=True, dampen=True
90
+ )
91
+ ss_mod = (grw + cycle).build()
92
+
93
+ # Estimate with PyMC
94
+ with pm.Model(coords=ss_mod.coords) as model:
95
+ P0 = pm.Deterministic('P0', pt.eye(ss_mod.k_states), dims=ss_mod.param_dims['P0'])
96
+
97
+ initial_level_trend = pm.Normal('initial_level_trend', dims=ss_mod.param_dims['initial_level_trend'])
98
+ sigma_level_trend = pm.HalfNormal('sigma_level_trend', dims=ss_mod.param_dims['sigma_level_trend'])
99
+
100
+ business_cycle = pm.Normal("business_cycle", dims=ss_mod.param_dims["business_cycle"])
101
+ dampening = pm.Beta("dampening_factor_business_cycle", 2, 2)
102
+ sigma_cycle = pm.HalfNormal("sigma_business_cycle", sigma=1)
103
+
104
+ ss_mod.build_statespace_graph(data)
105
+ idata = pm.sample(
106
+ nuts_sampler="nutpie", nuts_sampler_kwargs={"backend": "JAX", "gradient_backend": "JAX"}
107
+ )
108
+
109
+ **Multivariate Example:**
110
+ Model cycles for multiple economic indicators with variable-specific innovation variances:
111
+
112
+ .. code:: python
113
+
114
+ # Multivariate cycle component
115
+ cycle = st.CycleComponent(
116
+ name='business_cycle',
117
+ cycle_length=12,
118
+ estimate_cycle_length=False,
119
+ innovations=True,
120
+ dampen=True,
121
+ observed_state_names=['gdp', 'unemployment', 'inflation']
122
+ )
123
+ ss_mod = cycle.build()
124
+
125
+ with pm.Model(coords=ss_mod.coords) as model:
126
+ P0 = pm.Deterministic("P0", pt.eye(ss_mod.k_states), dims=ss_mod.param_dims["P0"])
127
+ # Initial states: shape (3, 2) for 3 variables, 2 states each
128
+ business_cycle = pm.Normal('business_cycle', dims=ss_mod.param_dims["business_cycle"])
129
+
130
+ # Dampening factor: scalar (shared across variables)
131
+ dampening = pm.Beta("dampening_factor_business_cycle", 2, 2)
132
+
133
+ # Innovation variances: shape (3,) for variable-specific variances
134
+ sigma_cycle = pm.HalfNormal(
135
+ "sigma_business_cycle", dims=ss_mod.param_dims["sigma_business_cycle"]
136
+ )
137
+
138
+ ss_mod.build_statespace_graph(data)
139
+ idata = pm.sample(
140
+ nuts_sampler="nutpie", nuts_sampler_kwargs={"backend": "JAX", "gradient_backend": "JAX"}
141
+ )
142
+
143
+ References
144
+ ----------
145
+ .. [1] Durbin, James, and Siem Jan Koopman. 2012.
146
+ Time Series Analysis by State Space Methods: Second Edition.
147
+ Oxford University Press.
148
+ """
149
+
150
+ def __init__(
151
+ self,
152
+ name: str | None = None,
153
+ cycle_length: int | None = None,
154
+ estimate_cycle_length: bool = False,
155
+ dampen: bool = False,
156
+ innovations: bool = True,
157
+ observed_state_names: list[str] | None = None,
158
+ ):
159
+ if observed_state_names is None:
160
+ observed_state_names = ["data"]
161
+
162
+ if cycle_length is None and not estimate_cycle_length:
163
+ raise ValueError("Must specify cycle_length if estimate_cycle_length is False")
164
+ if cycle_length is not None and estimate_cycle_length:
165
+ raise ValueError("Cannot specify cycle_length if estimate_cycle_length is True")
166
+ if name is None:
167
+ cycle = int(cycle_length) if cycle_length is not None else "Estimate"
168
+ name = f"Cycle[s={cycle}, dampen={dampen}, innovations={innovations}]"
169
+
170
+ self.estimate_cycle_length = estimate_cycle_length
171
+ self.cycle_length = cycle_length
172
+ self.innovations = innovations
173
+ self.dampen = dampen
174
+ self.n_coefs = 1
175
+
176
+ k_endog = len(observed_state_names)
177
+
178
+ k_states = 2 * k_endog
179
+ k_posdef = 2 * k_endog
180
+
181
+ obs_state_idx = np.zeros(k_states)
182
+ obs_state_idx[slice(0, k_states, 2)] = 1
183
+
184
+ super().__init__(
185
+ name=name,
186
+ k_endog=k_endog,
187
+ k_states=k_states,
188
+ k_posdef=k_posdef,
189
+ measurement_error=False,
190
+ combine_hidden_states=True,
191
+ obs_state_idxs=obs_state_idx,
192
+ observed_state_names=observed_state_names,
193
+ )
194
+
195
+ def make_symbolic_graph(self) -> None:
196
+ Z = np.array([1.0, 0.0]).reshape((1, -1))
197
+ design_matrix = block_diag(*[Z for _ in range(self.k_endog)])
198
+ self.ssm["design", :, :] = pt.as_tensor_variable(design_matrix)
199
+
200
+ # selection matrix R defines structure of innovations (always identity for cycle components)
201
+ # when innovations=False, state cov Q=0, hence R @ Q @ R.T = 0
202
+ R = np.eye(2) # 2x2 identity for each cycle component
203
+ selection_matrix = block_diag(*[R for _ in range(self.k_endog)])
204
+ self.ssm["selection", :, :] = pt.as_tensor_variable(selection_matrix)
205
+
206
+ init_state = self.make_and_register_variable(
207
+ f"{self.name}", shape=(self.k_endog, 2) if self.k_endog > 1 else (self.k_states,)
208
+ )
209
+ self.ssm["initial_state", :] = init_state.ravel()
210
+
211
+ if self.estimate_cycle_length:
212
+ lamb = self.make_and_register_variable(f"length_{self.name}", shape=())
213
+ else:
214
+ lamb = self.cycle_length
215
+
216
+ if self.dampen:
217
+ rho = self.make_and_register_variable(f"dampening_factor_{self.name}", shape=())
218
+ else:
219
+ rho = 1
220
+
221
+ T = rho * _frequency_transition_block(lamb, j=1)
222
+ transition = block_diag(*[T for _ in range(self.k_endog)])
223
+ self.ssm["transition"] = pt.specify_shape(transition, (self.k_states, self.k_states))
224
+
225
+ if self.innovations:
226
+ if self.k_endog == 1:
227
+ sigma_cycle = self.make_and_register_variable(f"sigma_{self.name}", shape=())
228
+ self.ssm["state_cov", :, :] = pt.eye(self.k_posdef) * sigma_cycle**2
229
+ else:
230
+ sigma_cycle = self.make_and_register_variable(
231
+ f"sigma_{self.name}", shape=(self.k_endog,)
232
+ )
233
+ state_cov = block_diag(
234
+ *[pt.eye(2) * sigma_cycle[i] ** 2 for i in range(self.k_endog)]
235
+ )
236
+ self.ssm["state_cov"] = pt.specify_shape(state_cov, (self.k_states, self.k_states))
237
+ else:
238
+ # explicitly set state cov to 0 when no innovations
239
+ self.ssm["state_cov", :, :] = pt.zeros((self.k_posdef, self.k_posdef))
240
+
241
+ def populate_component_properties(self):
242
+ self.state_names = [
243
+ f"{f}_{self.name}[{var_name}]" if self.k_endog > 1 else f"{f}_{self.name}"
244
+ for var_name in self.observed_state_names
245
+ for f in ["Cos", "Sin"]
246
+ ]
247
+
248
+ self.param_names = [f"{self.name}"]
249
+
250
+ if self.k_endog == 1:
251
+ self.param_dims = {self.name: (f"state_{self.name}",)}
252
+ self.coords = {f"state_{self.name}": self.state_names}
253
+ self.param_info = {
254
+ f"{self.name}": {
255
+ "shape": (2,),
256
+ "constraints": None,
257
+ "dims": (f"state_{self.name}",),
258
+ }
259
+ }
260
+ else:
261
+ self.param_dims = {self.name: (f"endog_{self.name}", f"state_{self.name}")}
262
+ self.coords = {
263
+ f"state_{self.name}": [f"Cos_{self.name}", f"Sin_{self.name}"],
264
+ f"endog_{self.name}": self.observed_state_names,
265
+ }
266
+ self.param_info = {
267
+ f"{self.name}": {
268
+ "shape": (self.k_endog, 2),
269
+ "constraints": None,
270
+ "dims": (f"endog_{self.name}", f"state_{self.name}"),
271
+ }
272
+ }
273
+
274
+ if self.estimate_cycle_length:
275
+ self.param_names += [f"length_{self.name}"]
276
+ self.param_info[f"length_{self.name}"] = {
277
+ "shape": () if self.k_endog == 1 else (self.k_endog,),
278
+ "constraints": "Positive, non-zero",
279
+ "dims": None if self.k_endog == 1 else f"endog_{self.name}",
280
+ }
281
+
282
+ if self.dampen:
283
+ self.param_names += [f"dampening_factor_{self.name}"]
284
+ self.param_info[f"dampening_factor_{self.name}"] = {
285
+ "shape": () if self.k_endog == 1 else (self.k_endog,),
286
+ "constraints": "0 < x ≤ 1",
287
+ "dims": None if self.k_endog == 1 else (f"endog_{self.name}",),
288
+ }
289
+
290
+ if self.innovations:
291
+ self.param_names += [f"sigma_{self.name}"]
292
+ if self.k_endog == 1:
293
+ self.param_info[f"sigma_{self.name}"] = {
294
+ "shape": (),
295
+ "constraints": "Positive",
296
+ "dims": None,
297
+ }
298
+ else:
299
+ self.param_dims[f"sigma_{self.name}"] = (f"endog_{self.name}",)
300
+ self.param_info[f"sigma_{self.name}"] = {
301
+ "shape": (self.k_endog,),
302
+ "constraints": "Positive",
303
+ "dims": (f"endog_{self.name}",),
304
+ }
305
+ self.shock_names = self.state_names.copy()
@@ -0,0 +1,257 @@
1
+ import numpy as np
2
+ import pytensor.tensor as pt
3
+
4
+ from pymc_extras.statespace.models.structural.core import Component
5
+ from pymc_extras.statespace.models.structural.utils import order_to_mask
6
+ from pymc_extras.statespace.utils.constants import POSITION_DERIVATIVE_NAMES
7
+
8
+
9
+ class LevelTrendComponent(Component):
10
+ r"""
11
+ Level and trend component of a structural time series model
12
+
13
+ Parameters
14
+ ----------
15
+ order : int
16
+
17
+ Number of time derivatives of the trend to include in the model. For example, when order=3, the trend will
18
+ be of the form ``y = a + b * t + c * t ** 2``, where the coefficients ``a, b, c`` come from the initial
19
+ state values.
20
+
21
+ innovations_order : int or sequence of int, optional
22
+
23
+ The number of stochastic innovations to include in the model. By default, ``innovations_order = order``
24
+
25
+ name : str, default "level_trend"
26
+ A name for this level-trend component. Used to label dimensions and coordinates.
27
+
28
+ observed_state_names : list[str] | None, default None
29
+ List of strings for observed state labels. If None, defaults to ["data"].
30
+
31
+ Notes
32
+ -----
33
+ This class implements the level and trend components of the general structural time series model. In the most
34
+ general form, the level and trend is described by a system of two time-varying equations.
35
+
36
+ .. math::
37
+ \begin{align}
38
+ \mu_{t+1} &= \mu_t + \nu_t + \zeta_t \\
39
+ \nu_{t+1} &= \nu_t + \xi_t
40
+ \zeta_t &\sim N(0, \sigma_\zeta) \\
41
+ \xi_t &\sim N(0, \sigma_\xi)
42
+ \end{align}
43
+
44
+ Where :math:`\mu_{t+1}` is the mean of the timeseries at time t, and :math:`\nu_t` is the drift or the slope of
45
+ the process. When both innovations :math:`\zeta_t` and :math:`\xi_t` are included in the model, it is known as a
46
+ *local linear trend* model. This system of two equations, corresponding to ``order=2``, can be expanded or
47
+ contracted by adding or removing equations. ``order=3`` would add an acceleration term to the sytsem:
48
+
49
+ .. math::
50
+ \begin{align}
51
+ \mu_{t+1} &= \mu_t + \nu_t + \zeta_t \\
52
+ \nu_{t+1} &= \nu_t + \eta_t + \xi_t \\
53
+ \eta_{t+1} &= \eta_{t-1} + \omega_t \\
54
+ \zeta_t &\sim N(0, \sigma_\zeta) \\
55
+ \xi_t &\sim N(0, \sigma_\xi) \\
56
+ \omega_t &\sim N(0, \sigma_\omega)
57
+ \end{align}
58
+
59
+ After setting all innovation terms to zero and defining initial states :math:`\mu_0, \nu_0, \eta_0`, these equations
60
+ can be collapsed to:
61
+
62
+ .. math::
63
+ \mu_t = \mu_0 + \nu_0 \cdot t + \eta_0 \cdot t^2
64
+
65
+ Which clarifies how the order and initial states influence the model. In particular, the initial states are the
66
+ coefficients on the intercept, slope, acceleration, and so on.
67
+
68
+ In this light, allowing for innovations can be understood as allowing these coefficients to vary over time. Each
69
+ component can be individually selected for time variation by passing a list to the ``innovations_order`` argument.
70
+ For example, a constant intercept with time varying trend and acceleration is specified as ``order=3,
71
+ innovations_order=[0, 1, 1]``.
72
+
73
+ By choosing the ``order`` and ``innovations_order``, a large variety of models can be obtained. Notable
74
+ models include:
75
+
76
+ * Constant intercept, ``order=1, innovations_order=0``
77
+
78
+ .. math::
79
+ \mu_t = \mu
80
+
81
+ * Constant linear slope, ``order=2, innovations_order=0``
82
+
83
+ .. math::
84
+ \mu_t = \mu_{t-1} + \nu
85
+
86
+ * Gaussian Random Walk, ``order=1, innovations_order=1``
87
+
88
+ .. math::
89
+ \mu_t = \mu_{t-1} + \zeta_t
90
+
91
+ * Gaussian Random Walk with Drift, ``order=2, innovations_order=1``
92
+
93
+ .. math::
94
+ \mu_t = \mu_{t-1} + \nu + \zeta_t
95
+
96
+ * Smooth Trend, ``order=2, innovations_order=[0, 1]``
97
+
98
+ .. math::
99
+ \begin{align}
100
+ \mu_t &= \mu_{t-1} + \nu_{t-1} \\
101
+ \nu_t &= \nu_{t-1} + \xi_t
102
+ \end{align}
103
+
104
+ * Local Level, ``order=2, innovations_order=2``
105
+
106
+ [1] notes that the smooth trend model produces more gradually changing slopes than the full local linear trend
107
+ model, and is equivalent to an "integrated trend model".
108
+
109
+ References
110
+ ----------
111
+ .. [1] Durbin, James, and Siem Jan Koopman. 2012.
112
+ Time Series Analysis by State Space Methods: Second Edition.
113
+ Oxford University Press.
114
+
115
+ """
116
+
117
+ def __init__(
118
+ self,
119
+ order: int | list[int] = 2,
120
+ innovations_order: int | list[int] | None = None,
121
+ name: str = "level_trend",
122
+ observed_state_names: list[str] | None = None,
123
+ ):
124
+ if innovations_order is None:
125
+ innovations_order = order
126
+
127
+ if observed_state_names is None:
128
+ observed_state_names = ["data"]
129
+ k_endog = len(observed_state_names)
130
+
131
+ self._order_mask = order_to_mask(order)
132
+ max_state = np.flatnonzero(self._order_mask)[-1].item() + 1
133
+
134
+ # If the user passes excess zeros, raise an error. The alternative is to prune them, but this would cause
135
+ # the shape of the state to be different to what the user expects.
136
+ if len(self._order_mask) > max_state:
137
+ raise ValueError(
138
+ f"order={order} is invalid. The highest derivative should not be set to zero. If you want a "
139
+ f"lower order model, explicitly omit the zeros."
140
+ )
141
+ k_states = max_state
142
+
143
+ if isinstance(innovations_order, int):
144
+ n = innovations_order
145
+ innovations_order = order_to_mask(k_states)
146
+ if n > 0:
147
+ innovations_order[n:] = False
148
+ else:
149
+ innovations_order[:] = False
150
+ else:
151
+ innovations_order = order_to_mask(innovations_order)
152
+
153
+ self.innovations_order = innovations_order[:max_state]
154
+ k_posdef = int(sum(innovations_order))
155
+
156
+ super().__init__(
157
+ name,
158
+ k_endog=k_endog,
159
+ k_states=k_states * k_endog,
160
+ k_posdef=k_posdef * k_endog,
161
+ observed_state_names=observed_state_names,
162
+ measurement_error=False,
163
+ combine_hidden_states=False,
164
+ obs_state_idxs=np.tile(np.array([1.0] + [0.0] * (k_states - 1)), k_endog),
165
+ )
166
+
167
+ def populate_component_properties(self):
168
+ k_endog = self.k_endog
169
+ k_states = self.k_states // k_endog
170
+ k_posdef = self.k_posdef // k_endog
171
+
172
+ name_slice = POSITION_DERIVATIVE_NAMES[:k_states]
173
+ self.param_names = [f"initial_{self.name}"]
174
+ base_names = [name for name, mask in zip(name_slice, self._order_mask) if mask]
175
+ self.state_names = [
176
+ f"{name}[{obs_name}]" for obs_name in self.observed_state_names for name in base_names
177
+ ]
178
+ self.param_dims = {f"initial_{self.name}": (f"state_{self.name}",)}
179
+ self.coords = {f"state_{self.name}": base_names}
180
+
181
+ if k_endog > 1:
182
+ self.param_dims[f"state_{self.name}"] = (
183
+ f"endog_{self.name}",
184
+ f"state_{self.name}",
185
+ )
186
+ self.param_dims = {f"initial_{self.name}": (f"endog_{self.name}", f"state_{self.name}")}
187
+ self.coords[f"endog_{self.name}"] = self.observed_state_names
188
+
189
+ shape = (k_endog, k_states) if k_endog > 1 else (k_states,)
190
+ self.param_info = {f"initial_{self.name}": {"shape": shape, "constraints": None}}
191
+
192
+ if self.k_posdef > 0:
193
+ self.param_names += [f"sigma_{self.name}"]
194
+
195
+ base_shock_names = [
196
+ name for name, mask in zip(name_slice, self.innovations_order) if mask
197
+ ]
198
+
199
+ self.shock_names = [
200
+ f"{name}[{obs_name}]"
201
+ for obs_name in self.observed_state_names
202
+ for name in base_shock_names
203
+ ]
204
+
205
+ self.param_dims[f"sigma_{self.name}"] = (
206
+ (f"shock_{self.name}",)
207
+ if k_endog == 1
208
+ else (f"endog_{self.name}", f"shock_{self.name}")
209
+ )
210
+ self.coords[f"shock_{self.name}"] = base_shock_names
211
+ self.param_info[f"sigma_{self.name}"] = {
212
+ "shape": (k_posdef,) if k_endog == 1 else (k_endog, k_posdef),
213
+ "constraints": "Positive",
214
+ }
215
+
216
+ for name in self.param_names:
217
+ self.param_info[name]["dims"] = self.param_dims[name]
218
+
219
+ def make_symbolic_graph(self) -> None:
220
+ k_endog = self.k_endog
221
+ k_states = self.k_states // k_endog
222
+ k_posdef = self.k_posdef // k_endog
223
+
224
+ initial_trend = self.make_and_register_variable(
225
+ f"initial_{self.name}",
226
+ shape=(k_states,) if k_endog == 1 else (k_endog, k_states),
227
+ )
228
+ self.ssm["initial_state", :] = initial_trend.ravel()
229
+
230
+ triu_idx = pt.triu_indices(k_states)
231
+ T = pt.zeros((k_states, k_states))[triu_idx[0], triu_idx[1]].set(1)
232
+
233
+ self.ssm["transition", :, :] = pt.specify_shape(
234
+ pt.linalg.block_diag(*[T for _ in range(k_endog)]), (self.k_states, self.k_states)
235
+ )
236
+
237
+ R = np.eye(k_states)
238
+ R = R[:, self.innovations_order]
239
+
240
+ self.ssm["selection", :, :] = pt.specify_shape(
241
+ pt.linalg.block_diag(*[R for _ in range(k_endog)]), (self.k_states, self.k_posdef)
242
+ )
243
+
244
+ Z = np.array([1.0] + [0.0] * (k_states - 1)).reshape((1, -1))
245
+
246
+ self.ssm["design", :, :] = pt.specify_shape(
247
+ pt.linalg.block_diag(*[Z for _ in range(k_endog)]), (self.k_endog, self.k_states)
248
+ )
249
+
250
+ if k_posdef > 0:
251
+ sigma_trend = self.make_and_register_variable(
252
+ f"sigma_{self.name}",
253
+ shape=(k_posdef,) if k_endog == 1 else (k_endog, k_posdef),
254
+ )
255
+ diag_idx = np.diag_indices(k_posdef * k_endog)
256
+ idx = np.s_["state_cov", diag_idx[0], diag_idx[1]]
257
+ self.ssm[idx] = (sigma_trend**2).ravel()
@@ -0,0 +1,137 @@
1
+ import numpy as np
2
+
3
+ from pymc_extras.statespace.models.structural.core import Component
4
+
5
+
6
+ class MeasurementError(Component):
7
+ r"""
8
+ Measurement error component for structural time series models.
9
+
10
+ This component adds observation noise to the model by introducing a variance parameter
11
+ that affects the observation covariance matrix H. Unlike other components, it has no
12
+ hidden states and should only be used in combination with other components.
13
+
14
+ Parameters
15
+ ----------
16
+ name : str, optional
17
+ Name of the measurement error component. Default is "MeasurementError".
18
+ observed_state_names : list[str] | None, optional
19
+ Names of the observed variables. If None, defaults to ["data"].
20
+
21
+ Notes
22
+ -----
23
+ The measurement error component models observation noise as:
24
+
25
+ .. math::
26
+
27
+ y_t = \text{signal}_t + \varepsilon_t, \quad \varepsilon_t \sim N(0, \sigma^2)
28
+
29
+ Where :math:`\text{signal}_t` is the true signal from other components and
30
+ :math:`\sigma^2` is the measurement error variance.
31
+
32
+ This component:
33
+ - Has no hidden states (k_states = 0)
34
+ - Has no innovations (k_posdef = 0)
35
+ - Adds a single parameter: sigma_{name}
36
+ - Modifies the observation covariance matrix H
37
+
38
+ Examples
39
+ --------
40
+ **Basic usage with trend component:**
41
+
42
+ .. code:: python
43
+
44
+ from pymc_extras.statespace import structural as st
45
+ import pymc as pm
46
+ import pytensor.tensor as pt
47
+
48
+ trend = st.LevelTrendComponent(order=2, innovations_order=1)
49
+ error = st.MeasurementError()
50
+
51
+ ss_mod = (trend + error).build()
52
+
53
+ # Use with PyMC
54
+ with pm.Model(coords=ss_mod.coords) as model:
55
+ P0 = pm.Deterministic('P0', pt.eye(ss_mod.k_states) * 10, dims=ss_mod.param_dims['P0'])
56
+ initial_trend = pm.Normal('initial_trend', sigma=10, dims=ss_mod.param_dims['initial_trend'])
57
+ sigma_obs = pm.Exponential('sigma_obs', 1, dims=ss_mod.param_dims['sigma_obs'])
58
+
59
+ ss_mod.build_statespace_graph(data)
60
+ idata = pm.sample()
61
+
62
+ **Multivariate measurement error:**
63
+
64
+ .. code:: python
65
+
66
+ # For multiple observed variables
67
+ # This creates separate measurement error variances for each variable
68
+ # sigma_obs_error will have shape (3,) for the three variables
69
+ error = st.MeasurementError(
70
+ name="obs_error",
71
+ observed_state_names=["gdp", "unemployment", "inflation"]
72
+ )
73
+
74
+ **Complete model example:**
75
+
76
+ .. code:: python
77
+
78
+ trend = st.LevelTrendComponent(order=2, innovations_order=1)
79
+ seasonal = st.TimeSeasonality(season_length=12, innovations=True)
80
+ error = st.MeasurementError()
81
+
82
+ model = (trend + seasonal + error).build()
83
+
84
+ # The model now includes:
85
+ # - Trend parameters: level_trend, sigma_trend
86
+ # - Seasonal parameters: seasonal_coefs, sigma_seasonal
87
+ # - Measurement error parameter: sigma_obs
88
+
89
+ See Also
90
+ --------
91
+ Component : Base class for all structural components.
92
+ StructuralTimeSeries : Complete model class.
93
+ """
94
+
95
+ def __init__(
96
+ self, name: str = "MeasurementError", observed_state_names: list[str] | None = None
97
+ ):
98
+ if observed_state_names is None:
99
+ observed_state_names = ["data"]
100
+
101
+ k_endog = len(observed_state_names)
102
+ k_states = 0
103
+ k_posdef = 0
104
+
105
+ super().__init__(
106
+ name,
107
+ k_endog,
108
+ k_states,
109
+ k_posdef,
110
+ measurement_error=True,
111
+ combine_hidden_states=False,
112
+ observed_state_names=observed_state_names,
113
+ )
114
+
115
+ def populate_component_properties(self):
116
+ self.param_names = [f"sigma_{self.name}"]
117
+ self.param_dims = {}
118
+ self.coords = {}
119
+
120
+ if self.k_endog > 1:
121
+ self.param_dims[f"sigma_{self.name}"] = (f"endog_{self.name}",)
122
+ self.coords[f"endog_{self.name}"] = self.observed_state_names
123
+
124
+ self.param_info = {
125
+ f"sigma_{self.name}": {
126
+ "shape": (self.k_endog,) if self.k_endog > 1 else (),
127
+ "constraints": "Positive",
128
+ "dims": (f"endog_{self.name}",) if self.k_endog > 1 else None,
129
+ }
130
+ }
131
+
132
+ def make_symbolic_graph(self) -> None:
133
+ sigma_shape = () if self.k_endog == 1 else (self.k_endog,)
134
+ error_sigma = self.make_and_register_variable(f"sigma_{self.name}", shape=sigma_shape)
135
+ diag_idx = np.diag_indices(self.k_endog)
136
+ idx = np.s_["obs_cov", diag_idx[0], diag_idx[1]]
137
+ self.ssm[idx] = error_sigma**2