Skip to content

Commit 21c64c6

Browse files
committed
Improve docstrings
1 parent 66e0252 commit 21c64c6

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

pymc_extras/statespace/models/structural/components/autoregressive.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ class AutoregressiveComponent(Component):
1717
If int, the number of lags to include in the model.
1818
If a sequence, an array-like of zeros and ones indicating which lags to include in the model.
1919
20+
name: str, default "auto_regressive"
21+
A name for this autoregressive component. Used to label dimensions and coordinates.
22+
23+
observed_state_names: list[str] | None, default None
24+
List of strings for observed state labels. If None, defaults to ["data"].
25+
2026
Notes
2127
-----
2228
An autoregressive component can be thought of as a way o introducing serially correlated errors into the model.

pymc_extras/statespace/models/structural/components/level_trend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class LevelTrendComponent(Component):
2222
2323
The number of stochastic innovations to include in the model. By default, ``innovations_order = order``
2424
25+
name : str, default "level_trend"
26+
A name for this level-trend component. Used to label dimensions and coordinates.
27+
28+
observed_state_names : list[str] | None, default None
29+
List of strings for observed state labels. If None, defaults to ["data"].
30+
2531
Notes
2632
-----
2733
This class implements the level and trend components of the general structural time series model. In the most

pymc_extras/statespace/models/structural/components/regression.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,99 @@
77

88

99
class RegressionComponent(Component):
10+
r"""
11+
Regression component for exogenous variables in a structural time series model
12+
13+
Parameters
14+
----------
15+
k_exog : int | None, default None
16+
Number of exogenous variables to include in the regression. Must be specified if
17+
state_names is not provided.
18+
19+
name : str | None, default "regression"
20+
A name for this regression component. Used to label dimensions and coordinates.
21+
22+
state_names : list[str] | None, default None
23+
List of strings for regression coefficient labels. If provided, must be of length
24+
k_exog. If None and k_exog is provided, coefficients will be named
25+
"{name}_1, {name}_2, ...".
26+
27+
observed_state_names : list[str] | None, default None
28+
List of strings for observed state labels. If None, defaults to ["data"].
29+
30+
innovations : bool, default False
31+
Whether to include stochastic innovations in the regression coefficients,
32+
allowing them to vary over time. If True, coefficients follow a random walk.
33+
34+
Notes
35+
-----
36+
This component implements regression with exogenous variables in a structural time series
37+
model. The regression component can be expressed as:
38+
39+
.. math::
40+
y_t = \beta_t^T x_t + \epsilon_t
41+
42+
Where :math:`y_t` is the dependent variable, :math:`x_t` is the vector of exogenous
43+
variables, :math:`\beta_t` is the vector of regression coefficients, and :math:`\epsilon_t`
44+
is the error term.
45+
46+
When ``innovations=False`` (default), the coefficients are constant over time:
47+
:math:`\beta_t = \beta_0` for all t.
48+
49+
When ``innovations=True``, the coefficients follow a random walk:
50+
:math:`\beta_{t+1} = \beta_t + \eta_t`, where :math:`\eta_t \sim N(0, \Sigma_\beta)`.
51+
52+
The component supports both univariate and multivariate regression. In the multivariate
53+
case, separate coefficients are estimated for each endogenous variable (i.e time series).
54+
55+
Examples
56+
--------
57+
Simple regression with constant coefficients:
58+
59+
.. code:: python
60+
61+
from pymc_extras.statespace import structural as st
62+
import pymc as pm
63+
import pytensor.tensor as pt
64+
65+
trend = st.LevelTrendComponent(order=1, innovations_order=1)
66+
regression = st.RegressionComponent(k_exog=2, state_names=['intercept', 'slope'])
67+
ss_mod = (trend + regression).build()
68+
69+
with pm.Model(coords=ss_mod.coords) as model:
70+
# Prior for regression coefficients
71+
betas = pm.Normal('betas', dims=ss_mod.param_dims['beta_regression'])
72+
73+
# Prior for trend innovations
74+
sigma_trend = pm.Exponential('sigma_trend', 1)
75+
76+
ss_mod.build_statespace_graph(data)
77+
idata = pm.sample()
78+
79+
Multivariate regression with time-varying coefficients:
80+
- There are 2 exogenous variables (price and income effects)
81+
- There are 2 endogenous variables (sales and revenue)
82+
- The regression coefficients are allowed to vary over time (`innovations=True`)
83+
84+
.. code:: python
85+
86+
regression = st.RegressionComponent(
87+
k_exog=2,
88+
state_names=['price_effect', 'income_effect'],
89+
observed_state_names=['sales', 'revenue'],
90+
innovations=True
91+
)
92+
93+
with pm.Model(coords=ss_mod.coords) as model:
94+
betas = pm.Normal('betas', dims=ss_mod.param_dims['beta_regression'])
95+
96+
# Innovation variance for time-varying coefficients
97+
sigma_beta = pm.Exponential('sigma_beta', 1, dims=ss_mod.param_dims['sigma_beta_regression'])
98+
99+
ss_mod.build_statespace_graph(data)
100+
idata = pm.sample()
101+
"""
102+
10103
def __init__(
11104
self,
12105
k_exog: int | None = None,

pymc_extras/statespace/models/structural/components/seasonality.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class TimeSeasonality(Component):
3636
included in the model, but it will not be identified -- you will need to handle this in the priors (e.g. with
3737
ZeroSumNormal).
3838
39+
observed_state_names: list[str] | None, default None
40+
List of strings for observed state labels. If None, defaults to ["data"].
41+
3942
Notes
4043
-----
4144
A seasonal effect is any pattern that repeats every fixed interval. Although there are many possible ways to
@@ -274,6 +277,9 @@ class FrequencySeasonality(Component):
274277
innovations: bool, default True
275278
Whether to include stochastic innovations in the strength of the seasonal effect
276279
280+
observed_state_names: list[str] | None, default None
281+
List of strings for observed state labels. If None, defaults to ["data"].
282+
277283
Notes
278284
-----
279285
A seasonal effect is any pattern that repeats every fixed interval. Although there are many possible ways to

0 commit comments

Comments
 (0)