@@ -25,13 +25,12 @@ run_control:
25
25
slideshow:
26
26
slide_type: '-'
27
27
---
28
- %pylab inline
29
28
import arviz as az
30
- import pymc3 as pm
31
- import scipy
32
- import theano.tensor as tt
33
-
34
- from pymc3.distributions.timeseries import EulerMaruyama
29
+ import matplotlib.pyplot as plt
30
+ import numpy as np
31
+ import pymc as pm
32
+ import pytensor.tensor as pt
33
+ import scipy as sp
35
34
```
36
35
37
36
``` {code-cell} ipython3
@@ -57,8 +56,8 @@ run_control:
57
56
read_only: false
58
57
---
59
58
# parameters
60
- λ = -0.78
61
- σ2 = 5e-3
59
+ lam = -0.78
60
+ s2 = 5e-3
62
61
N = 200
63
62
dt = 1e-1
64
63
@@ -68,13 +67,13 @@ x_t = []
68
67
69
68
# simulate
70
69
for i in range(N):
71
- x += dt * λ * x + sqrt(dt) * σ2 * randn()
70
+ x += dt * lam * x + np. sqrt(dt) * s2 * np.random. randn()
72
71
x_t.append(x)
73
72
74
- x_t = array(x_t)
73
+ x_t = np. array(x_t)
75
74
76
75
# z_t noisy observation
77
- z_t = x_t + randn(x_t.size) * 5e-3
76
+ z_t = x_t + np.random. randn(x_t.size) * 5e-3
78
77
```
79
78
80
79
``` {code-cell} ipython3
@@ -88,14 +87,16 @@ run_control:
88
87
slideshow:
89
88
slide_type: subslide
90
89
---
91
- figure(figsize=(10, 3))
92
- subplot(121)
93
- plot(x_t[:30], "k", label="$x(t)$", alpha=0.5), plot(z_t[:30], "r", label="$z(t)$", alpha=0.5)
94
- title("Transient"), legend()
95
- subplot(122)
96
- plot(x_t[30:], "k", label="$x(t)$", alpha=0.5), plot(z_t[30:], "r", label="$z(t)$", alpha=0.5)
97
- title("All time")
98
- tight_layout()
90
+ plt.figure(figsize=(10, 3))
91
+ plt.plot(x_t[:30], "k", label="$x(t)$", alpha=0.5)
92
+ plt.plot(z_t[:30], "r", label="$z(t)$", alpha=0.5)
93
+ plt.title("Transient")
94
+ plt.legend()
95
+ plt.subplot(122)
96
+ plt.plot(x_t[30:], "k", label="$x(t)$", alpha=0.5)
97
+ plt.plot(z_t[30:], "r", label="$z(t)$", alpha=0.5)
98
+ plt.title("All time")
99
+ plt.legend();
99
100
```
100
101
101
102
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}}
@@ -114,7 +115,7 @@ run_control:
114
115
read_only: false
115
116
---
116
117
def lin_sde(x, lam):
117
- return lam * x, σ2
118
+ return lam * x, s2
118
119
```
119
120
120
121
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}, "slideshow": {"slide_type": "subslide"}}
@@ -134,11 +135,11 @@ slideshow:
134
135
---
135
136
with pm.Model() as model:
136
137
# uniform prior, but we know it must be negative
137
- lam = pm.Flat("lam ")
138
+ l = pm.Flat("l ")
138
139
139
140
# "hidden states" following a linear SDE distribution
140
141
# parametrized by time step (det. variable) and lam (random variable)
141
- xh = EulerMaruyama("xh", dt, lin_sde, (lam ,), shape=N, testval=x_t )
142
+ xh = pm. EulerMaruyama("xh", dt=dt, sde_fn= lin_sde, sde_pars=(l ,), shape=N)
142
143
143
144
# predicted observation
144
145
zh = pm.Normal("zh", mu=xh, sigma=5e-3, observed=z_t)
@@ -156,32 +157,28 @@ run_control:
156
157
read_only: false
157
158
---
158
159
with model:
159
- trace = pm.sample(2000, tune=1000 )
160
+ trace = pm.sample()
160
161
```
161
162
162
163
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}, "slideshow": {"slide_type": "subslide"}}
163
164
164
165
Next, we plot some basic statistics on the samples from the posterior,
165
166
166
167
``` {code-cell} ipython3
167
- ---
168
- button: false
169
- nbpresent:
170
- id: 925f1829-24cb-4c28-9b6b-7e9c9e86f2fd
171
- new_sheet: false
172
- run_control:
173
- read_only: false
174
- ---
175
- figure(figsize=(10, 3))
176
- subplot(121)
177
- plot(percentile(trace[xh], [2.5, 97.5], axis=0).T, "k", label=r"$\hat{x}_{95\%}(t)$")
178
- plot(x_t, "r", label="$x(t)$")
179
- legend()
180
-
181
- subplot(122)
182
- hist(trace[lam], 30, label=r"$\hat{\lambda}$", alpha=0.5)
183
- axvline(λ, color="r", label=r"$\lambda$", alpha=0.5)
184
- legend();
168
+ plt.figure(figsize=(10, 3))
169
+ plt.subplot(121)
170
+ plt.plot(
171
+ trace.posterior.quantile((0.025, 0.975), dim=("chain", "draw"))["xh"].values.T,
172
+ "k",
173
+ label=r"$\hat{x}_{95\%}(t)$",
174
+ )
175
+ plt.plot(x_t, "r", label="$x(t)$")
176
+ plt.legend()
177
+
178
+ plt.subplot(122)
179
+ plt.hist(az.extract(trace.posterior)["l"], 30, label=r"$\hat{\lambda}$", alpha=0.5)
180
+ plt.axvline(lam, color="r", label=r"$\lambda$", alpha=0.5)
181
+ plt.legend();
185
182
```
186
183
187
184
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}, "slideshow": {"slide_type": "subslide"}}
@@ -194,20 +191,20 @@ In other words, we
194
191
- check that the new observations fit with the original data
195
192
196
193
``` {code-cell} ipython3
197
- ---
198
- button: false
199
- new_sheet: false
200
- run_control:
201
- read_only: false
202
- ---
203
194
# generate trace from posterior
204
- ppc_trace = pm.sample_posterior_predictive(trace, model=model)
195
+ with model:
196
+ pm.sample_posterior_predictive(trace, extend_inferencedata=True)
197
+ ```
205
198
206
- # plot with data
207
- figure(figsize=(10, 3))
208
- plot(percentile(ppc_trace["zh"], [2.5, 97.5], axis=0).T, "k", label=r"$z_{95\% PP}(t)$")
209
- plot(z_t, "r", label="$z(t)$")
210
- legend()
199
+ ``` {code-cell} ipython3
200
+ plt.figure(figsize=(10, 3))
201
+ plt.plot(
202
+ trace.posterior_predictive.quantile((0.025, 0.975), dim=("chain", "draw"))["zh"].values.T,
203
+ "k",
204
+ label=r"$z_{95\% PP}(t)$",
205
+ )
206
+ plt.plot(z_t, "r", label="$z(t)$")
207
+ plt.legend();
211
208
```
212
209
213
210
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}}
@@ -231,28 +228,22 @@ As the next model, let's use a 2D deterministic oscillator,
231
228
with noisy observation $z(t) = m x + (1 - m) y + N(0, 0.05)$.
232
229
233
230
``` {code-cell} ipython3
234
- ---
235
- button: false
236
- new_sheet: false
237
- run_control:
238
- read_only: false
239
- ---
240
- N, τ, a, m, σ2 = 200, 3.0, 1.05, 0.2, 1e-1
231
+ N, tau, a, m, s2 = 200, 3.0, 1.05, 0.2, 1e-1
241
232
xs, ys = [0.0], [1.0]
242
233
for i in range(N):
243
234
x, y = xs[-1], ys[-1]
244
- dx = τ * (x - x**3.0 / 3.0 + y)
245
- dy = (1.0 / τ ) * (a - x)
246
- xs.append(x + dt * dx + sqrt(dt) * σ2 * randn())
247
- ys.append(y + dt * dy + sqrt(dt) * σ2 * randn())
248
- xs, ys = array(xs), array(ys)
249
- zs = m * xs + (1 - m) * ys + randn(xs.size) * 0.1
250
-
251
- figure(figsize=(10, 2))
252
- plot(xs, label="$x(t)$")
253
- plot(ys, label="$y(t)$")
254
- plot(zs, label="$z(t)$")
255
- legend()
235
+ dx = tau * (x - x**3.0 / 3.0 + y)
236
+ dy = (1.0 / tau ) * (a - x)
237
+ xs.append(x + dt * dx + np. sqrt(dt) * s2 * np.random. randn())
238
+ ys.append(y + dt * dy + np. sqrt(dt) * s2 * np.random. randn())
239
+ xs, ys = np. array(xs), np. array(ys)
240
+ zs = m * xs + (1 - m) * ys + np.random. randn(xs.size) * 0.1
241
+
242
+ plt. figure(figsize=(10, 2))
243
+ plt. plot(xs, label="$x(t)$")
244
+ plt. plot(ys, label="$y(t)$")
245
+ plt. plot(zs, label="$z(t)$")
246
+ plt. legend()
256
247
```
257
248
258
249
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}, "slideshow": {"slide_type": "subslide"}}
@@ -268,12 +259,12 @@ new_sheet: false
268
259
run_control:
269
260
read_only: false
270
261
---
271
- def osc_sde(xy, τ , a):
262
+ def osc_sde(xy, tau , a):
272
263
x, y = xy[:, 0], xy[:, 1]
273
- dx = τ * (x - x**3.0 / 3.0 + y)
274
- dy = (1.0 / τ ) * (a - x)
275
- dxy = tt .stack([dx, dy], axis=0).T
276
- return dxy, σ2
264
+ dx = tau * (x - x**3.0 / 3.0 + y)
265
+ dy = (1.0 / tau ) * (a - x)
266
+ dxy = pt .stack([dx, dy], axis=0).T
267
+ return dxy, s2
277
268
```
278
269
279
270
+++ {"button": false, "new_sheet": false, "run_control": {"read_only": false}}
@@ -291,14 +282,20 @@ new_sheet: false
291
282
run_control:
292
283
read_only: false
293
284
---
294
- xys = c_[xs, ys]
285
+ xys = np. c_[xs, ys]
295
286
296
287
with pm.Model() as model:
297
- τh = pm.Uniform("τh", lower=0.1, upper=5.0)
298
- ah = pm.Uniform("ah", lower=0.5, upper=1.5)
299
- mh = pm.Uniform("mh", lower=0.0, upper=1.0)
300
- xyh = EulerMaruyama("xyh", dt, osc_sde, (τh, ah), shape=xys.shape, testval=xys)
301
- zh = pm.Normal("zh", mu=mh * xyh[:, 0] + (1 - mh) * xyh[:, 1], sigma=0.1, observed=zs)
288
+ tau_h = pm.Uniform("tau_h", lower=0.1, upper=5.0)
289
+ a_h = pm.Uniform("a_h", lower=0.5, upper=1.5)
290
+ m_h = pm.Uniform("m_h", lower=0.0, upper=1.0)
291
+ xy_h = pm.EulerMaruyama(
292
+ "xy_h", dt=dt, sde_fn=osc_sde, sde_pars=(tau_h, a_h), shape=xys.shape, initval=xys
293
+ )
294
+ zh = pm.Normal("zh", mu=m_h * xy_h[:, 0] + (1 - m_h) * xy_h[:, 1], sigma=0.1, observed=zs)
295
+ ```
296
+
297
+ ``` {code-cell} ipython3
298
+ pm.__version__
302
299
```
303
300
304
301
``` {code-cell} ipython3
0 commit comments