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.ipynb
+59-5Lines changed: 59 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -16,12 +16,32 @@
16
16
":::"
17
17
]
18
18
},
19
+
{
20
+
"cell_type": "markdown",
21
+
"id": "91d1fcf3",
22
+
"metadata": {},
23
+
"source": [
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 {class}`~pytensor.scan` to loop efficiently inside a PyMC model.\n",
25
+
"\n",
26
+
"For this example, we consider an autoregressive model AR(2). Recall that an AR(2) model is defined as:\n",
"That is, we have a recursive linear model in term of the first two lags of the time series."
35
+
]
36
+
},
19
37
{
20
38
"cell_type": "code",
21
39
"execution_count": 1,
22
40
"id": "elect-softball",
23
41
"metadata": {
24
-
"tags": []
42
+
"tags": [
43
+
"hide-input"
44
+
]
25
45
},
26
46
"outputs": [
27
47
{
@@ -52,18 +72,30 @@
52
72
"rng = np.random.default_rng(42)"
53
73
]
54
74
},
75
+
{
76
+
"cell_type": "markdown",
77
+
"id": "dd1d7055",
78
+
"metadata": {},
79
+
"source": [
80
+
"## Define AR(2) Process\n",
81
+
"\n",
82
+
"We start by encoding the generative graph of the AR(2) model as a function `ar_dist`. The strategy is to pass this function as a custom distribution via {class}`pm.CustomDist` inside a PyMC model. \n",
83
+
"\n",
84
+
"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 {class}`~pytensor.scan` operation."
85
+
]
86
+
},
55
87
{
56
88
"cell_type": "code",
57
89
"execution_count": 2,
58
90
"id": "25029181",
59
91
"metadata": {},
60
92
"outputs": [],
61
93
"source": [
62
-
"lags = 2\n",
63
-
"trials = 100\n",
94
+
"lags = 2 # Number of lags\n",
95
+
"trials = 100 # Time series length\n",
64
96
"\n",
65
97
"\n",
66
-
"def ar_dist(ar_init, rho, sigma, size):\n",
98
+
"def ar_dist(ar_init, rho, sigma):\n",
67
99
" def ar_step(x_tm2, x_tm1, rho, sigma):\n",
68
100
" mu = x_tm1 * rho[0] + x_tm2 * rho[1]\n",
69
101
" x = mu + pm.Normal.dist(sigma=sigma)\n",
@@ -80,6 +112,16 @@
80
112
" return ar_innov"
81
113
]
82
114
},
115
+
{
116
+
"cell_type": "markdown",
117
+
"id": "9af4ec18",
118
+
"metadata": {},
119
+
"source": [
120
+
"## Generate AR(2) Graph\n",
121
+
"\n",
122
+
"Now that we have implemented the AR(2) step, we can assign priors to the parameters `rho` and `sigma`. We also specify the initial state `ar_init` as a zero vector."
123
+
]
124
+
},
83
125
{
84
126
"cell_type": "code",
85
127
"execution_count": 3,
@@ -261,7 +303,9 @@
261
303
"id": "7346db65",
262
304
"metadata": {},
263
305
"source": [
264
-
"## Prior"
306
+
"## Prior\n",
307
+
"\n",
308
+
"Let's sample from the prior distribution to see how the AR(2) model behaves."
Copy file name to clipboardExpand all lines: examples/time_series/Time_Series_Generative_Graph.myst.md
+35-3Lines changed: 35 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,23 @@ kernelspec:
19
19
:author: Juan Orduz and Ricardo Vieira
20
20
:::
21
21
22
+
+++
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 {class}`~pytensor.scan` to loop efficiently inside a PyMC model.
25
+
26
+
For this example, we consider an autoregressive model AR(2). Recall that an AR(2) model is defined as:
We start by encoding the generative graph of the AR(2) model as a function `ar_dist`. The strategy is to pass this function as a custom distribution via {class}`pm.CustomDist` inside a PyMC model.
61
+
62
+
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 {class}`~pytensor.scan` operation.
Now that we have implemented the AR(2) step, we can assign priors to the parameters `rho` and `sigma`. We also specify the initial state `ar_init` as a zero vector.
89
+
64
90
```{code-cell} ipython3
65
91
coords = {
66
92
"lags": range(-lags, 0),
@@ -95,6 +121,8 @@ pm.model_to_graphviz(model)
95
121
96
122
## Prior
97
123
124
+
Let's sample from the prior distribution to see how the AR(2) model behaves.
0 commit comments