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
In addition to what's included in base Anaconda, we need to install the following packages
26
20
27
21
```{code-cell} ipython3
28
22
:tags: [hide-output]
29
23
30
-
!pip install arviz pymc
24
+
!pip install numpyro
31
25
```
32
26
33
27
We'll begin with some Python imports.
34
28
35
29
```{code-cell} ipython3
36
-
37
-
import arviz as az
38
-
import pymc as pmc
39
30
import numpyro
40
31
from numpyro import distributions as dist
41
32
42
33
import numpy as np
43
34
import jax.numpy as jnp
44
35
from jax import random
45
36
import matplotlib.pyplot as plt
46
-
47
-
import logging
48
-
logging.basicConfig()
49
-
logger = logging.getLogger('pymc')
50
-
logger.setLevel(logging.CRITICAL)
51
-
52
37
```
53
38
54
-
This lecture uses Bayesian methods offered by [pymc](https://www.pymc.io/projects/docs/en/stable/) and [numpyro](https://num.pyro.ai/en/stable/) to make statistical inferences about two parameters of a univariate first-order autoregression.
39
+
This lecture uses Bayesian methods offered by [numpyro](https://num.pyro.ai/en/stable/) to make statistical inferences about two parameters of a univariate first-order autoregression.
55
40
56
41
57
42
The model is a good laboratory for illustrating
@@ -74,15 +59,14 @@ $\{\epsilon_{t+1}\}$ is a sequence of i.i.d. normal random variables with mean $
74
59
The second component of the statistical model is
75
60
76
61
$$
77
-
y_0 \sim {\cal N}(\mu_0, \sigma_0^2)
62
+
y_0 \sim {\mathcal{N}}(\mu_0, \sigma_0^2)
78
63
$$ (eq:themodel_2)
79
64
80
65
81
66
82
67
Consider a sample $\{y_t\}_{t=0}^T$ governed by this statistical model.
83
68
84
-
The model
85
-
implies that the likelihood function of $\{y_t\}_{t=0}^T$ can be **factored**:
69
+
The model implies that the likelihood function of $\{y_t\}_{t=0}^T$ can be **factored**:
@@ -115,7 +99,7 @@ Unknown parameters are $\rho, \sigma_x$.
115
99
116
100
We have independent **prior probability distributions** for $\rho, \sigma_x$ and want to compute a posterior probability distribution after observing a sample $\{y_{t}\}_{t=0}^T$.
117
101
118
-
The notebook uses `pymc4` and `numpyro` to compute a posterior distribution of $\rho, \sigma_x$. We will use NUTS samplers to generate samples from the posterior in a chain. Both of these libraries support NUTS samplers.
102
+
The notebook uses `numpyro` to compute a posterior distribution of $\rho, \sigma_x$. We will use NUTS samplers to generate samples from the posterior in a chain.
119
103
120
104
NUTS is a form of Monte Carlo Markov Chain (MCMC) algorithm that bypasses random walk behaviour and allows for convergence to a target distribution more quickly. This not only has the advantage of speed, but allows for complex models to be fitted without having to employ specialised knowledge regarding the theory underlying those fitting methods.
121
105
@@ -124,7 +108,7 @@ Thus, we explore consequences of making these alternative assumptions about the
124
108
- A first procedure is to condition on whatever value of $y_0$ is observed. This amounts to assuming that the probability distribution of the random variable $y_0$ is a Dirac delta function that puts probability one on the observed value of $y_0$.
125
109
126
110
- A second procedure assumes that $y_0$ is drawn from the stationary distribution of a process described by {eq}`eq:themodel`
127
-
so that $y_0 \sim {\cal N} \left(0, {\sigma_x^2\over (1-\rho)^2} \right)$
111
+
so that $y_0 \sim {\mathcal{N}} \left(0, \frac{\sigma_x^2}{(1-\rho)^2} \right)$
128
112
129
113
When the initial value $y_0$ is far out in a tail of the stationary distribution, conditioning on an initial value gives a posterior that is **more accurate** in a sense that we'll explain.
130
114
@@ -145,26 +129,25 @@ How we select the initial value $y_0$ matters.
145
129
To illustrate the issue, we'll begin by choosing an initial $y_0$ that is far out in a tail of the stationary distribution.
146
130
147
131
```{code-cell} ipython3
148
-
149
-
def ar1_simulate(rho, sigma, y0, T):
132
+
def ar1_simulate(ρ, σ, y0, T):
150
133
151
134
# Allocate space and draw epsilons
152
135
y = np.empty(T)
153
-
eps = np.random.normal(0.,sigma,T)
136
+
eps = np.random.normal(0., σ, T)
154
137
155
138
# Initial condition and step forward
156
139
y[0] = y0
157
140
for t in range(1, T):
158
-
y[t] = rho*y[t-1] + eps[t]
141
+
y[t] = ρ * y[t-1] + eps[t]
159
142
160
143
return y
161
144
162
-
sigma = 1.
163
-
rho = 0.5
145
+
σ = 1.0
146
+
ρ = 0.5
164
147
T = 50
165
148
166
149
np.random.seed(145353452)
167
-
y = ar1_simulate(rho, sigma, 10, T)
150
+
y = ar1_simulate(ρ, σ, 10, T)
168
151
```
169
152
170
153
```{code-cell} ipython3
@@ -176,174 +159,60 @@ Now we shall use Bayes' law to construct a posterior distribution, conditioning
176
159
177
160
(Later we'll assume that $y_0$ is drawn from the stationary distribution, but not now.)
[pmc.sample](https://www.pymc.io/projects/docs/en/v5.10.0/api/generated/pymc.sample.html#pymc-sample) by default uses the NUTS samplers to generate samples as shown in the below cell:
Please note how the posterior for $\rho$ has shifted to the right relative to when we conditioned on $y_0$ instead of assuming that $y_0$ is drawn from the stationary distribution.
283
-
284
-
Think about why this happens.
285
-
286
-
```{hint}
287
-
It is connected to how Bayes Law (conditional probability) solves an **inverse problem** by putting high probability on parameter values
288
-
that make observations more likely.
289
-
```
290
-
291
-
We'll return to this issue after we use `numpyro` to compute posteriors under our two alternative assumptions about the distribution of $y_0$.
292
-
293
-
We'll now repeat the calculations using `numpyro`.
294
-
295
-
## Numpyro Implementation
164
+
First, we'll implement the AR(1) model conditioning on the initial value using NumPyro. The NUTS sampler is used to generate samples from the posterior distribution.
Please note how the posterior for $\rho$ has shifted to the right relative to when we conditioned on $y_0$ instead of assuming that $y_0$ is drawn from the stationary distribution.
291
+
292
+
Think about why this happens.
293
+
294
+
```{hint}
295
+
It is connected to how Bayes Law (conditional probability) solves an **inverse problem** by putting high probability on parameter values
296
+
that make observations more likely.
297
+
```
298
+
409
299
Look what happened to the posterior!
410
300
411
301
It has moved far from the true values of the parameters used to generate the data because of how Bayes' Law (i.e., conditional probability)
0 commit comments