You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/time_series/Time_Series_Generative_Graph.myst.md
+35-1Lines changed: 35 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,19 @@ kernelspec:
21
21
22
22
+++
23
23
24
-
In This notebook, we show to model and fit a time series model starting from a generative graph. In particular, we explain how to use {func}`~pytensor.scan.basic.scan` to loop efficiently inside a PyMC model.
24
+
In this notebook, we show how to model and fit a time series model starting from a generative graph. In particular, we explain how to use {func}`~pytensor.scan.basic.scan` to loop efficiently inside a PyMC model.
25
+
26
+
:::{admonition} **Motivation**
27
+
:class: note
28
+
29
+
Why would we do that, instead of just using {class}`~pm.distributions.timeseries.AR`? What are the benefits?
30
+
31
+
The pre-built time series models in PyMC are very useful and easy to use. Nevertheless, they are not flexible enough to model more complex time series models. By using a generative graph, we can model any time series model we want, as long as we can define it in terms of a generative graph. For example:
32
+
33
+
- Auto-regressive models with different noise distribution (e.g. {class}`~pm.distributions.continuous.StudentT` noise).
34
+
- Exponential smoothing models.
35
+
- ARIMA-GARCH models.
36
+
:::
25
37
26
38
For this example, we consider an autoregressive model AR(2). Recall that an AR(2) model is defined as:
27
39
@@ -61,17 +73,39 @@ We start by encoding the generative graph of the AR(2) model as a function `ar_d
61
73
62
74
We need to specify the initial state (`ar_init`), the autoregressive coefficients (`rho`), and the standard deviation of the noise (`sigma`). Given such parameters, we can define the generative graph of the AR(2) model using the {func}`~pytensor.scan.basic.scan` operation.
63
75
76
+
:::{admonition} **What are all of these functions?**
77
+
:class: note
78
+
79
+
At first, it might seem a bit overwhelming to see all these functions. However, they are just helper functions to define the generative graph of the AR(2) model.
80
+
81
+
- {func}`~pymc.pytensorfcollect_default_updates` tells PyMC that the random variable (RV) in the generative graph should be updated in every iteration of the loop.
82
+
83
+
- {func}`~pytensor.scan.basic.scan` is an efficient way to loop inside a PyMC model. It is similar to the `for` loop in Python, but it is optimized for `pytensor`. We need to specify the following arguments:
84
+
85
+
-`fn`: The function that defines the transition steep.
86
+
-`outputs_info`: The is the list of variables or dictionaries describing the initial state of the outputs computed recurrently.
87
+
-`non_sequences`: The list of arguments that are passed to `fn` at each steps. In this case are the autoregressive coefficients and the noise standard deviation of the AR(2) model.
88
+
-`n_steps`: The number of steps to loop.
89
+
-`strict`: If `True`, all the shared variables used in `fn` must be provided as a part of `non_sequences` or `sequences` (In this example we do not use the argument `sequences`, which is the list of variables or dictionaries describing the sequences `scan` has to iterate over. In this case we can simply loop over the time steps).
90
+
:::
91
+
92
+
Let's see concrete implementations:
93
+
64
94
```{code-cell} ipython3
65
95
lags = 2 # Number of lags
66
96
trials = 100 # Time series length
67
97
68
98
69
99
def ar_dist(ar_init, rho, sigma, size):
100
+
# This is the transition function for the AR(2) model.
101
+
# We take as inputs previous steps and then specify the autoregressive relationship.
0 commit comments