pymc-extras 0.2.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 (101) hide show
  1. pymc_extras/__init__.py +29 -0
  2. pymc_extras/distributions/__init__.py +40 -0
  3. pymc_extras/distributions/continuous.py +351 -0
  4. pymc_extras/distributions/discrete.py +399 -0
  5. pymc_extras/distributions/histogram_utils.py +163 -0
  6. pymc_extras/distributions/multivariate/__init__.py +3 -0
  7. pymc_extras/distributions/multivariate/r2d2m2cp.py +446 -0
  8. pymc_extras/distributions/timeseries.py +356 -0
  9. pymc_extras/gp/__init__.py +18 -0
  10. pymc_extras/gp/latent_approx.py +183 -0
  11. pymc_extras/inference/__init__.py +18 -0
  12. pymc_extras/inference/find_map.py +431 -0
  13. pymc_extras/inference/fit.py +44 -0
  14. pymc_extras/inference/laplace.py +570 -0
  15. pymc_extras/inference/pathfinder.py +134 -0
  16. pymc_extras/inference/smc/__init__.py +13 -0
  17. pymc_extras/inference/smc/sampling.py +451 -0
  18. pymc_extras/linearmodel.py +130 -0
  19. pymc_extras/model/__init__.py +0 -0
  20. pymc_extras/model/marginal/__init__.py +0 -0
  21. pymc_extras/model/marginal/distributions.py +276 -0
  22. pymc_extras/model/marginal/graph_analysis.py +372 -0
  23. pymc_extras/model/marginal/marginal_model.py +595 -0
  24. pymc_extras/model/model_api.py +56 -0
  25. pymc_extras/model/transforms/__init__.py +0 -0
  26. pymc_extras/model/transforms/autoreparam.py +434 -0
  27. pymc_extras/model_builder.py +759 -0
  28. pymc_extras/preprocessing/__init__.py +0 -0
  29. pymc_extras/preprocessing/standard_scaler.py +17 -0
  30. pymc_extras/printing.py +182 -0
  31. pymc_extras/statespace/__init__.py +13 -0
  32. pymc_extras/statespace/core/__init__.py +7 -0
  33. pymc_extras/statespace/core/compile.py +48 -0
  34. pymc_extras/statespace/core/representation.py +438 -0
  35. pymc_extras/statespace/core/statespace.py +2268 -0
  36. pymc_extras/statespace/filters/__init__.py +15 -0
  37. pymc_extras/statespace/filters/distributions.py +453 -0
  38. pymc_extras/statespace/filters/kalman_filter.py +820 -0
  39. pymc_extras/statespace/filters/kalman_smoother.py +126 -0
  40. pymc_extras/statespace/filters/utilities.py +59 -0
  41. pymc_extras/statespace/models/ETS.py +670 -0
  42. pymc_extras/statespace/models/SARIMAX.py +536 -0
  43. pymc_extras/statespace/models/VARMAX.py +393 -0
  44. pymc_extras/statespace/models/__init__.py +6 -0
  45. pymc_extras/statespace/models/structural.py +1651 -0
  46. pymc_extras/statespace/models/utilities.py +387 -0
  47. pymc_extras/statespace/utils/__init__.py +0 -0
  48. pymc_extras/statespace/utils/constants.py +74 -0
  49. pymc_extras/statespace/utils/coord_tools.py +0 -0
  50. pymc_extras/statespace/utils/data_tools.py +182 -0
  51. pymc_extras/utils/__init__.py +23 -0
  52. pymc_extras/utils/linear_cg.py +290 -0
  53. pymc_extras/utils/pivoted_cholesky.py +69 -0
  54. pymc_extras/utils/prior.py +200 -0
  55. pymc_extras/utils/spline.py +131 -0
  56. pymc_extras/version.py +11 -0
  57. pymc_extras/version.txt +1 -0
  58. pymc_extras-0.2.0.dist-info/LICENSE +212 -0
  59. pymc_extras-0.2.0.dist-info/METADATA +99 -0
  60. pymc_extras-0.2.0.dist-info/RECORD +101 -0
  61. pymc_extras-0.2.0.dist-info/WHEEL +5 -0
  62. pymc_extras-0.2.0.dist-info/top_level.txt +2 -0
  63. tests/__init__.py +13 -0
  64. tests/distributions/__init__.py +19 -0
  65. tests/distributions/test_continuous.py +185 -0
  66. tests/distributions/test_discrete.py +210 -0
  67. tests/distributions/test_discrete_markov_chain.py +258 -0
  68. tests/distributions/test_multivariate.py +304 -0
  69. tests/model/__init__.py +0 -0
  70. tests/model/marginal/__init__.py +0 -0
  71. tests/model/marginal/test_distributions.py +131 -0
  72. tests/model/marginal/test_graph_analysis.py +182 -0
  73. tests/model/marginal/test_marginal_model.py +867 -0
  74. tests/model/test_model_api.py +29 -0
  75. tests/statespace/__init__.py +0 -0
  76. tests/statespace/test_ETS.py +411 -0
  77. tests/statespace/test_SARIMAX.py +405 -0
  78. tests/statespace/test_VARMAX.py +184 -0
  79. tests/statespace/test_coord_assignment.py +116 -0
  80. tests/statespace/test_distributions.py +270 -0
  81. tests/statespace/test_kalman_filter.py +326 -0
  82. tests/statespace/test_representation.py +175 -0
  83. tests/statespace/test_statespace.py +818 -0
  84. tests/statespace/test_statespace_JAX.py +156 -0
  85. tests/statespace/test_structural.py +829 -0
  86. tests/statespace/utilities/__init__.py +0 -0
  87. tests/statespace/utilities/shared_fixtures.py +9 -0
  88. tests/statespace/utilities/statsmodel_local_level.py +42 -0
  89. tests/statespace/utilities/test_helpers.py +310 -0
  90. tests/test_blackjax_smc.py +222 -0
  91. tests/test_find_map.py +98 -0
  92. tests/test_histogram_approximation.py +109 -0
  93. tests/test_laplace.py +238 -0
  94. tests/test_linearmodel.py +208 -0
  95. tests/test_model_builder.py +306 -0
  96. tests/test_pathfinder.py +45 -0
  97. tests/test_pivoted_cholesky.py +24 -0
  98. tests/test_printing.py +98 -0
  99. tests/test_prior_from_trace.py +172 -0
  100. tests/test_splines.py +77 -0
  101. tests/utils.py +31 -0
@@ -0,0 +1,2268 @@
1
+ import logging
2
+
3
+ from collections.abc import Callable, Sequence
4
+ from typing import Any
5
+
6
+ import numpy as np
7
+ import pandas as pd
8
+ import pymc as pm
9
+ import pytensor
10
+ import pytensor.tensor as pt
11
+
12
+ from arviz import InferenceData
13
+ from pymc.model import modelcontext
14
+ from pymc.model.transform.optimization import freeze_dims_and_data
15
+ from pymc.util import RandomState
16
+ from pytensor import Variable, graph_replace
17
+ from pytensor.compile import get_mode
18
+
19
+ from pymc_extras.statespace.core.representation import PytensorRepresentation
20
+ from pymc_extras.statespace.filters import (
21
+ KalmanSmoother,
22
+ SquareRootFilter,
23
+ StandardFilter,
24
+ UnivariateFilter,
25
+ )
26
+ from pymc_extras.statespace.filters.distributions import (
27
+ LinearGaussianStateSpace,
28
+ MvNormalSVD,
29
+ SequenceMvNormal,
30
+ )
31
+ from pymc_extras.statespace.filters.utilities import stabilize
32
+ from pymc_extras.statespace.utils.constants import (
33
+ ALL_STATE_AUX_DIM,
34
+ ALL_STATE_DIM,
35
+ FILTER_OUTPUT_DIMS,
36
+ FILTER_OUTPUT_TYPES,
37
+ JITTER_DEFAULT,
38
+ MATRIX_DIMS,
39
+ MATRIX_NAMES,
40
+ OBS_STATE_DIM,
41
+ SHOCK_DIM,
42
+ SHORT_NAME_TO_LONG,
43
+ TIME_DIM,
44
+ VECTOR_VALUED,
45
+ )
46
+ from pymc_extras.statespace.utils.data_tools import register_data_with_pymc
47
+
48
+ _log = logging.getLogger("pymc.experimental.statespace")
49
+
50
+ floatX = pytensor.config.floatX
51
+ FILTER_FACTORY = {
52
+ "standard": StandardFilter,
53
+ "univariate": UnivariateFilter,
54
+ "cholesky": SquareRootFilter,
55
+ }
56
+
57
+
58
+ def _validate_filter_arg(filter_arg):
59
+ if filter_arg.lower() not in FILTER_OUTPUT_TYPES:
60
+ raise ValueError(
61
+ f'filter_output should be one of {", ".join(FILTER_OUTPUT_TYPES)}, received {filter_arg}'
62
+ )
63
+
64
+
65
+ def _verify_group(group):
66
+ if group not in ["prior", "posterior"]:
67
+ raise ValueError(f'Argument "group" must be one of "prior" or "posterior", found {group}')
68
+
69
+
70
+ class PyMCStateSpace:
71
+ r"""
72
+ Base class for Linear Gaussian Statespace models in PyMC.
73
+
74
+ Holds a ``PytensorRepresentation`` and ``KalmanFilter``, and provides a mapping between a PyMC model and the
75
+ statespace model.
76
+
77
+ Parameters
78
+ ----------
79
+ k_endog : int
80
+ The number of endogenous variables (observed time series).
81
+
82
+ k_states : int
83
+ The number of state variables.
84
+
85
+ k_posdef : int
86
+ The number of shocks in the model
87
+
88
+ filter_type : str, optional
89
+ The type of Kalman filter to use. Valid options are "standard", "univariate", "single", "cholesky", and
90
+ "steady_state". For more information, see the docs for each filter. Default is "standard".
91
+
92
+ verbose : bool, optional
93
+ If True, displays information about the initialized model. Defaults to True.
94
+
95
+ measurement_error : bool, optional
96
+ If true, the model contains measurement error. Needed by post-estimation sampling methods to decide how to
97
+ compute the observation errors. If False, these errors are deterministically zero; if True, they are sampled
98
+ from a multivariate normal.
99
+
100
+ Notes
101
+ -----
102
+ Based on the statsmodels statespace implementation https://github.com/statsmodels/statsmodels/blob/main/statsmodels/tsa/statespace/representation.py,
103
+ described in [1].
104
+
105
+ All statespace models inherit from this base class, which is responsible for providing an interface between a
106
+ PyMC model and a PytensorRepresentation of a linear statespace model. This is done via the ``update`` method,
107
+ which takes as input a vector of PyMC random variables and assigns them to their correct positions inside the
108
+ underlying ``PytensorRepresentation``. Construction of the parameter vector, called ``theta``, is done
109
+ automatically, but depend on the names provided in the ``param_names`` property.
110
+
111
+ To implement a new statespace model, one needs to:
112
+
113
+ 1. Overload the ``param_names`` property to return a list of parameter names.
114
+ 2. Overload the ``update`` method to put these parameters into their respective statespace matrices
115
+
116
+ In addition, a number of additional properties can be overloaded to provide users with additional resources
117
+ when writing their PyMC models. For details, see the attributes section of the docs for this class.
118
+
119
+ Finally, this class holds post-estimation methods common to all statespace models, which do not need to be
120
+ overloaded when writing a custom statespace model.
121
+
122
+ Examples
123
+ --------
124
+ The local level model is a simple statespace model. It is a Gaussian random walk with a drift term that itself also
125
+ follows a Gaussian random walk, as described by the following two equations:
126
+
127
+ .. math::
128
+ \begin{align}
129
+ y_{t} &= y_{t-1} + x_t + \nu_t \tag{1} \\
130
+ x_{t} &= x_{t-1} + \eta_t \tag{2}
131
+ \end{align}
132
+
133
+ Where :math:`y_t` is the observed data, and :math:`x_t` is an unobserved trend term. The model has two unknown
134
+ parameters, the variances on the two innovations, :math:`sigma_\nu` and :math:`sigma_\eta`. Take the hidden state
135
+ vector to be :math:`\begin{bmatrix} y_t & x_t \end{bmatrix}^T` and the shock vector
136
+ :math:`\varepsilon_t = \begin{bmatrix} \nu_t & \eta_t \end{bmatrix}^T`. Then this model can be cast into state-space
137
+ form with the following matrices:
138
+
139
+ .. math::
140
+ \begin{align}
141
+ T &= \begin{bmatrix}1 & 1 \\ 0 & 1 \end{bmatrix} &
142
+ R &= \begin{bmatrix}1 & 0 \\ 0 & 1 \end{bmatrix} &
143
+ Q &= \begin{bmatrix} \sigma_\nu & 0 \\ 0 & \sigma_\eta \end{bmatrix} &
144
+ Z &= \begin{bmatrix} 1 & 0 \end{bmatrix}
145
+ \end{align}
146
+
147
+ With the remaining statespace matrices as zero matrices of the appropriate sizes. The model has two states,
148
+ two shocks, and one observed state. Knowing all this, a very simple local level model can be implemented as
149
+ follows:
150
+
151
+ .. code:: python
152
+
153
+ from pymc_extras.statespace.core import PyMCStateSpace
154
+ import numpy as np
155
+
156
+ class LocalLevel(PyMCStateSpace):
157
+ def __init__():
158
+ # Initialize the superclass. This creates the PytensorRepresentation and the Kalman Filter
159
+ super().__init__(k_endog=1, k_states=2, k_posdef=2)
160
+
161
+ # Declare the non-zero, non-parameterized matrices
162
+ self.ssm['transition', :, :] = np.array([[1.0, 1.0], [0.0, 1.0]])
163
+ self.ssm['selection', :, :] = np.eye(2)
164
+ self.ssm['design', :, :] = np.array([[1.0, 0.0]])
165
+
166
+ @property
167
+ def param_names(self):
168
+ return ['x0', 'P0', 'sigma_nu', 'sigma_eta']
169
+
170
+ def make_symbolic_graph(self):
171
+ # Declare symbolic variables that represent parameters of the model
172
+ # In this case, we have 4: x0 (initial state), P0 (initial state covariance), sigma_nu, and sigma_eta
173
+
174
+ x0 = self.make_and_register_variable('x0', shape=(2,))
175
+ P0 = self.make_and_register_variable('P0', shape=(2,2))
176
+ sigma_mu = self.make_and_register_variable('sigma_nu')
177
+ sigma_eta = self.make_and_register_variable('sigma_eta')
178
+
179
+ # Next, use these symbolic variables to build the statespace matrices by assigning each parameter
180
+ # to its correct location in the correct matrix
181
+
182
+ self.ssm['initial_state', :] = x0
183
+ self.ssm['initial_state_cov', :, :] = P0
184
+ self.ssm['state_cov', 0, 0] = sigma_nu
185
+ self.ssm['state_cov', 1, 1] = sigma_eta
186
+
187
+ After defining priors over the named parameters ``P0``, ``x0``, ``sigma_eta``, and ``sigma_nu``, we can sample
188
+ from this model:
189
+
190
+ .. code:: python
191
+
192
+ import pymc as pm
193
+
194
+ ll = LocalLevel()
195
+
196
+ with pm.Model() as mod:
197
+ x0 = pm.Normal('x0', shape=(2,))
198
+ P0_diag = pm.Exponential('P0_diag', 1, shape=(2,))
199
+ P0 = pm.Deterministic('P0', pt.diag(P0_diag))
200
+ sigma_nu = pm.Exponential('sigma_nu', 1)
201
+ sigma_eta = pm.Exponential('sigma_eta', 1)
202
+
203
+ ll.build_statespace_graph(data = data)
204
+ idata = pm.sample()
205
+
206
+
207
+ References
208
+ ----------
209
+ .. [1] Fulton, Chad. "Estimating time series models by state space methods in Python: Statsmodels." (2015).
210
+ http://www.chadfulton.com/files/fulton_statsmodels_2017_v1.pdf
211
+
212
+ """
213
+
214
+ def __init__(
215
+ self,
216
+ k_endog: int,
217
+ k_states: int,
218
+ k_posdef: int,
219
+ filter_type: str = "standard",
220
+ verbose: bool = True,
221
+ measurement_error: bool = False,
222
+ ):
223
+ self._fit_mode: str | None = None
224
+ self._fit_coords: dict[str, Sequence[str]] | None = None
225
+ self._fit_dims: dict[str, Sequence[str]] | None = None
226
+ self._fit_data: pt.TensorVariable | None = None
227
+
228
+ self._needs_exog_data = None
229
+ self._exog_names = []
230
+ self._exog_data_info = {}
231
+ self._name_to_variable = {}
232
+ self._name_to_data = {}
233
+
234
+ self.k_endog = k_endog
235
+ self.k_states = k_states
236
+ self.k_posdef = k_posdef
237
+ self.measurement_error = measurement_error
238
+
239
+ # All models contain a state space representation and a Kalman filter
240
+ self.ssm = PytensorRepresentation(k_endog, k_states, k_posdef)
241
+
242
+ # This will be populated with PyMC random matrices after calling _insert_random_variables
243
+ self.subbed_ssm: list[pt.TensorVariable] | None = None
244
+
245
+ if filter_type.lower() not in FILTER_FACTORY.keys():
246
+ raise NotImplementedError(
247
+ "The following are valid filter types: " + ", ".join(list(FILTER_FACTORY.keys()))
248
+ )
249
+
250
+ if filter_type == "single" and self.k_endog > 1:
251
+ raise ValueError('Cannot use filter_type = "single" with multiple observed time series')
252
+
253
+ self.kalman_filter = FILTER_FACTORY[filter_type.lower()]()
254
+ self.kalman_smoother = KalmanSmoother()
255
+ self.make_symbolic_graph()
256
+
257
+ if verbose:
258
+ # These are split into separate try-except blocks, because it will be quite rare of models to implement
259
+ # _print_data_requirements, but we still want to print the prior requirements.
260
+ try:
261
+ self._print_prior_requirements()
262
+ except NotImplementedError:
263
+ pass
264
+ try:
265
+ self._print_data_requirements()
266
+ except NotImplementedError:
267
+ pass
268
+
269
+ def _print_prior_requirements(self) -> None:
270
+ """
271
+ Prints a short report to the terminal about the priors needed for the model, including their names,
272
+ shapes, named dimensions, and any parameter constraints.
273
+ """
274
+ out = ""
275
+ for param, info in self.param_info.items():
276
+ out += f'\t{param} -- shape: {info["shape"]}, constraints: {info["constraints"]}, dims: {info["dims"]}\n'
277
+ out = out.rstrip()
278
+
279
+ _log.info(
280
+ "The following parameters should be assigned priors inside a PyMC "
281
+ f"model block: \n"
282
+ f"{out}"
283
+ )
284
+
285
+ def _print_data_requirements(self) -> None:
286
+ """
287
+ Prints a short report to the terminal about the data needed for the model, including their names, shapes,
288
+ and named dimensions.
289
+ """
290
+ if not self.data_info:
291
+ return
292
+
293
+ out = ""
294
+ for data, info in self.data_info.items():
295
+ out += f'\t{data} -- shape: {info["shape"]}, dims: {info["dims"]}\n'
296
+ out = out.rstrip()
297
+
298
+ _log.info(
299
+ "The following Data variables should be assigned to the model inside a PyMC "
300
+ f"model block: \n"
301
+ f"{out}"
302
+ )
303
+
304
+ def _unpack_statespace_with_placeholders(
305
+ self,
306
+ ) -> tuple[
307
+ pt.TensorVariable,
308
+ pt.TensorVariable,
309
+ pt.TensorVariable,
310
+ pt.TensorVariable,
311
+ pt.TensorVariable,
312
+ pt.TensorVariable,
313
+ pt.TensorVariable,
314
+ pt.TensorVariable,
315
+ pt.TensorVariable,
316
+ ]:
317
+ """
318
+ Helper function to quickly obtain all statespace matrices in the standard order. Matrices returned by this
319
+ method will include pytensor placeholders.
320
+ """
321
+
322
+ a0 = self.ssm["initial_state"]
323
+ P0 = self.ssm["initial_state_cov"]
324
+ c = self.ssm["state_intercept"]
325
+ d = self.ssm["obs_intercept"]
326
+ T = self.ssm["transition"]
327
+ Z = self.ssm["design"]
328
+ R = self.ssm["selection"]
329
+ H = self.ssm["obs_cov"]
330
+ Q = self.ssm["state_cov"]
331
+
332
+ return a0, P0, c, d, T, Z, R, H, Q
333
+
334
+ def unpack_statespace(self) -> list[pt.TensorVariable]:
335
+ """
336
+ Helper function to quickly obtain all statespace matrices in the standard order.
337
+ """
338
+
339
+ if self.subbed_ssm is None:
340
+ raise ValueError(
341
+ "Cannot unpack the complete statespace system until PyMC model variables have been "
342
+ "inserted. To build the random statespace matrices, call build_statespace_graph() inside"
343
+ "a PyMC model context. "
344
+ )
345
+
346
+ return self.subbed_ssm
347
+
348
+ @property
349
+ def param_names(self) -> list[str]:
350
+ """
351
+ Names of model parameters
352
+
353
+ A list of all parameters expected by the model. Each parameter will be sought inside the active PyMC model
354
+ context when ``build_statespace_graph`` is invoked.
355
+ """
356
+ raise NotImplementedError("The param_names property has not been implemented!")
357
+
358
+ @property
359
+ def data_names(self) -> list[str]:
360
+ """
361
+ Names of data variables expected by the model.
362
+
363
+ This does not include the observed data series, which is automatically handled by PyMC. This property only
364
+ needs to be implemented for models that expect exogenous data.
365
+ """
366
+ return []
367
+
368
+ @property
369
+ def param_info(self) -> dict[str, dict[str, Any]]:
370
+ """
371
+ Information about parameters needed to declare priors
372
+
373
+ A dictionary of param_name: dictionary key-value pairs. The return value is used by the
374
+ ``_print_prior_requirements`` method, to print a message telling users how to define the necessary priors for
375
+ the model. Each dictionary should have the following key-value pairs:
376
+ * key: "shape", value: a tuple of integers
377
+ * key: "constraints", value: a string describing the support of the prior (positive,
378
+ positive semi-definite, etc)
379
+ * key: "dims", value: tuple of strings
380
+ """
381
+ raise NotImplementedError("The params_info property has not been implemented!")
382
+
383
+ @property
384
+ def data_info(self) -> dict[str, dict[str, Any]]:
385
+ """
386
+ Information about Data variables that need to be declared in the PyMC model block.
387
+
388
+ Returns a dictionary of data_name: dictionary of property-name:property description pairs. The return value is
389
+ used by the ``_print_data_requirements`` method, to print a message telling users how to define the necessary
390
+ data for the model. Each dictionary should have the following key-value pairs:
391
+ * key: "shape", value: a tuple of integers
392
+ * key: "dims", value: tuple of strings
393
+ """
394
+ raise NotImplementedError("The data_info property has not been implemented!")
395
+
396
+ @property
397
+ def state_names(self) -> list[str]:
398
+ """
399
+ A k_states length list of strings, associated with the model's hidden states
400
+
401
+ """
402
+
403
+ raise NotImplementedError("The state_names property has not been implemented!")
404
+
405
+ @property
406
+ def observed_states(self) -> list[str]:
407
+ """
408
+ A k_endog length list of strings, associated with the model's observed states
409
+ """
410
+ raise NotImplementedError("The observed_states property has not been implemented!")
411
+
412
+ @property
413
+ def shock_names(self) -> list[str]:
414
+ """
415
+ A k_posdef length list of strings, associated with the model's shock processes
416
+
417
+ """
418
+ raise NotImplementedError("The shock_names property has not been implemented!")
419
+
420
+ @property
421
+ def default_priors(self) -> dict[str, Callable]:
422
+ """
423
+ Dictionary of parameter names and callable functions to construct default priors for the model
424
+
425
+ Returns a dictionary with param_name: Callable key-value pairs. Used by the ``add_default_priors()`` method
426
+ to automatically add priors to the PyMC model.
427
+ """
428
+ raise NotImplementedError("The default_priors property has not been implemented!")
429
+
430
+ @property
431
+ def coords(self) -> dict[str, Sequence[str]]:
432
+ """
433
+ PyMC model coordinates
434
+
435
+ Returns a dictionary of dimension: coordinate key-value pairs, to be provided to ``pm.Model``. Dimensions
436
+ should come from the default names defined in ``statespace.utils.constants`` for them to be detected by
437
+ sampling methods.
438
+ """
439
+ raise NotImplementedError("The coords property has not been implemented!")
440
+
441
+ @property
442
+ def param_dims(self) -> dict[str, Sequence[str]]:
443
+ """
444
+ Dictionary of named dimensions for each model parameter
445
+
446
+ Returns a dictionary of param_name: dimension key-value pairs, to be provided to the ``dims`` argument of a
447
+ PyMC random variable. Dimensions should come from the default names defined in ``statespace.utils.constants``
448
+ for them to be detected by sampling methods.
449
+
450
+ """
451
+ raise NotImplementedError("The param_dims property has not been implemented!")
452
+
453
+ def add_default_priors(self) -> None:
454
+ """
455
+ Add default priors to the active PyMC model context
456
+ """
457
+ raise NotImplementedError("The add_default_priors property has not been implemented!")
458
+
459
+ def make_and_register_variable(
460
+ self, name, shape: int | tuple[int, ...] | None = None, dtype=floatX
461
+ ) -> pt.TensorVariable:
462
+ """
463
+ Helper function to create a pytensor symbolic variable and register it in the _name_to_variable dictionary
464
+
465
+ Parameters
466
+ ----------
467
+ name : str
468
+ The name of the placeholder variable. Must be the name of a model parameter.
469
+ shape : int or tuple of int
470
+ Shape of the parameter
471
+ dtype : str, default pytensor.config.floatX
472
+ dtype of the parameter
473
+
474
+ Notes
475
+ -----
476
+ Symbolic pytensor variables are used in the ``make_symbolic_graph`` method as placeholders for PyMC random
477
+ variables. The change is made in the ``_insert_random_variables`` method via ``pytensor.graph_replace``. To
478
+ make the change, a dictionary mapping pytensor variables to PyMC random variables needs to be constructed.
479
+
480
+ The purpose of this method is to:
481
+ 1. Create the placeholder symbolic variables
482
+ 2. Register the placeholder variable in the ``_name_to_variable`` dictionary
483
+
484
+ The shape provided here will define the shape of the prior that will need to be provided by the user.
485
+
486
+ An error is raised if the provided name has already been registered, or if the name is not present in the
487
+ ``param_names`` property.
488
+ """
489
+ if name not in self.param_names:
490
+ raise ValueError(
491
+ f"{name} is not a model parameter. All placeholder variables should correspond to model "
492
+ f"parameters."
493
+ )
494
+
495
+ if name in self._name_to_variable.keys():
496
+ raise ValueError(
497
+ f"{name} is already a registered placeholder variable with shape "
498
+ f"{self._name_to_variable[name].type.shape}"
499
+ )
500
+
501
+ placeholder = pt.tensor(name, shape=shape, dtype=dtype)
502
+ self._name_to_variable[name] = placeholder
503
+ return placeholder
504
+
505
+ def make_and_register_data(
506
+ self, name: str, shape: int | tuple[int], dtype: str = floatX
507
+ ) -> Variable:
508
+ r"""
509
+ Helper function to create a pytensor symbolic variable and register it in the _name_to_data dictionary
510
+
511
+ Parameters
512
+ ----------
513
+ name : str
514
+ The name of the placeholder data. Must be the name of an expected data variable.
515
+ shape : int or tuple of int
516
+ Shape of the parameter
517
+ dtype : str, default pytensor.config.floatX
518
+ dtype of the parameter
519
+
520
+ Notes
521
+ -----
522
+ See docstring for make_and_register_variable for more details. This function is similar, but handles data
523
+ inputs instead of model parameters.
524
+
525
+ An error is raised if the provided name has already been registered, or if the name is not present in the
526
+ ``data_names`` property.
527
+ """
528
+ if name not in self.data_names:
529
+ raise ValueError(
530
+ f"{name} is not a model parameter. All placeholder variables should correspond to model "
531
+ f"parameters."
532
+ )
533
+
534
+ if name in self._name_to_data.keys():
535
+ raise ValueError(
536
+ f"{name} is already a registered placeholder variable with shape "
537
+ f"{self._name_to_data[name].type.shape}"
538
+ )
539
+
540
+ placeholder = pt.tensor(name, shape=shape, dtype=dtype)
541
+ self._name_to_data[name] = placeholder
542
+ return placeholder
543
+
544
+ def make_symbolic_graph(self) -> None:
545
+ """
546
+ The purpose of the make_symbolic_graph function is to hide tedious parameter allocations from the user.
547
+ In statespace models, it is extremely rare for an entire matrix to be defined by a single prior distribution.
548
+ Instead, users expect to place priors over single entries of the matrix. The purpose of this function is to
549
+ meet that expectation.
550
+
551
+ Every statespace model needs to implement this function.
552
+
553
+ Examples
554
+ --------
555
+ As an example, consider an ARMA(2,2) model, which has five parameters (excluding the initial state distribution):
556
+ 2 AR parameters (:math:`\rho_1` and :math:`\rho_2`), 2 MA parameters (:math:`\theta_1` and :math:`theta_2`),
557
+ and a single innovation covariance (:math:`\\sigma`). A common way of writing this statespace is:
558
+
559
+ ..math::
560
+
561
+ \begin{align}
562
+ T &= \begin{bmatrix} \rho_1 & 1 & 0 \\
563
+ \rho_2 & 0 & 1 \\
564
+ 0 & 0 & 0
565
+ \\end{bmatrix} \\
566
+ R & = \begin{bmatrix} 1 \\ \theta_1 \\ \theta_2 \\end{bmatrix} \\
567
+ Q &= \begin{bmatrix} \\sigma \\end{bmatrix}
568
+ \\end{align}
569
+
570
+ To implement this model, we begin by creating the required matrices, and fill in the "fixed" values -- the ones
571
+ at position (0, 1) and (0, 2) in the T matrix, and at position (0, 0) in the R matrix. These are then saved
572
+ to the class's PytensorRepresentation -- called ``ssm``.
573
+
574
+ .. code:: python
575
+
576
+ T = np.eye(2, k=1)
577
+ R = np.concatenate([np.ones(1,1), np.zeros((2, 1))], axis=0)
578
+
579
+ self.ssm['transition'] = T
580
+ self.ssm['selection'] = R
581
+
582
+ Next, placeholders need to be inserted for the random variables rho_1, rho_2, theta_1, theta_2, and sigma.
583
+ This can be done many ways: we could define two vectors, rho and theta, and a scalar for sigma, or five
584
+ scalars. Whatever is chosen, the choice needs to be consistent with the ``param_names`` property.
585
+
586
+ Suppose the ``param_names`` are ``[rho, theta, sigma]``, then we make one placeholder for each, and insert it
587
+ into the correct ``ssm`` matrix, at the correct location. To create placeholders, use the
588
+ ``make_and_register_variable`` helper method, which will maintain an internal registry of variables.
589
+
590
+ .. code:: python
591
+ rho_parmas = self.make_and_register_variable(name='rho', shape=(2,))
592
+ theta_params = self.make_and_register_variable(name='theta', shape=(2,))
593
+ sigma = self.make_and_register_variable(name='sigma', shape=(1,))
594
+
595
+ self.ssm['transition', :, 0] = rho_params
596
+ self.ssm['selection', 1:, 0] = theta_params
597
+ self.ssm['state_cov', 0, 0] = sigma
598
+ """
599
+ raise NotImplementedError("The make_symbolic_statespace method has not been implemented!")
600
+
601
+ def _get_matrix_shape_and_dims(self, name: str) -> tuple[tuple[int] | None, tuple[str] | None]:
602
+ """
603
+ Get the shape and dimensions of a matrix associated with the specified name.
604
+
605
+ This method retrieves the shape and dimensions of a matrix associated with the given name. Importantly,
606
+ it will only find named dimension if they are the "default" dimension names defined in the
607
+ statespace.utils.constant.py file.
608
+
609
+ Parameters
610
+ ----------
611
+ name : str
612
+ The name of the matrix whose shape and dimensions need to be retrieved.
613
+
614
+ Returns
615
+ -------
616
+ shape: tuple or None
617
+ If no named dimension are found, the shape of the requested matrix, otherwise None.
618
+
619
+ dims: tuple or None
620
+ If named dimensions are found, a tuple of strings, otherwise None
621
+ """
622
+
623
+ pm_mod = modelcontext(None)
624
+ dims = MATRIX_DIMS.get(name, None)
625
+ dims = dims if all([dim in pm_mod.coords.keys() for dim in dims]) else None
626
+ data_len = len(self._fit_data)
627
+
628
+ if name in self.kalman_filter.seq_names:
629
+ shape = (data_len, *self.ssm[SHORT_NAME_TO_LONG[name]].type.shape)
630
+ dims = (TIME_DIM, *dims)
631
+ else:
632
+ shape = self.ssm[SHORT_NAME_TO_LONG[name]].type.shape
633
+
634
+ shape = shape if dims is None else None
635
+
636
+ return shape, dims
637
+
638
+ def _save_exogenous_data_info(self):
639
+ """
640
+ Store exogenous data required by posterior sampling functions
641
+ """
642
+ pymc_mod = modelcontext(None)
643
+ for data_name in self.data_names:
644
+ data = pymc_mod[data_name]
645
+ self._exog_data_info[data_name] = {
646
+ "name": data_name,
647
+ "value": data.get_value(),
648
+ "dims": pymc_mod.named_vars_to_dims.get(data_name, None),
649
+ }
650
+
651
+ def _insert_random_variables(self):
652
+ """
653
+ Replace pytensor symbolic variables with PyMC random variables.
654
+
655
+ Examples
656
+ --------
657
+ .. code:: python
658
+
659
+ ss_mod = pmss.BayesianSARIMA(order=(2, 0, 2), verbose=False, stationary_initialization=True)
660
+ with pm.Model():
661
+ x0 = pm.Normal('x0', size=ss_mod.k_states)
662
+ ar_params = pm.Normal('ar_params', size=ss_mod.p)
663
+ ma_parama = pm.Normal('ma_params', size=ss_mod.q)
664
+ sigma_state = pm.Normal('sigma_state')
665
+
666
+ ss_mod._insert_random_variables()
667
+ matrics = ss_mod.unpack_statespace()
668
+
669
+ pm.draw(matrices['transition'], random_seed=RANDOM_SEED)
670
+ >>> array([[-0.90590386, 1. , 0. ],
671
+ >>> [ 1.25190143, 0. , 1. ],
672
+ >>> [ 0. , 0. , 0. ]])
673
+
674
+ pm.draw(matrices['selection'], random_seed=RANDOM_SEED)
675
+ >>> array([[ 1. ],
676
+ >>> [-2.46741039],
677
+ >>> [-0.28947689]])
678
+
679
+ pm.draw(matrices['state_cov'], random_seed=RANDOM_SEED)
680
+ >>> array([[-1.69353533]])
681
+ """
682
+
683
+ pymc_model = modelcontext(None)
684
+ found_params = []
685
+ with pymc_model:
686
+ for param_name in self.param_names:
687
+ param = getattr(pymc_model, param_name, None)
688
+ if param:
689
+ found_params.append(param.name)
690
+
691
+ missing_params = list(set(self.param_names) - set(found_params))
692
+ if len(missing_params) > 0:
693
+ raise ValueError(
694
+ "The following required model parameters were not found in the PyMC model: "
695
+ + ", ".join(missing_params)
696
+ )
697
+
698
+ excess_params = list(set(found_params) - set(self.param_names))
699
+ if len(excess_params) > 0:
700
+ raise ValueError(
701
+ "The following parameters were found in the PyMC model but are not required by the statespace model: "
702
+ + ", ".join(excess_params)
703
+ )
704
+
705
+ matrices = list(self._unpack_statespace_with_placeholders())
706
+
707
+ replacement_dict = {var: pymc_model[name] for name, var in self._name_to_variable.items()}
708
+ self.subbed_ssm = graph_replace(matrices, replace=replacement_dict, strict=True)
709
+
710
+ def _insert_data_variables(self):
711
+ """
712
+ Replace symbolic pytensor variables with PyMC data containers.
713
+
714
+ Only used when models require exogenous data. The observed data is not added to the model using this method!
715
+ """
716
+
717
+ try:
718
+ data_names = self.data_names
719
+ except NotImplementedError:
720
+ return
721
+
722
+ pymc_model = modelcontext(None)
723
+ found_data = []
724
+ with pymc_model:
725
+ for data_name in data_names:
726
+ data = getattr(pymc_model, data_name, None)
727
+ if data:
728
+ found_data.append(data.name)
729
+
730
+ missing_data = list(set(data_names) - set(found_data))
731
+ if len(missing_data) > 0:
732
+ raise ValueError(
733
+ "The following required exogenous data were not found in the PyMC model: "
734
+ + ", ".join(missing_data)
735
+ )
736
+
737
+ replacement_dict = {data: pymc_model[name] for name, data in self._name_to_data.items()}
738
+ self.subbed_ssm = graph_replace(self.subbed_ssm, replace=replacement_dict, strict=True)
739
+
740
+ def _register_matrices_with_pymc_model(self) -> list[pt.TensorVariable]:
741
+ """
742
+ Add all statespace matrices to the PyMC model currently on the context stack as pm.Deterministic nodes, and
743
+ adds named dimensions if they are found.
744
+
745
+ Returns
746
+ -------
747
+ registered_matrices: list of pt.TensorVariable
748
+ list of statespace matrices, wrapped in pm.Deterministic
749
+ """
750
+
751
+ pm_mod = modelcontext(None)
752
+ matrices = self.unpack_statespace()
753
+
754
+ registered_matrices = []
755
+ for i, (matrix, name) in enumerate(zip(matrices, MATRIX_NAMES)):
756
+ time_varying_ndim = 2 if name in VECTOR_VALUED else 3
757
+ if not getattr(pm_mod, name, None):
758
+ shape, dims = self._get_matrix_shape_and_dims(name)
759
+ has_dims = dims is not None
760
+
761
+ if matrix.ndim == time_varying_ndim and has_dims:
762
+ dims = (TIME_DIM, *dims)
763
+
764
+ x = pm.Deterministic(name, matrix, dims=dims)
765
+ registered_matrices.append(x)
766
+ else:
767
+ registered_matrices.append(matrices[i])
768
+
769
+ return registered_matrices
770
+
771
+ @staticmethod
772
+ def _register_kalman_filter_outputs_with_pymc_model(outputs: tuple[pt.TensorVariable]) -> None:
773
+ mod = modelcontext(None)
774
+ coords = mod.coords
775
+
776
+ states, covs = outputs[:4], outputs[4:]
777
+
778
+ state_names = [
779
+ "filtered_state",
780
+ "predicted_state",
781
+ "predicted_observed_state",
782
+ "smoothed_state",
783
+ ]
784
+ cov_names = [
785
+ "filtered_covariance",
786
+ "predicted_covariance",
787
+ "predicted_observed_covariance",
788
+ "smoothed_covariance",
789
+ ]
790
+
791
+ with mod:
792
+ for var, name in zip(states + covs, state_names + cov_names):
793
+ dim_names = FILTER_OUTPUT_DIMS.get(name, None)
794
+ dims = tuple([dim if dim in coords.keys() else None for dim in dim_names])
795
+ pm.Deterministic(name, var, dims=dims)
796
+
797
+ def build_statespace_graph(
798
+ self,
799
+ data: np.ndarray | pd.DataFrame | pt.TensorVariable,
800
+ register_data: bool = True,
801
+ mode: str | None = None,
802
+ missing_fill_value: float | None = None,
803
+ cov_jitter: float | None = JITTER_DEFAULT,
804
+ save_kalman_filter_outputs_in_idata: bool = False,
805
+ ) -> None:
806
+ """
807
+ Given a parameter vector `theta`, constructs the full computational graph describing the state space model and
808
+ the associated log probability of the data. Hidden states and log probabilities are computed via the Kalman
809
+ Filter.
810
+
811
+ Parameters
812
+ ----------
813
+ data : Union[np.ndarray, pd.DataFrame, pt.TensorVariable]
814
+ The observed data used to fit the state space model. It can be a NumPy array, a Pandas DataFrame,
815
+ or a Pytensor tensor variable.
816
+
817
+ register_data : bool, optional, default=True
818
+ If True, the observed data will be registered with PyMC as a pm.Data variable. In addition,
819
+ a "time" dim will be created an added to the model's coords.
820
+
821
+ mode : Optional[str], optional, default=None
822
+ The Pytensor mode used for the computation graph construction. If None, the default mode will be used.
823
+ Other options include "JAX" and "NUMBA".
824
+
825
+ missing_fill_value: float, optional, default=-9999
826
+ A value to mask in missing values. NaN values in the data need to be filled with an arbitrary value to
827
+ avoid triggering PyMC's automatic imputation machinery (missing values are instead filled by treating them
828
+ as hidden states during Kalman filtering).
829
+
830
+ In general this never needs to be set. But if by a wild coincidence your data includes the value -9999.0,
831
+ you will need to change the missing_fill_value to something else, to avoid incorrectly mark in
832
+ data as missing.
833
+
834
+ cov_jitter: float, default 1e-8 or 1e-6 if pytensor.config.floatX is float32
835
+ The Kalman filter is known to be numerically unstable, especially at half precision. This value is added to
836
+ the diagonal of every covariance matrix -- predicted, filtered, and smoothed -- at every step, to ensure
837
+ all matrices are strictly positive semi-definite.
838
+
839
+ Obviously, if this can be zero, that's best. In general:
840
+ - Having measurement error makes Kalman Filters more robust. A large source of numerical errors come
841
+ from the Filtered and Smoothed covariance matrices having a zero in the (0, 0) position, which always
842
+ occurs when there is no measurement error. You can lower this value in the presence of measurement
843
+ error.
844
+
845
+ - The Univariate Filter is more robust than other filters, and can tolerate a lower jitter value
846
+
847
+ save_kalman_filter_outputs_in_idata: bool, optional, default=False
848
+ If True, Kalman Filter outputs will be saved in the model as deterministics. Useful for debugging, but
849
+ should not be necessary for the majority of users.
850
+ """
851
+ pm_mod = modelcontext(None)
852
+
853
+ self._insert_random_variables()
854
+ self._save_exogenous_data_info()
855
+ self._insert_data_variables()
856
+
857
+ obs_coords = pm_mod.coords.get(OBS_STATE_DIM, None)
858
+ self._fit_data = data
859
+
860
+ data, nan_mask = register_data_with_pymc(
861
+ data,
862
+ n_obs=self.ssm.k_endog,
863
+ obs_coords=obs_coords,
864
+ register_data=register_data,
865
+ missing_fill_value=missing_fill_value,
866
+ )
867
+
868
+ filter_outputs = self.kalman_filter.build_graph(
869
+ pt.as_tensor_variable(data),
870
+ *self.unpack_statespace(),
871
+ mode=mode,
872
+ missing_fill_value=missing_fill_value,
873
+ cov_jitter=cov_jitter,
874
+ )
875
+
876
+ logp = filter_outputs.pop(-1)
877
+ states, covs = filter_outputs[:3], filter_outputs[3:]
878
+ filtered_states, predicted_states, observed_states = states
879
+ filtered_covariances, predicted_covariances, observed_covariances = covs
880
+ if save_kalman_filter_outputs_in_idata:
881
+ smooth_states, smooth_covariances = self._build_smoother_graph(
882
+ filtered_states, filtered_covariances, self.unpack_statespace(), mode=mode
883
+ )
884
+ all_kf_outputs = [*states, smooth_states, *covs, smooth_covariances]
885
+ self._register_kalman_filter_outputs_with_pymc_model(all_kf_outputs)
886
+
887
+ obs_dims = FILTER_OUTPUT_DIMS["predicted_observed_state"]
888
+ obs_dims = obs_dims if all([dim in pm_mod.coords.keys() for dim in obs_dims]) else None
889
+
890
+ SequenceMvNormal(
891
+ "obs",
892
+ mus=observed_states,
893
+ covs=observed_covariances,
894
+ logp=logp,
895
+ observed=data,
896
+ dims=obs_dims,
897
+ )
898
+
899
+ self._fit_coords = pm_mod.coords.copy()
900
+ self._fit_dims = pm_mod.named_vars_to_dims.copy()
901
+ self._fit_mode = mode
902
+
903
+ def _build_smoother_graph(
904
+ self,
905
+ filtered_states: pt.TensorVariable,
906
+ filtered_covariances: pt.TensorVariable,
907
+ matrices,
908
+ mode: str | None = None,
909
+ cov_jitter=JITTER_DEFAULT,
910
+ ):
911
+ """
912
+ Build the computation graph for the Kalman smoother.
913
+
914
+ This method constructs the computation graph for applying the Kalman smoother to the filtered states
915
+ and covariances obtained from the Kalman filter. The Kalman smoother is used to generate smoothed
916
+ estimates of the latent states and their covariances in a state space model.
917
+
918
+ The Kalman smoother provides a more accurate estimate of the latent states by incorporating future
919
+ information in the backward pass, resulting in smoothed state trajectories.
920
+
921
+ Parameters
922
+ ----------
923
+ filtered_states : pytensor.tensor.TensorVariable
924
+ The filtered states obtained from the Kalman filter. Returned by the `build_statespace_graph` method.
925
+
926
+ filtered_covariances : pytensor.tensor.TensorVariable
927
+ The filtered state covariances obtained from the Kalman filter. Returned by the `build_statespace_graph`
928
+ method.
929
+
930
+ mode : Optional[str], default=None
931
+ The mode used by pytensor for the construction of the logp graph. If None, the mode provided to
932
+ `build_statespace_graph` will be used.
933
+
934
+ Returns
935
+ -------
936
+ Tuple[pytensor.tensor.TensorVariable, pytensor.tensor.TensorVariable]
937
+ A tuple containing TensorVariables representing the smoothed states and smoothed state covariances
938
+ obtained from the Kalman smoother.
939
+ """
940
+
941
+ pymc_model = modelcontext(None)
942
+ with pymc_model:
943
+ *_, T, Z, R, H, Q = matrices
944
+
945
+ smooth_states, smooth_covariances = self.kalman_smoother.build_graph(
946
+ T, R, Q, filtered_states, filtered_covariances, mode=mode, cov_jitter=cov_jitter
947
+ )
948
+ smooth_states.name = "smooth_states"
949
+ smooth_covariances.name = "smooth_covariances"
950
+
951
+ return smooth_states, smooth_covariances
952
+
953
+ def _build_dummy_graph(self) -> None:
954
+ """
955
+ Build a dummy computation graph for the state space model matrices.
956
+
957
+ This method creates "dummy" pm.Flat variables representing the deep parameters used in the state space model.
958
+
959
+ Returns
960
+ -------
961
+ list[pm.Flat]
962
+ A list of pm.Flat variables representing all parameters estimated by the model.
963
+ """
964
+ for name in self.param_names:
965
+ pm.Flat(
966
+ name,
967
+ shape=self._name_to_variable[name].type.shape,
968
+ dims=self._fit_dims.get(name, None),
969
+ )
970
+
971
+ def _kalman_filter_outputs_from_dummy_graph(
972
+ self,
973
+ data: pt.TensorLike | None = None,
974
+ data_dims: str | tuple[str] | list[str] | None = None,
975
+ scenario: dict[str, pd.DataFrame] | pd.DataFrame | None = None,
976
+ ) -> tuple[list[pt.TensorVariable], list[tuple[pt.TensorVariable, pt.TensorVariable]]]:
977
+ """
978
+ Builds a Kalman filter graph using "dummy" pm.Flat distributions for the model variables and sorts the returns
979
+ into (mean, covariance) pairs for each of filtered, predicted, and smoothed output.
980
+
981
+ Parameters
982
+ ----------
983
+ data: pt.TensorLike, optional
984
+ Observed data on which to condition the model. If not provided, the function will use the data that was
985
+ provided when the model was built.
986
+ data_dims: str or tuple of str, optional
987
+ Dimension names associated with the model data. If None, defaults to ("time", "obs_state")
988
+
989
+ Returns
990
+ -------
991
+ matrices: list of tensors
992
+ Statespace matrices with dummy parameters.
993
+
994
+ grouped_outputs: list of tuple of tensors
995
+ A list of tuples, each containing the mean and covariance of the filtered, predicted, and smoothed states.
996
+ """
997
+ if scenario is None:
998
+ scenario = dict()
999
+
1000
+ pm_mod = modelcontext(None)
1001
+ self._build_dummy_graph()
1002
+ self._insert_random_variables()
1003
+
1004
+ for name in self.data_names:
1005
+ if name not in pm_mod:
1006
+ pm.Data(**self._exog_data_info[name])
1007
+
1008
+ self._insert_data_variables()
1009
+
1010
+ for name in self.data_names:
1011
+ if name in scenario.keys():
1012
+ pm.set_data({name: scenario[name]})
1013
+
1014
+ x0, P0, c, d, T, Z, R, H, Q = self.unpack_statespace()
1015
+
1016
+ if data is None:
1017
+ data = self._fit_data
1018
+
1019
+ obs_coords = pm_mod.coords.get(OBS_STATE_DIM, None)
1020
+
1021
+ data, nan_mask = register_data_with_pymc(
1022
+ data,
1023
+ n_obs=self.ssm.k_endog,
1024
+ obs_coords=obs_coords,
1025
+ data_dims=data_dims,
1026
+ register_data=True,
1027
+ )
1028
+
1029
+ filter_outputs = self.kalman_filter.build_graph(
1030
+ data,
1031
+ x0,
1032
+ P0,
1033
+ c,
1034
+ d,
1035
+ T,
1036
+ Z,
1037
+ R,
1038
+ H,
1039
+ Q,
1040
+ mode=self._fit_mode,
1041
+ )
1042
+
1043
+ filter_outputs.pop(-1)
1044
+ states, covariances = filter_outputs[:3], filter_outputs[3:]
1045
+
1046
+ filtered_states, predicted_states, _ = states
1047
+ filtered_covariances, predicted_covariances, _ = covariances
1048
+
1049
+ [smoothed_states, smoothed_covariances] = self.kalman_smoother.build_graph(
1050
+ T, R, Q, filtered_states, filtered_covariances, mode=self._fit_mode
1051
+ )
1052
+
1053
+ grouped_outputs = [
1054
+ (filtered_states, filtered_covariances),
1055
+ (predicted_states, predicted_covariances),
1056
+ (smoothed_states, smoothed_covariances),
1057
+ ]
1058
+
1059
+ return [x0, P0, c, d, T, Z, R, H, Q], grouped_outputs
1060
+
1061
+ def _sample_conditional(
1062
+ self,
1063
+ idata: InferenceData,
1064
+ group: str,
1065
+ random_seed: RandomState | None = None,
1066
+ data: pt.TensorLike | None = None,
1067
+ **kwargs,
1068
+ ):
1069
+ """
1070
+ Common functionality shared between `sample_conditional_prior` and `sample_conditional_posterior`. See those
1071
+ methods for details.
1072
+
1073
+ Parameters
1074
+ ----------
1075
+ idata : InferenceData
1076
+ An Arviz InferenceData object containing the posterior distribution over model parameters.
1077
+
1078
+ group : str
1079
+ InferenceData group from which to draw samples. Should be one of "prior" or "posterior".
1080
+
1081
+ random_seed : int, RandomState or Generator, optional
1082
+ Seed for the random number generator.
1083
+
1084
+ data: pt.TensorLike, optional
1085
+ Observed data on which to condition the model. If not provided, the function will use the data that was
1086
+ provided when the model was built.
1087
+
1088
+ kwargs:
1089
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
1090
+
1091
+ Returns
1092
+ -------
1093
+ InferenceData
1094
+ An Arviz InferenceData object containing sampled trajectories from the requested conditional distribution,
1095
+ with data variables "filtered_{group}", "predicted_{group}", and "smoothed_{group}".
1096
+ """
1097
+ if data is None and self._fit_data is None:
1098
+ raise ValueError("No data provided to condition the model")
1099
+
1100
+ _verify_group(group)
1101
+ group_idata = getattr(idata, group)
1102
+
1103
+ with pm.Model(coords=self._fit_coords) as forward_model:
1104
+ (
1105
+ [
1106
+ x0,
1107
+ P0,
1108
+ c,
1109
+ d,
1110
+ T,
1111
+ Z,
1112
+ R,
1113
+ H,
1114
+ Q,
1115
+ ],
1116
+ grouped_outputs,
1117
+ ) = self._kalman_filter_outputs_from_dummy_graph(data=data)
1118
+
1119
+ for name, (mu, cov) in zip(FILTER_OUTPUT_TYPES, grouped_outputs):
1120
+ dummy_ll = pt.zeros_like(mu)
1121
+
1122
+ state_dims = (
1123
+ (TIME_DIM, ALL_STATE_DIM)
1124
+ if all([dim in self._fit_coords for dim in [TIME_DIM, ALL_STATE_DIM]])
1125
+ else (None, None)
1126
+ )
1127
+ obs_dims = (
1128
+ (TIME_DIM, OBS_STATE_DIM)
1129
+ if all([dim in self._fit_coords for dim in [TIME_DIM, OBS_STATE_DIM]])
1130
+ else (None, None)
1131
+ )
1132
+
1133
+ SequenceMvNormal(
1134
+ f"{name}_{group}",
1135
+ mus=mu,
1136
+ covs=cov,
1137
+ logp=dummy_ll,
1138
+ dims=state_dims,
1139
+ )
1140
+
1141
+ obs_mu = (Z @ mu[..., None]).squeeze(-1)
1142
+ obs_cov = Z @ cov @ pt.swapaxes(Z, -2, -1) + H
1143
+
1144
+ SequenceMvNormal(
1145
+ f"{name}_{group}_observed",
1146
+ mus=obs_mu,
1147
+ covs=obs_cov,
1148
+ logp=dummy_ll,
1149
+ dims=obs_dims,
1150
+ )
1151
+
1152
+ # TODO: Remove this after pm.Flat initial values are fixed
1153
+ forward_model.rvs_to_initial_values = {
1154
+ rv: None for rv in forward_model.rvs_to_initial_values.keys()
1155
+ }
1156
+
1157
+ frozen_model = freeze_dims_and_data(forward_model)
1158
+ with frozen_model:
1159
+ idata_conditional = pm.sample_posterior_predictive(
1160
+ group_idata,
1161
+ var_names=[
1162
+ f"{name}_{group}{suffix}"
1163
+ for name in FILTER_OUTPUT_TYPES
1164
+ for suffix in ["", "_observed"]
1165
+ ],
1166
+ compile_kwargs={"mode": get_mode(self._fit_mode)},
1167
+ random_seed=random_seed,
1168
+ **kwargs,
1169
+ )
1170
+
1171
+ return idata_conditional.posterior_predictive
1172
+
1173
+ def _sample_unconditional(
1174
+ self,
1175
+ idata: InferenceData,
1176
+ group: str,
1177
+ steps: int | None = None,
1178
+ use_data_time_dim: bool = False,
1179
+ random_seed: RandomState | None = None,
1180
+ **kwargs,
1181
+ ):
1182
+ """
1183
+ Draw unconditional sample trajectories according to state space dynamics, using random samples from the
1184
+ a provided trace. The state space update equations are:
1185
+
1186
+ X[t+1] = T @ X[t] + R @ eta[t], eta ~ N(0, Q)
1187
+ Y[t] = Z @ X[t] + nu[t], nu ~ N(0, H)
1188
+ x[0] ~ N(a0, P0)
1189
+
1190
+ Parameters
1191
+ ----------
1192
+ idata : InferenceData
1193
+ An Arviz InferenceData object with a posterior group containing samples from the
1194
+ posterior distribution over model parameters.
1195
+
1196
+ steps : Optional[int], default=None
1197
+ The number of time steps to sample for the unconditional trajectories. If not provided (None),
1198
+ the function will sample trajectories for the entire available time dimension in the posterior.
1199
+ Otherwise, it will generate trajectories for the specified number of steps.
1200
+
1201
+ use_data_time_dim : bool, default=False
1202
+ If True, the function uses the time dimension present in the provided `idata` object to sample
1203
+ unconditional trajectories. If False, a custom time dimension is created based on the number of steps
1204
+ specified, or if steps is None, it uses the entire available time dimension in the posterior.
1205
+
1206
+ random_seed : int, RandomState or Generator, optional
1207
+ Seed for the random number generator.
1208
+
1209
+ kwargs:
1210
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
1211
+
1212
+ Returns
1213
+ -------
1214
+ InferenceData
1215
+ An Arviz InfereceData with two groups, posterior_latent and posterior_observed
1216
+
1217
+ - posterior_latent represents the latent state trajectories `X[t]`, which follows the dynamics:
1218
+ `x[t+1] = T @ x[t] + R @ eta[t]`, where `eta ~ N(0, Q)`.
1219
+
1220
+ - posterior_observed represents the observed state trajectories `Y[t]`, which is obtained from
1221
+ the latent state trajectories: `y[t] = Z @ x[t] + nu[t]`, where `nu ~ N(0, H)`.
1222
+ """
1223
+ _verify_group(group)
1224
+ group_idata = getattr(idata, group)
1225
+ dims = None
1226
+ temp_coords = self._fit_coords.copy()
1227
+
1228
+ if not use_data_time_dim and steps is not None:
1229
+ temp_coords.update({TIME_DIM: np.arange(1 + steps, dtype="int")})
1230
+ steps = len(temp_coords[TIME_DIM]) - 1
1231
+ elif steps is not None:
1232
+ n_dimsteps = len(temp_coords[TIME_DIM])
1233
+ if n_dimsteps != steps:
1234
+ raise ValueError(
1235
+ f"Length of time dimension does not match specified number of steps, expected"
1236
+ f" {n_dimsteps} steps, or steps=None."
1237
+ )
1238
+ else:
1239
+ steps = len(temp_coords[TIME_DIM]) - 1
1240
+
1241
+ if all([dim in self._fit_coords for dim in [TIME_DIM, ALL_STATE_DIM, OBS_STATE_DIM]]):
1242
+ dims = [TIME_DIM, ALL_STATE_DIM, OBS_STATE_DIM]
1243
+
1244
+ with pm.Model(coords=temp_coords if dims is not None else None) as forward_model:
1245
+ self._build_dummy_graph()
1246
+ self._insert_random_variables()
1247
+
1248
+ for name in self.data_names:
1249
+ pm.Data(**self._exog_data_info[name])
1250
+
1251
+ self._insert_data_variables()
1252
+
1253
+ matrices = [x0, P0, c, d, T, Z, R, H, Q] = self.unpack_statespace()
1254
+
1255
+ if not self.measurement_error:
1256
+ H_jittered = pm.Deterministic(
1257
+ "H_jittered", pt.specify_shape(stabilize(H), (self.k_endog, self.k_endog))
1258
+ )
1259
+ matrices = [x0, P0, c, d, T, Z, R, H_jittered, Q]
1260
+
1261
+ LinearGaussianStateSpace(
1262
+ group,
1263
+ *matrices,
1264
+ steps=steps,
1265
+ dims=dims,
1266
+ mode=self._fit_mode,
1267
+ sequence_names=self.kalman_filter.seq_names,
1268
+ k_endog=self.k_endog,
1269
+ )
1270
+
1271
+ # TODO: Remove this after pm.Flat has its initial_value fixed
1272
+ forward_model.rvs_to_initial_values = {
1273
+ rv: None for rv in forward_model.rvs_to_initial_values.keys()
1274
+ }
1275
+ frozen_model = freeze_dims_and_data(forward_model)
1276
+
1277
+ with frozen_model:
1278
+ idata_unconditional = pm.sample_posterior_predictive(
1279
+ group_idata,
1280
+ var_names=[f"{group}_latent", f"{group}_observed"],
1281
+ compile_kwargs={"mode": self._fit_mode},
1282
+ random_seed=random_seed,
1283
+ **kwargs,
1284
+ )
1285
+
1286
+ return idata_unconditional.posterior_predictive
1287
+
1288
+ def sample_conditional_prior(
1289
+ self, idata: InferenceData, random_seed: RandomState | None = None, **kwargs
1290
+ ) -> InferenceData:
1291
+ """
1292
+ Sample from the conditional prior; that is, given parameter draws from the prior distribution,
1293
+ compute Kalman filtered trajectories. Trajectories are drawn from a single multivariate normal with mean and
1294
+ covariance computed via either the Kalman filter, smoother, or predictions.
1295
+
1296
+ Parameters
1297
+ ----------
1298
+ idata : InferenceData
1299
+ Arviz InferenceData with prior samples for state space matrices x0, P0, c, d, T, Z, R, H, Q.
1300
+ Obtained from `pm.sample_prior_predictive` after calling PyMCStateSpace.build_statespace_graph().
1301
+
1302
+ random_seed : int, RandomState or Generator, optional
1303
+ Seed for the random number generator.
1304
+
1305
+ kwargs:
1306
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
1307
+
1308
+ Returns
1309
+ -------
1310
+ InferenceData
1311
+ An Arviz InferenceData object containing sampled trajectories from the conditional prior.
1312
+ The trajectories are stored in the posterior_predictive group with names "filtered_prior",
1313
+ "predicted_prior", and "smoothed_prior".
1314
+ """
1315
+
1316
+ return self._sample_conditional(idata, "prior", random_seed, **kwargs)
1317
+
1318
+ def sample_conditional_posterior(
1319
+ self, idata: InferenceData, random_seed: RandomState | None = None, **kwargs
1320
+ ):
1321
+ """
1322
+ Sample from the conditional posterior; that is, given parameter draws from the posterior distribution,
1323
+ compute Kalman filtered trajectories. Trajectories are drawn from a single multivariate normal with mean and
1324
+ covariance computed via either the Kalman filter, smoother, or predictions.
1325
+
1326
+ Parameters
1327
+ ----------
1328
+ idata : InferenceData
1329
+ An Arviz InferenceData object containing the posterior distribution over model parameters.
1330
+
1331
+ random_seed : int, RandomState or Generator, optional
1332
+ Seed for the random number generator.
1333
+
1334
+ kwargs:
1335
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
1336
+
1337
+ Returns
1338
+ -------
1339
+ InferenceData
1340
+ An Arviz InferenceData object containing sampled trajectories from the conditional posterior.
1341
+ The trajectories are stored in the posterior_predictive group with names "filtered_posterior",
1342
+ "predicted_posterior", and "smoothed_posterior".
1343
+ """
1344
+
1345
+ return self._sample_conditional(idata, "posterior", random_seed, **kwargs)
1346
+
1347
+ def sample_unconditional_prior(
1348
+ self,
1349
+ idata: InferenceData,
1350
+ steps: int | None = None,
1351
+ use_data_time_dim: bool = False,
1352
+ random_seed: RandomState | None = None,
1353
+ **kwargs,
1354
+ ) -> InferenceData:
1355
+ """
1356
+ Draw unconditional sample trajectories according to state space dynamics, using random samples from the prior
1357
+ distribution over model parameters. The state space update equations are:
1358
+
1359
+ X[t+1] = T @ X[t] + R @ eta[t], eta ~ N(0, Q)
1360
+ Y[t] = Z @ X[t] + nu[t], nu ~ N(0, H)
1361
+
1362
+ Parameters
1363
+ ----------
1364
+ idata: InferenceData
1365
+ Arviz InferenceData with prior samples for state space matrices x0, P0, c, d, T, Z, R, H, Q.
1366
+ Obtained from `pm.sample_prior_predictive` after calling PyMCStateSpace.build_statespace_graph().
1367
+
1368
+ steps : Optional[int], default=None
1369
+ The number of time steps to sample for the unconditional trajectories. If not provided (None),
1370
+ the function will sample trajectories for the entire available time dimension in the posterior.
1371
+ Otherwise, it will generate trajectories for the specified number of steps.
1372
+
1373
+ use_data_time_dim : bool, default=False
1374
+ If True, the function uses the time dimension present in the provided `idata` object to sample
1375
+ unconditional trajectories. If False, a custom time dimension is created based on the number of steps
1376
+ specified, or if steps is None, it uses the entire available time dimension in the posterior.
1377
+
1378
+ random_seed : int, RandomState or Generator, optional
1379
+ Seed for the random number generator.
1380
+
1381
+ kwargs:
1382
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
1383
+
1384
+ Returns
1385
+ -------
1386
+ InferenceData
1387
+ An Arviz InfereceData with two data variables, prior_latent and prior_observed
1388
+
1389
+ - prior_latent represents the latent state trajectories `X[t]`, which follows the dynamics:
1390
+ `x[t+1] = T @ x[t] + R @ eta[t]`, where `eta ~ N(0, Q)`.
1391
+
1392
+ - prior_observed represents the observed state trajectories `Y[t]`, which is obtained from
1393
+ the observation equation: `y[t] = Z @ x[t] + nu[t]`, where `nu ~ N(0, H)`.
1394
+ """
1395
+
1396
+ return self._sample_unconditional(
1397
+ idata, "prior", steps, use_data_time_dim, random_seed, **kwargs
1398
+ )
1399
+
1400
+ def sample_unconditional_posterior(
1401
+ self,
1402
+ idata: InferenceData,
1403
+ steps: int | None = None,
1404
+ use_data_time_dim: bool = False,
1405
+ random_seed: RandomState | None = None,
1406
+ **kwargs,
1407
+ ) -> InferenceData:
1408
+ """
1409
+ Draw unconditional sample trajectories according to state space dynamics, using random samples from the
1410
+ posterior distribution over model parameters. The state space update equations are:
1411
+
1412
+ X[t+1] = T @ X[t] + R @ eta[t], eta ~ N(0, Q)
1413
+ Y[t] = Z @ X[t] + nu[t], nu ~ N(0, H)
1414
+ x[0] ~ N(a0, P0)
1415
+
1416
+ Parameters
1417
+ ----------
1418
+ idata : InferenceData
1419
+ An Arviz InferenceData object with a posterior group containing samples from the
1420
+ posterior distribution over model parameters.
1421
+
1422
+ steps : Optional[int], default=None
1423
+ The number of time steps to sample for the unconditional trajectories. If not provided (None),
1424
+ the function will sample trajectories for the entire available time dimension in the posterior.
1425
+ Otherwise, it will generate trajectories for the specified number of steps.
1426
+
1427
+ use_data_time_dim : bool, default=False
1428
+ If True, the function uses the time dimension present in the provided `idata` object to sample
1429
+ unconditional trajectories. If False, a custom time dimension is created based on the number of steps
1430
+ specified, or if steps is None, it uses the entire available time dimension in the posterior.
1431
+
1432
+ random_seed : int, RandomState or Generator, optional
1433
+ Seed for the random number generator.
1434
+
1435
+ Returns
1436
+ -------
1437
+ InferenceData
1438
+ An Arviz InfereceData with two groups, posterior_latent and posterior_observed
1439
+
1440
+ - posterior_latent represents the latent state trajectories `X[t]`, which follows the dynamics:
1441
+ `x[t+1] = T @ x[t] + R @ eta[t]`, where `eta ~ N(0, Q)`.
1442
+
1443
+ - posterior_observed represents the observed state trajectories `Y[t]`, which is obtained from
1444
+ the latent state trajectories: `y[t] = Z @ x[t] + nu[t]`, where `nu ~ N(0, H)`.
1445
+ """
1446
+
1447
+ return self._sample_unconditional(
1448
+ idata, "posterior", steps, use_data_time_dim, random_seed, **kwargs
1449
+ )
1450
+
1451
+ def sample_statespace_matrices(
1452
+ self, idata, matrix_names: str | list[str] | None, group: str = "posterior"
1453
+ ):
1454
+ """
1455
+ Draw samples of requested statespace matrices from provided idata
1456
+
1457
+ Parameters
1458
+ ----------
1459
+ matrix_names: str, list[str], optional
1460
+ Statespace matrices to be sampled. Valid names are short names: x0, P0, c, d, T, Z, R, H, Q, or
1461
+ "formal" names: initial_state, initial_state_cov, state_intercept, obs_intercept, transition, design,
1462
+ selection, obs_cov, state_cov
1463
+ idata: az.InferenceData
1464
+ InferenceData from which to sample
1465
+
1466
+ group: str, one of "posterior" or "prior"
1467
+ Whether to sample from priors or posteriors
1468
+
1469
+ Returns
1470
+ -------
1471
+ idata_matrices: az.InterenceData
1472
+ """
1473
+ _verify_group(group)
1474
+
1475
+ if matrix_names is None:
1476
+ matrix_names = MATRIX_NAMES
1477
+ elif isinstance(matrix_names, str):
1478
+ matrix_names = [matrix_names]
1479
+
1480
+ with pm.Model(coords=self._fit_coords) as forward_model:
1481
+ self._build_dummy_graph()
1482
+ self._insert_random_variables()
1483
+
1484
+ for name in self.data_names:
1485
+ pm.Data(**self._exog_data_info[name])
1486
+
1487
+ self._insert_data_variables()
1488
+ matrices = self.unpack_statespace()
1489
+ for short_name, matrix in zip(MATRIX_NAMES, matrices):
1490
+ long_name = SHORT_NAME_TO_LONG[short_name]
1491
+ if (long_name in matrix_names) or (short_name in matrix_names):
1492
+ name = long_name if long_name in matrix_names else short_name
1493
+ dims = [x if x in self._fit_coords else None for x in MATRIX_DIMS[short_name]]
1494
+ pm.Deterministic(name, matrix, dims=dims)
1495
+
1496
+ # TODO: Remove this after pm.Flat has its initial_value fixed
1497
+ forward_model.rvs_to_initial_values = {
1498
+ rv: None for rv in forward_model.rvs_to_initial_values.keys()
1499
+ }
1500
+ frozen_model = freeze_dims_and_data(forward_model)
1501
+ with frozen_model:
1502
+ matrix_idata = pm.sample_posterior_predictive(
1503
+ idata if group == "posterior" else idata.prior,
1504
+ var_names=matrix_names,
1505
+ compile_kwargs={"mode": self._fit_mode},
1506
+ extend_inferencedata=False,
1507
+ )
1508
+
1509
+ return matrix_idata
1510
+
1511
+ @staticmethod
1512
+ def _validate_forecast_args(
1513
+ time_index: pd.RangeIndex | pd.DatetimeIndex,
1514
+ start: int | pd.Timestamp,
1515
+ periods: int | None = None,
1516
+ end: int | pd.Timestamp = None,
1517
+ scenario: pd.DataFrame | np.ndarray | None = None,
1518
+ use_scenario_index: bool = False,
1519
+ verbose: bool = True,
1520
+ ):
1521
+ if isinstance(start, pd.Timestamp) and start not in time_index:
1522
+ raise ValueError("Datetime start must be in the data index used to fit the model.")
1523
+ elif isinstance(start, int):
1524
+ if abs(start) > len(time_index):
1525
+ raise ValueError(
1526
+ "Integer start must be within the range of the data index used to fit the model."
1527
+ )
1528
+ if periods is None and end is None:
1529
+ raise ValueError("Must specify one of either periods or end")
1530
+ if periods is not None and end is not None:
1531
+ raise ValueError("Must specify exactly one of either periods or end")
1532
+ if scenario is None and use_scenario_index:
1533
+ raise ValueError("use_scenario_index=True requires a scenario to be provided.")
1534
+ if scenario is not None and use_scenario_index:
1535
+ if isinstance(scenario, dict):
1536
+ first_df = next(
1537
+ (df for df in scenario.values() if isinstance(df, pd.DataFrame | pd.Series)),
1538
+ None,
1539
+ )
1540
+ if first_df is None:
1541
+ raise ValueError(
1542
+ "use_scenario_index=True requires a scenario to be a DataFrame or Series."
1543
+ )
1544
+ elif not isinstance(scenario, pd.DataFrame | pd.Series):
1545
+ raise ValueError(
1546
+ "use_scenario_index=True requires a scenario to be a DataFrame or Series."
1547
+ )
1548
+ if use_scenario_index and any(arg is not None for arg in [start, end, periods]) and verbose:
1549
+ _log.warning(
1550
+ "start, end, and periods arguments are ignored when use_scenario_index is True. Pass only "
1551
+ "one or the other to avoid this warning, or pass verbose = False."
1552
+ )
1553
+
1554
+ def _get_fit_time_index(self) -> pd.RangeIndex | pd.DatetimeIndex:
1555
+ time_index = self._fit_coords.get(TIME_DIM, None) if self._fit_coords is not None else None
1556
+ if time_index is None:
1557
+ raise ValueError(
1558
+ "No time dimension found on coordinates used to fit the model. Has this model been fit?"
1559
+ )
1560
+
1561
+ if isinstance(time_index[0], pd.Timestamp):
1562
+ time_index = pd.DatetimeIndex(time_index)
1563
+ time_index.freq = time_index.inferred_freq
1564
+ else:
1565
+ time_index = np.array(time_index)
1566
+
1567
+ return time_index
1568
+
1569
+ def _validate_scenario_data(
1570
+ self,
1571
+ scenario: pd.DataFrame | np.ndarray | dict[str, pd.DataFrame | np.ndarray] | None,
1572
+ name: str | None = None,
1573
+ verbose=True,
1574
+ ):
1575
+ """
1576
+ Validate the scenario data provided to the forecast method by checking that it has the correct shape and
1577
+ dimensions.
1578
+
1579
+ Parameters
1580
+ ----------
1581
+ scenario
1582
+ name
1583
+ verbose
1584
+
1585
+ Returns
1586
+ -------
1587
+ scenario: pd.DataFrame | np.ndarray | dict[str, pd.DataFrame | np.ndarray]
1588
+ Scenario data, validated and potentially modified.
1589
+
1590
+ """
1591
+ if not self._needs_exog_data:
1592
+ return scenario
1593
+
1594
+ var_to_dims = {key: info["dims"][1:] for key, info in self.data_info.items()}
1595
+
1596
+ if any(len(dims) > 1 for dims in var_to_dims.values()):
1597
+ raise NotImplementedError(">2d exogenous data is not yet supported.")
1598
+ coords = {
1599
+ var: self._fit_coords[dim[0]]
1600
+ for var, dim in var_to_dims.items()
1601
+ if dim[0] in self._fit_coords
1602
+ }
1603
+
1604
+ if self._needs_exog_data and scenario is None:
1605
+ exog_str = ",".join(self._exog_names)
1606
+ suffix = "s" if len(exog_str) > 1 else ""
1607
+ raise ValueError(
1608
+ f"This model was fit using exogenous data. Forecasting cannot be performed without "
1609
+ f"providing scenario data for the following variable{suffix}: {exog_str}"
1610
+ )
1611
+
1612
+ if isinstance(scenario, dict):
1613
+ for name, data in scenario.items():
1614
+ if name not in self._exog_names:
1615
+ raise ValueError(
1616
+ f"Scenario data provided for variable '{name}', which is not an exogenous variable "
1617
+ f"used to fit the model."
1618
+ )
1619
+
1620
+ # Recursively call this function to trigger the non-dictionary branch of the checks on each object
1621
+ # inside the dictionary
1622
+ scenario[name] = self._validate_scenario_data(data, name)
1623
+
1624
+ # The provided dictionary might be a mix of numpy arrays and dataframes if the user is truly horrible.
1625
+ # For checking shapes, the first object will always be good enough. But we also need to make sure all the
1626
+ # indices agree, so we grab the first dataframe (which might not exist, but that's OK)
1627
+ first_scenario = next(iter(scenario.values()))
1628
+ first_df = next((df for df in scenario.values() if isinstance(df, pd.DataFrame)), None)
1629
+
1630
+ if not all(data.shape[0] == first_scenario.shape[0] for data in scenario.values()):
1631
+ raise ValueError(
1632
+ "Scenario data must have the same number of time steps for all variables."
1633
+ )
1634
+
1635
+ if first_df is not None and not all(
1636
+ df.index.equals(first_df.index)
1637
+ for df in scenario.values()
1638
+ if isinstance(df, pd.DataFrame)
1639
+ ):
1640
+ raise ValueError("Scenario data must have the same index for all variables.")
1641
+
1642
+ return scenario
1643
+
1644
+ elif isinstance(scenario, pd.Series | pd.DataFrame | np.ndarray | list | tuple):
1645
+ # A user might be lazy and pass a simple list when there is only one exogenous variable.
1646
+ if isinstance(scenario, list | tuple) or (
1647
+ isinstance(scenario, np.ndarray) and scenario.ndim == 1
1648
+ ):
1649
+ scenario = np.array(scenario).reshape(-1, 1)
1650
+
1651
+ if name is None:
1652
+ # name should only be None on the first non-recursive call. We only arrive to this branch in that case
1653
+ # if a non-dictionary was passed, which in turn should only happen if only a single exogenous data
1654
+ # needs to be set.
1655
+ if len(self._exog_names) > 1:
1656
+ raise ValueError(
1657
+ "Multiple exogenous variables were used to fit the model. Provide a dictionary of "
1658
+ "scenario data instead."
1659
+ )
1660
+ name = self._exog_names[0]
1661
+
1662
+ # Omit dataframe from this basic shape check so we can give more detailed information about missing columns
1663
+ # in the next check
1664
+ if not isinstance(scenario, pd.DataFrame | pd.Series) and scenario.shape[1] != len(
1665
+ coords[name]
1666
+ ):
1667
+ raise ValueError(
1668
+ f"Scenario data for variable '{name}' has the wrong number of columns. Expected "
1669
+ f"{len(coords[name])}, got {scenario.shape[1]}"
1670
+ )
1671
+
1672
+ if isinstance(scenario, pd.Series):
1673
+ if len(coords[name]) > 1:
1674
+ raise ValueError(
1675
+ f"Scenario data for variable '{name}' has the wrong number of columns. Expected "
1676
+ f"{len(coords[name])}, got 1"
1677
+ )
1678
+
1679
+ if isinstance(scenario, pd.DataFrame):
1680
+ expected_cols = coords[name]
1681
+ cols = scenario.columns
1682
+ missing_columns = sorted(list(set(expected_cols) - set(cols)))
1683
+ if len(missing_columns) > 0:
1684
+ suffix = "s" if len(missing_columns) > 1 else ""
1685
+ raise ValueError(
1686
+ f"Scenario data for variable '{name}' is missing the following column{suffix}: "
1687
+ f"{', '.join(missing_columns)}"
1688
+ )
1689
+
1690
+ extra_columns = sorted(list(set(cols) - set(expected_cols)))
1691
+ if len(extra_columns) > 0:
1692
+ suffix = "s" if len(extra_columns) > 1 else ""
1693
+ verb = "is" if len(extra_columns) == 1 else "are"
1694
+ raise ValueError(
1695
+ f"Scenario data for variable '{name}' contains the following extra column{suffix} "
1696
+ f"that {verb} not used by the model: "
1697
+ f"{', '.join(extra_columns)}"
1698
+ )
1699
+
1700
+ if not (a == b for a, b in zip(expected_cols, cols)) and verbose:
1701
+ _log.warning(
1702
+ f"Scenario data for {name} has a different column order than the data used to fit the "
1703
+ f"model. Columns will be automatically re-ordered. Ensure consistent ordering to avoid "
1704
+ f"silent errors."
1705
+ )
1706
+ scenario = scenario[expected_cols]
1707
+
1708
+ return scenario
1709
+
1710
+ @staticmethod
1711
+ def _build_forecast_index(
1712
+ time_index: pd.RangeIndex | pd.DatetimeIndex,
1713
+ start: int | pd.Timestamp | None = None,
1714
+ end: int | pd.Timestamp = None,
1715
+ periods: int | None = None,
1716
+ use_scenario_index: bool = False,
1717
+ scenario: pd.DataFrame | np.ndarray | None = None,
1718
+ ) -> tuple[int | pd.Timestamp, pd.RangeIndex | pd.DatetimeIndex]:
1719
+ """
1720
+ Construct a pandas Index for the requested forecast horizon.
1721
+
1722
+ Parameters
1723
+ ----------
1724
+ time_index: pd.RangeIndex or pd.DatetimeIndex
1725
+ Index of the data used to fit the model
1726
+ start: int or pd.Timestamp, optional
1727
+ Date from which to begin forecasting. If using a datetime index, integer start will be interpreted
1728
+ as a positional index. Otherwise, start must be found inside the time_index
1729
+ end: int or pd.Timestamp, optional
1730
+ Date at which to end forecasting. If using a datetime index, end must be a timestamp.
1731
+ periods: int, optional
1732
+ Number of periods to forecast
1733
+ scenario: pd.DataFrame, np.ndarray, optional
1734
+ Scenario data to use for forecasting. If provided, the index of the scenario data will be used as the
1735
+ forecast index. If provided, start, end, and periods will be ignored.
1736
+ use_scenario_index: bool, default False
1737
+ If True, the index of the scenario data will be used as the forecast index.
1738
+
1739
+
1740
+ Returns
1741
+ -------
1742
+ start: int | pd.TimeStamp
1743
+ The starting date index or time step from which to generate the forecasts.
1744
+
1745
+ forecast_index: pd.DatetimeIndex or pd.RangeIndex
1746
+ Index for the forecast results
1747
+ """
1748
+
1749
+ def get_or_create_index(x, time_index, start=None):
1750
+ if isinstance(x, pd.DataFrame | pd.Series):
1751
+ return x.index
1752
+ elif isinstance(x, dict):
1753
+ return get_or_create_index(next(iter(x.values())), time_index, start)
1754
+ elif isinstance(x, np.ndarray | list | tuple):
1755
+ if start is None:
1756
+ raise ValueError(
1757
+ "Provided scenario has no index and no start date was provided. This combination "
1758
+ "is ambiguous. Please provide a start date, or add an index to the scenario."
1759
+ )
1760
+ is_datetime_index = isinstance(time_index, pd.DatetimeIndex)
1761
+ n = x.shape[0] if isinstance(x, np.ndarray) else len(x)
1762
+
1763
+ if isinstance(start, int):
1764
+ start = time_index[start]
1765
+ if is_datetime_index:
1766
+ return pd.date_range(start, periods=n, freq=time_index.freq)
1767
+ return pd.RangeIndex(start, n + start, step=1, dtype="int")
1768
+
1769
+ else:
1770
+ raise ValueError(f"{type(x)} is not a valid type for scenario data.")
1771
+
1772
+ x0_idx = None
1773
+
1774
+ if use_scenario_index:
1775
+ forecast_index = get_or_create_index(scenario, time_index, start)
1776
+ is_datetime = isinstance(forecast_index, pd.DatetimeIndex)
1777
+
1778
+ # If the user provided an index, we want to take it as-is (without removing the start value). Instead,
1779
+ # step one back and use this as the start value.
1780
+ delta = forecast_index.freq if is_datetime else 1
1781
+ x0_idx = forecast_index[0] - delta
1782
+
1783
+ else:
1784
+ # Otherwise, build an index. It will be a DateTime index if we have all the necessary information, otherwise
1785
+ # use a range index.
1786
+ is_datetime = isinstance(time_index, pd.DatetimeIndex)
1787
+ forecast_index = None
1788
+
1789
+ if is_datetime:
1790
+ freq = time_index.freq
1791
+ if isinstance(start, int):
1792
+ start = time_index[start]
1793
+ if isinstance(end, int):
1794
+ raise ValueError(
1795
+ "end must be a timestamp if using a datetime index. To specify a number of "
1796
+ "timesteps from the start date, use the periods argument instead."
1797
+ )
1798
+ if end is not None:
1799
+ forecast_index = pd.date_range(start, end=end, freq=freq)
1800
+ if periods is not None:
1801
+ # date_range includes both the start and end date, but we're going to pop off the start later
1802
+ # (it will be interpreted as x0). So we need to add 1 to the periods so the user gets "periods"
1803
+ # number of forecasts back
1804
+ forecast_index = pd.date_range(start, periods=periods + 1, freq=freq)
1805
+
1806
+ else:
1807
+ # If the user provided a positive integer as start, directly interpret it as the start time. If its
1808
+ # negative, interpret it as a positional index.
1809
+ if start < 0:
1810
+ start = time_index[start]
1811
+ if end is not None:
1812
+ forecast_index = pd.RangeIndex(start, end, step=1, dtype="int")
1813
+ if periods is not None:
1814
+ forecast_index = pd.RangeIndex(start, start + periods + 1, step=1, dtype="int")
1815
+
1816
+ if is_datetime:
1817
+ if forecast_index.freq != time_index.freq:
1818
+ raise ValueError(
1819
+ "The frequency of the forecast index must match the frequency on the data used "
1820
+ f"to fit the model. Got {forecast_index.freq}, expected {time_index.freq}"
1821
+ )
1822
+
1823
+ if x0_idx is None:
1824
+ x0_idx, forecast_index = forecast_index[0], forecast_index[1:]
1825
+ if x0_idx in forecast_index:
1826
+ raise ValueError("x0_idx should not be in the forecast index")
1827
+ if x0_idx not in time_index:
1828
+ raise ValueError("start must be in the data index used to fit the model.")
1829
+
1830
+ # The starting value should not be included in the forecast index. It will be used only to define x0 and P0,
1831
+ # and no forecast will be associated with it.
1832
+ return x0_idx, forecast_index
1833
+
1834
+ def _finalize_scenario_initialization(
1835
+ self,
1836
+ scenario: pd.DataFrame | np.ndarray | dict[str, pd.DataFrame | np.ndarray] | None,
1837
+ forecast_index: pd.RangeIndex | pd.DatetimeIndex,
1838
+ name=None,
1839
+ ):
1840
+ try:
1841
+ var_to_dims = {key: info["dims"][1:] for key, info in self.data_info.items()}
1842
+ except NotImplementedError:
1843
+ return scenario
1844
+
1845
+ if any(len(dims) > 1 for dims in var_to_dims.values()):
1846
+ raise NotImplementedError(">2d exogenous data is not yet supported.")
1847
+ coords = {
1848
+ var: self._fit_coords[dim[0]]
1849
+ for var, dim in var_to_dims.items()
1850
+ if dim[0] in self._fit_coords
1851
+ }
1852
+
1853
+ if scenario is None:
1854
+ return scenario
1855
+
1856
+ if isinstance(scenario, dict):
1857
+ for name, data in scenario.items():
1858
+ scenario[name] = self._finalize_scenario_initialization(data, forecast_index, name)
1859
+ return scenario
1860
+
1861
+ # This was already checked as valid
1862
+ name = self._exog_names[0] if name is None else name
1863
+
1864
+ # Small tidying up in the case we just have a single scenario that's already a dataframe.
1865
+ if isinstance(scenario, pd.DataFrame | pd.Series):
1866
+ if isinstance(scenario, pd.Series):
1867
+ scenario = scenario.to_frame(name=coords[name][0])
1868
+ if not scenario.index.equals(forecast_index):
1869
+ scenario.index = forecast_index
1870
+
1871
+ # lists and tuples were handled during validation, along with shape check, so just cast arrays to dataframes
1872
+ # with the correct index and columns
1873
+ if isinstance(scenario, np.ndarray):
1874
+ scenario = pd.DataFrame(scenario, index=forecast_index, columns=coords[name])
1875
+
1876
+ return scenario
1877
+
1878
+ def forecast(
1879
+ self,
1880
+ idata: InferenceData,
1881
+ start: int | pd.Timestamp | None = None,
1882
+ periods: int | None = None,
1883
+ end: int | pd.Timestamp = None,
1884
+ scenario: pd.DataFrame | np.ndarray | dict[str, pd.DataFrame | np.ndarray] | None = None,
1885
+ use_scenario_index: bool = False,
1886
+ filter_output="smoothed",
1887
+ random_seed: RandomState | None = None,
1888
+ verbose: bool = True,
1889
+ **kwargs,
1890
+ ) -> InferenceData:
1891
+ """
1892
+ Generate forecasts of state space model trajectories into the future.
1893
+
1894
+ This function combines posterior parameter samples in the provided idata with model dynamics to generate
1895
+ forecasts for out-of-sample data. The trajectory is initialized using the filter output specified in
1896
+ the filter_output argument.
1897
+
1898
+ Parameters
1899
+ ----------
1900
+ idata : InferenceData
1901
+ An Arviz InferenceData object containing the posterior distribution over model parameters.
1902
+
1903
+ start : int or pd.Timestamp, optional
1904
+ The starting date index or time step from which to generate the forecasts. If the data provided to
1905
+ `PyMCStateSpace.build_statespace_graph` had a datetime index, `start` should be a datetime.
1906
+ If using integer time series, `start` should be an integer indicating the starting time step. In either
1907
+ case, `start` should be in the data index used to build the statespace graph.
1908
+
1909
+ If start is None, the last value on the data's index will be used.
1910
+
1911
+ periods : int, optional
1912
+ The number of time steps to forecast into the future. If `periods` is specified, the `end`
1913
+ parameter will be ignored. If `None`, then the `end` parameter must be provided.
1914
+
1915
+ end : int or pd.Timestamp, optional
1916
+ The ending date index or time step up to which to generate the forecasts. If the data provided to
1917
+ `PyMCStateSpace.build_statespace_graph` had a datetime index, `start` should be a datetime.
1918
+ If using integer time series, `end` should be an integer indicating the ending time step.
1919
+ If `end` is provided, the `periods` parameter will be ignored.
1920
+
1921
+ scenario: pd.Dataframe or np.ndarray, optional
1922
+ Exogenous variables to use for scenario-based forecasting. Must be a 2d array-like, with second dimension
1923
+ equal to the number of exogenous variables. If start, end, or periods are specified, the first dimension
1924
+ must conform with these settings. Otherwise, the index of the scenario data will be used to set the
1925
+ number of forecast steps. If the index of the forecast scenairo is a pandas DateTimeIndex, its frequency
1926
+ must match the frequency of the data used to fit the model. Otherwise, dates will be based on the number
1927
+ of forecast steps and the data.
1928
+
1929
+ use_scenario_index: bool, default False
1930
+ If True, the index of the scenario data will be used to determine the forecast period. In this case,
1931
+ the start, end, and periods arguments will be ignored. If True, the scenario data must be a DataFrame,
1932
+ otherwise an error will be raised.
1933
+
1934
+ filter_output : str, default="smoothed"
1935
+ The type of Kalman Filter output used to initialize the forecasts. The 0th timestep of the forecast will
1936
+ be sampled from x[0] ~ N(filter_output_mean[start], filter_output_covariance[start]). Default is "smoothed",
1937
+ which uses past and future data to make the best possible hidden state estimate.
1938
+
1939
+ random_seed : int, RandomState or Generator, optional
1940
+ Seed for the random number generator.
1941
+
1942
+ verbose: bool, default=True
1943
+ Whether to print diagnostic information about forecasting.
1944
+
1945
+ kwargs:
1946
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
1947
+
1948
+ Returns
1949
+ -------
1950
+ InferenceData
1951
+ An Arviz InferenceData object containing forecast samples for the latent and observed state
1952
+ trajectories of the state space model, named "forecast_latent" and "forecast_observed".
1953
+
1954
+ - forecast_latent represents the latent state trajectories `X[t]`, which follows the dynamics:
1955
+ `x[t+1] = T @ x[t] + R @ eta[t]`, where `eta ~ N(0, Q)`.
1956
+
1957
+ - forecast_observed represents the observed state trajectories `Y[t]`, which is obtained from
1958
+ the latent state trajectories: `y[t] = Z @ x[t] + nu[t]`, where `nu ~ N(0, H)`.
1959
+
1960
+ """
1961
+ filter_time_dim = TIME_DIM
1962
+
1963
+ _validate_filter_arg(filter_output)
1964
+ time_index = self._get_fit_time_index()
1965
+
1966
+ if start is None and verbose:
1967
+ _log.warning(
1968
+ "No start date provided. Using the last date in the data index. To silence this warning, "
1969
+ "explicitly pass a start date or set verbose = False"
1970
+ )
1971
+ start = time_index[-1]
1972
+
1973
+ if self._needs_exog_data and not isinstance(scenario, dict):
1974
+ if len(self.data_names) > 1:
1975
+ raise ValueError(
1976
+ "Model needs more than one exogenous data to do forecasting. In this case, you must "
1977
+ "pass a dictionary of scenario data."
1978
+ )
1979
+ [data_name] = self.data_names
1980
+ scenario = {data_name: scenario}
1981
+
1982
+ scenario: dict = self._validate_scenario_data(scenario, verbose=verbose)
1983
+
1984
+ self._validate_forecast_args(
1985
+ time_index=time_index,
1986
+ start=start,
1987
+ end=end,
1988
+ periods=periods,
1989
+ scenario=scenario,
1990
+ use_scenario_index=use_scenario_index,
1991
+ verbose=verbose,
1992
+ )
1993
+
1994
+ t0, forecast_index = self._build_forecast_index(
1995
+ time_index=time_index,
1996
+ start=start,
1997
+ end=end,
1998
+ periods=periods,
1999
+ scenario=scenario,
2000
+ use_scenario_index=use_scenario_index,
2001
+ )
2002
+ scenario = self._finalize_scenario_initialization(scenario, forecast_index)
2003
+ temp_coords = self._fit_coords.copy()
2004
+
2005
+ dims = None
2006
+ if all([dim in temp_coords for dim in [filter_time_dim, ALL_STATE_DIM, OBS_STATE_DIM]]):
2007
+ dims = [TIME_DIM, ALL_STATE_DIM, OBS_STATE_DIM]
2008
+
2009
+ t0_idx = np.flatnonzero(time_index == t0)[0]
2010
+
2011
+ temp_coords["data_time"] = time_index
2012
+ temp_coords[TIME_DIM] = forecast_index
2013
+
2014
+ mu_dims, cov_dims = None, None
2015
+ if all([dim in self._fit_coords for dim in [TIME_DIM, ALL_STATE_DIM, ALL_STATE_AUX_DIM]]):
2016
+ mu_dims = ["data_time", ALL_STATE_DIM]
2017
+ cov_dims = ["data_time", ALL_STATE_DIM, ALL_STATE_AUX_DIM]
2018
+
2019
+ with pm.Model(coords=temp_coords) as forecast_model:
2020
+ (_, _, *matrices), grouped_outputs = self._kalman_filter_outputs_from_dummy_graph(
2021
+ data_dims=["data_time", OBS_STATE_DIM],
2022
+ )
2023
+
2024
+ group_idx = FILTER_OUTPUT_TYPES.index(filter_output)
2025
+ mu, cov = grouped_outputs[group_idx]
2026
+
2027
+ x0 = pm.Deterministic(
2028
+ "x0_slice", mu[t0_idx], dims=mu_dims[1:] if mu_dims is not None else None
2029
+ )
2030
+ P0 = pm.Deterministic(
2031
+ "P0_slice", cov[t0_idx], dims=cov_dims[1:] if cov_dims is not None else None
2032
+ )
2033
+
2034
+ if scenario is not None:
2035
+ sub_dict = {
2036
+ forecast_model[data_name]: pt.as_tensor_variable(
2037
+ scenario.get(data_name), name=data_name
2038
+ )
2039
+ for data_name in self.data_names
2040
+ }
2041
+
2042
+ matrices = graph_replace(matrices, replace=sub_dict, strict=True)
2043
+ [setattr(matrix, "name", name) for name, matrix in zip(MATRIX_NAMES[2:], matrices)]
2044
+
2045
+ _ = LinearGaussianStateSpace(
2046
+ "forecast",
2047
+ x0,
2048
+ P0,
2049
+ *matrices,
2050
+ steps=len(forecast_index),
2051
+ dims=dims,
2052
+ mode=self._fit_mode,
2053
+ sequence_names=self.kalman_filter.seq_names,
2054
+ k_endog=self.k_endog,
2055
+ append_x0=False,
2056
+ )
2057
+
2058
+ forecast_model.rvs_to_initial_values = {
2059
+ k: None for k in forecast_model.rvs_to_initial_values.keys()
2060
+ }
2061
+ frozen_model = freeze_dims_and_data(forecast_model)
2062
+
2063
+ with frozen_model:
2064
+ idata_forecast = pm.sample_posterior_predictive(
2065
+ idata,
2066
+ var_names=["forecast_latent", "forecast_observed"],
2067
+ compile_kwargs={"mode": self._fit_mode},
2068
+ random_seed=random_seed,
2069
+ **kwargs,
2070
+ )
2071
+
2072
+ return idata_forecast.posterior_predictive
2073
+
2074
+ def impulse_response_function(
2075
+ self,
2076
+ idata,
2077
+ n_steps: int = 40,
2078
+ use_posterior_cov: bool = True,
2079
+ shock_size: float | np.ndarray | None = None,
2080
+ shock_cov: np.ndarray | None = None,
2081
+ shock_trajectory: np.ndarray | None = None,
2082
+ orthogonalize_shocks: bool = False,
2083
+ random_seed: RandomState | None = None,
2084
+ **kwargs,
2085
+ ):
2086
+ """
2087
+ Generate impulse response functions (IRF) from state space model dynamics.
2088
+
2089
+ An impulse response function represents the dynamic response of the state space model
2090
+ to an instantaneous shock applied to the system. This function calculates the IRF
2091
+ based on either provided shock specifications or the posterior state covariance matrix.
2092
+
2093
+ Parameters
2094
+ ----------
2095
+ idata : az.InferenceData
2096
+ An Arviz InferenceData object containing the posterior distribution over model parameters.
2097
+
2098
+ n_steps: int
2099
+ The number of time steps to calculate the impulse response. Default is 40.
2100
+
2101
+ If `shock_trajectory` is provided, the length of the shock trajectory will override this value.
2102
+
2103
+ use_posterior_cov: bool, default=True
2104
+ Whether to use the covariance matrix of the posterior distribution to generate the impulse response.
2105
+
2106
+ Only one of `use_posterior_cov`, `shock_cov`, `shock_size`, or `shock_trajectory` can be specified.
2107
+
2108
+ shock_size : Optional[Union[float, np.ndarray]], default=None
2109
+ The size of the shock applied to the system. If specified, it will create a covariance
2110
+ matrix for the shock with diagonal elements equal to `shock_size`. If float, the diagonal will be filled
2111
+ with `shock_size`. If an array, `shock_size` must match the number of shocks in the state space model.
2112
+
2113
+ Only one of `use_posterior_cov`, `shock_cov`, `shock_size`, or `shock_trajectory` can be specified.
2114
+
2115
+ shock_cov : Optional[np.ndarray], default=None
2116
+ A user-specified covariance matrix for the shocks. It should be a 2D numpy array with
2117
+ dimensions (n_shocks, n_shocks), where n_shocks is the number of shocks in the state space model.
2118
+
2119
+ Only one of `use_posterior_cov`, `shock_cov`, `shock_size`, or `shock_trajectory` can be specified.
2120
+
2121
+ shock_trajectory : Optional[np.ndarray], default=None
2122
+ A pre-defined trajectory of shocks applied to the system. It should be a 2D numpy array
2123
+ with dimensions (n, n_shocks), where n is the number of time steps and k_posdef is the
2124
+ number of shocks in the state space model.
2125
+
2126
+ Only one of `use_posterior_cov`, `shock_cov`, `shock_size`, or `shock_trajectory` can be specified.
2127
+
2128
+ orthogonalize_shocks : bool, default=False
2129
+ If True, orthogonalize the shocks using Cholesky decomposition when generating the impulse
2130
+ response. This option is ignored if `shock_trajectory` or `shock_size` are used.
2131
+
2132
+ random_seed : int, RandomState or Generator, optional
2133
+ Seed for the random number generator.
2134
+
2135
+ kwargs:
2136
+ Additional keyword arguments are passed to pymc.sample_posterior_predictive
2137
+
2138
+ Returns
2139
+ -------
2140
+ pm.InferenceData
2141
+ An Arviz InferenceData object containing impulse response function in a variable named "irf".
2142
+ """
2143
+ options = [shock_size, shock_cov, shock_trajectory]
2144
+ n_options = sum(x is not None for x in options)
2145
+ Q = None # No covariance matrix needed if a trajectory is provided. Will be overwritten later if needed.
2146
+
2147
+ if n_options > 1:
2148
+ raise ValueError("Specify exactly 0 or 1 of shock_size, shock_cov, or shock_trajectory")
2149
+ elif n_options == 1:
2150
+ # If the user passed an alternative parameterization for the shocks of the IRF, don't use the posterior
2151
+ use_posterior_cov = False
2152
+
2153
+ if shock_trajectory is not None:
2154
+ # Validate the shock trajectory
2155
+ n, k = shock_trajectory.shape
2156
+ steps = n
2157
+
2158
+ if k != self.k_posdef:
2159
+ raise ValueError(
2160
+ "If shock_trajectory is provided, there must be a trajectory provided for each shock. "
2161
+ f"Model has {self.k_posdef} shocks, but shock_trajectory has only {k} columns"
2162
+ )
2163
+ if steps is not None and steps != n:
2164
+ _log.warning(
2165
+ "Both steps and shock_trajectory were provided but do not agree. Length of "
2166
+ "shock_trajectory will take priority, and steps will be ignored."
2167
+ )
2168
+ n_steps = n # Overwrite steps with the length of the shock trajectory
2169
+ shock_trajectory = pt.as_tensor_variable(shock_trajectory)
2170
+
2171
+ simulation_coords = self._fit_coords.copy()
2172
+ simulation_coords[TIME_DIM] = np.arange(n_steps, dtype="int")
2173
+
2174
+ with pm.Model(coords=simulation_coords):
2175
+ self._build_dummy_graph()
2176
+ self._insert_random_variables()
2177
+
2178
+ P0, _, c, d, T, Z, R, H, post_Q = self.unpack_statespace()
2179
+ x0 = pm.Deterministic("x0_new", pt.zeros(self.k_states), dims=[ALL_STATE_DIM])
2180
+
2181
+ if use_posterior_cov:
2182
+ Q = post_Q
2183
+ if orthogonalize_shocks:
2184
+ Q = pt.linalg.cholesky(Q) / pt.diag(Q)
2185
+ elif shock_cov is not None:
2186
+ Q = pt.as_tensor_variable(shock_cov)
2187
+ if orthogonalize_shocks:
2188
+ Q = pt.linalg.cholesky(Q) / pt.diag(Q)
2189
+
2190
+ if shock_trajectory is None:
2191
+ shock_trajectory = pt.zeros((n_steps, self.k_posdef))
2192
+ if Q is not None:
2193
+ init_shock = MvNormalSVD("initial_shock", mu=0, cov=Q, dims=[SHOCK_DIM])
2194
+ else:
2195
+ init_shock = pm.Deterministic(
2196
+ "initial_shock",
2197
+ pt.as_tensor_variable(np.atleast_1d(shock_size)),
2198
+ dims=[SHOCK_DIM],
2199
+ )
2200
+ shock_trajectory = pt.set_subtensor(shock_trajectory[0], init_shock)
2201
+
2202
+ else:
2203
+ shock_trajectory = pt.as_tensor_variable(shock_trajectory)
2204
+
2205
+ def irf_step(shock, x, c, T, R):
2206
+ next_x = c + T @ x + R @ shock
2207
+ return next_x
2208
+
2209
+ irf, updates = pytensor.scan(
2210
+ irf_step,
2211
+ sequences=[shock_trajectory],
2212
+ outputs_info=[x0],
2213
+ non_sequences=[c, T, R],
2214
+ n_steps=n_steps,
2215
+ strict=True,
2216
+ mode=self._fit_mode,
2217
+ )
2218
+
2219
+ pm.Deterministic("irf", irf, dims=[TIME_DIM, ALL_STATE_DIM])
2220
+
2221
+ compile_kwargs = kwargs.get("compile_kwargs", {})
2222
+ if "mode" not in compile_kwargs.keys():
2223
+ compile_kwargs = {"mode": self._fit_mode}
2224
+ else:
2225
+ mode = compile_kwargs.get("mode")
2226
+ if mode is not None and mode != self._fit_mode:
2227
+ raise ValueError(
2228
+ f"User provided compile mode ({mode}) does not match the compile mode used to "
2229
+ f"construct the model ({self._fit_mode})."
2230
+ )
2231
+
2232
+ compile_kwargs.update({"mode": self._fit_mode})
2233
+
2234
+ irf_idata = pm.sample_posterior_predictive(
2235
+ idata,
2236
+ var_names=["irf"],
2237
+ compile_kwargs=compile_kwargs,
2238
+ random_seed=random_seed,
2239
+ **kwargs,
2240
+ )
2241
+
2242
+ return irf_idata.posterior_predictive
2243
+
2244
+ def _sort_obs_inputs_by_time_varying(self, d, Z):
2245
+ seqs = []
2246
+ non_seqs = []
2247
+
2248
+ for matrix, name in zip([d, Z], ["d", "Z"]):
2249
+ if name in self.kalman_filter.seq_names:
2250
+ seqs.append(matrix)
2251
+ else:
2252
+ non_seqs.append(matrix)
2253
+
2254
+ return seqs, non_seqs
2255
+
2256
+ @staticmethod
2257
+ def _sort_obs_scan_args(args):
2258
+ args = list(args)
2259
+
2260
+ # If a matrix is time-varying, pytensor will put a [t] on the name
2261
+ arg_names = [x.name.replace("[t]", "") for x in args]
2262
+ ordered_args = []
2263
+
2264
+ for name in ["d", "Z"]:
2265
+ idx = arg_names.index(name)
2266
+ ordered_args.append(args[idx])
2267
+
2268
+ return ordered_args