Skip to content

Commit 00b0760

Browse files
authored
FIX: Adjustments for main branch to compile (#237)
* FIX: Add arviz to environment * FIX: headers in bayes_nonconj * FIX: equation references * FIX: headers in prob_matrix * FIX: add install for packages outside env * FIX: Adjust prob_matrix for PDF * fix equations in ar1_turningpoint * FIX: header in ar1_bayes * FIX: $ duplicate * FIX: math * fix math * Adjust ar1_turningpts * Remove offending TeX for pdflatex builds
1 parent a6b1491 commit 00b0760

File tree

6 files changed

+130
-213
lines changed

6 files changed

+130
-213
lines changed

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies:
1414
- ghp-import==1.1.0
1515
- sphinxcontrib-youtube==1.1.0
1616
- sphinx-togglebutton==0.3.1
17+
- arviz==0.12.1
1718
# Sandpit Requirements
1819
- quantecon
1920
- array-to-latex

lectures/ar1_bayes.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ kernelspec:
1111
name: python3
1212
---
1313

14-
## Posterior Distributions for AR(1) Parameters
15-
16-
14+
# Posterior Distributions for AR(1) Parameters
1715

1816
We'll begin with some Python imports.
1917

@@ -174,7 +172,7 @@ Now we shall use Bayes' law to construct a posterior distribution, conditioning
174172
175173
First we'll use **pymc4**.
176174
177-
### `PyMC` Implementation
175+
## `PyMC` Implementation
178176
179177
For a normal distribution in `pymc`,
180178
$var = 1/\tau = \sigma^{2}$.
@@ -286,7 +284,7 @@ We'll return to this issue after we use `numpyro` to compute posteriors under ou
286284
287285
We'll now repeat the calculations using `numpyro`.
288286
289-
### `Numpyro` Implementation
287+
## `Numpyro` Implementation
290288
291289
```{code-cell} ipython3
292290

lectures/ar1_turningpts.md

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ kernelspec:
1313

1414
# Forecasting an AR(1) process
1515

16+
```{code-cell} ipython3
17+
:tags: [hide-output]
18+
19+
!pip install arviz pymc
20+
```
21+
1622
This lecture describes methods for forecasting statistics that are functions of future values of a univariate autogressive process.
1723

1824
The methods are designed to take into account two possible sources of uncertainty about these statistics:
@@ -25,7 +31,7 @@ We consider two sorts of statistics:
2531

2632
- prospective values $y_{t+j}$ of a random process $\{y_t\}$ that is governed by the AR(1) process
2733

28-
- sample path properties that are defined as non-linear functions of future values $\{y_{t+j}\}_{j\geq 1}$ at time $t$.
34+
- sample path properties that are defined as non-linear functions of future values $\{y_{t+j}\}_{j \geq 1}$ at time $t$.
2935

3036
**Sample path properties** are things like "time to next turning point" or "time to next recession"
3137

@@ -54,54 +60,48 @@ logger = logging.getLogger('pymc')
5460
logger.setLevel(logging.CRITICAL)
5561
```
5662

57-
## A Univariate First-Order Autoregressive Process
58-
63+
## A Univariate First-Order Autoregressive Process
5964

6065
Consider the univariate AR(1) model:
6166

6267
$$
6368
y_{t+1} = \rho y_t + \sigma \epsilon_{t+1}, \quad t \geq 0
64-
$$ (eq1)
69+
$$ (ar1-tp-eq1)
6570
6671
where the scalars $\rho$ and $\sigma$ satisfy $|\rho| < 1$ and $\sigma > 0$;
6772
$\{\epsilon_{t+1}\}$ is a sequence of i.i.d. normal random variables with mean $0$ and variance $1$.
6873
69-
The initial condition $y_{0}$ is a known number.
74+
The initial condition $y_{0}$ is a known number.
7075
71-
Equation {eq}`eq1` implies that for $t \geq 0$, the conditional density of $y_{t+1}$ is
76+
Equation {eq}`ar1-tp-eq1` implies that for $t \geq 0$, the conditional density of $y_{t+1}$ is
7277
7378
$$
7479
f(y_{t+1} | y_{t}; \rho, \sigma) \sim {\mathcal N}(\rho y_{t}, \sigma^2) \
75-
$$ (eq2)
76-
77-
78-
Further, equation {eq}`eq1` also implies that for $t \geq 0$, the conditional density of $y_{t+j}$ for $j \geq 1$ is
80+
$$ (ar1-tp-eq2)
7981
82+
Further, equation {eq}`ar1-tp-eq1` also implies that for $t \geq 0$, the conditional density of $y_{t+j}$ for $j \geq 1$ is
8083
8184
$$
8285
f(y_{t+j} | y_{t}; \rho, \sigma) \sim {\mathcal N}\left(\rho^j y_{t}, \sigma^2 \frac{1 - \rho^{2j}}{1 - \rho^2} \right)
83-
$$ (eq3)
86+
$$ (ar1-tp-eq3)
8487
85-
86-
The predictive distribution {eq}`eq3` that assumes that the parameters $\rho, \sigma$ are known, which we express
88+
The predictive distribution {eq}`ar1-tp-eq3` that assumes that the parameters $\rho, \sigma$ are known, which we express
8789
by conditioning on them.
8890
8991
We also want to compute a predictive distribution that does not condition on $\rho,\sigma$ but instead takes account of our uncertainty about them.
9092
91-
We form this predictive distribution by integrating {eq}`eq3` with respect to a joint posterior distribution $\pi_t(\rho,\sigma | y^t )$
93+
We form this predictive distribution by integrating {eq}`ar1-tp-eq3` with respect to a joint posterior distribution $\pi_t(\rho,\sigma | y^t)$
9294
that conditions on an observed history $y^t = \{y_s\}_{s=0}^t$:
9395
9496
$$
9597
f(y_{t+j} | y^t) = \int f(y_{t+j} | y_{t}; \rho, \sigma) \pi_t(\rho,\sigma | y^t ) d \rho d \sigma
96-
$$ (eq4)
97-
98-
98+
$$ (ar1-tp-eq4)
9999
100-
Predictive distribution {eq}`eq3` assumes that parameters $(\rho,\sigma)$ are known.
100+
Predictive distribution {eq}`ar1-tp-eq3` assumes that parameters $(\rho,\sigma)$ are known.
101101
102-
Predictive distribution {eq}`eq4` assumes that parameters $(\rho,\sigma)$ are uncertain, but have known probability distribution $\pi_t(\rho,\sigma | y^t )$.
102+
Predictive distribution {eq}`ar1-tp-eq4` assumes that parameters $(\rho,\sigma)$ are uncertain, but have known probability distribution $\pi_t(\rho,\sigma | y^t )$.
103103
104-
We also want to compute some predictive distributions of "sample path statistics" that might include, for example
104+
We also want to compute some predictive distributions of "sample path statistics" that might include, for example
105105
106106
- the time until the next "recession",
107107
- the minimum value of $Y$ over the next 8 periods,
@@ -115,20 +115,16 @@ To accomplish that for situations in which we are uncertain about parameter valu
115115
- for each draw $n=0,1,...,N$, simulate a "future path" of length $T_1$ with parameters $\left(\rho_n,\sigma_n\right)$ and compute our three "sample path statistics";
116116
- finally, plot the desired statistics from the $N$ samples as an empirical distribution.
117117
118-
119-
120118
## Implementation
121119
122120
First, we'll simulate a sample path from which to launch our forecasts.
123121
124122
In addition to plotting the sample path, under the assumption that the true parameter values are known,
125123
we'll plot $.9$ and $.95$ coverage intervals using conditional distribution
126-
{eq}`eq3` described above.
124+
{eq}`ar1-tp-eq3` described above.
127125
128126
We'll also plot a bunch of samples of sequences of future values and watch where they fall relative to the coverage interval.
129127
130-
131-
132128
```{code-cell} ipython3
133129
def AR1_simulate(rho, sigma, y0, T):
134130
@@ -198,30 +194,30 @@ Wecker {cite}`wecker1979predicting` proposed using simulation techniques to char
198194
199195
He called these functions "path properties" to contrast them with properties of single data points.
200196
201-
He studied two special prospective path properties of a given series $\{y_t\}$.
197+
He studied two special prospective path properties of a given series $\{y_t\}$.
202198
203-
The first was **time until the next turning point**
199+
The first was **time until the next turning point**
204200
205-
* he defined a **"turning point"** to be the date of the second of two successive declines in $y$.
201+
* he defined a **"turning point"** to be the date of the second of two successive declines in $y$.
206202
207-
To examine this statistic, let $Z$ be an indicator process
203+
To examine this statistic, let $Z$ be an indicator process
208204
209-
$$
205+
<!-- $$
210206
Z_t(Y(\omega)) := \left\{
211207
\begin{array} {c}
212208
\ 1 & \text{if } Y_t(\omega)< Y_{t-1}(\omega)< Y_{t-2}(\omega) \geq Y_{t-3}(\omega) \\
213209
0 & \text{otherwise}
214210
\end{array} \right.
215-
$$
211+
$$ -->
216212
217213
Then the random variable **time until the next turning point** is defined as the following **stopping time** with respect to $Z$:
218214
219215
$$
220216
W_t(\omega):= \inf \{ k\geq 1 \mid Z_{t+k}(\omega) = 1\}
221217
$$
222218
223-
Wecker {cite}`wecker1979predicting` also studied **the minimum value of $Y$ over the next 8 quarters**
224-
which can be defined as the random variable
219+
Wecker {cite}`wecker1979predicting` also studied **the minimum value of $Y$ over the next 8 quarters**
220+
which can be defined as the random variable
225221
226222
$$
227223
M_t(\omega) := \min \{ Y_{t+1}(\omega); Y_{t+2}(\omega); \dots; Y_{t+8}(\omega)\}
@@ -230,36 +226,34 @@ $$
230226
It is interesting to study yet another possible concept of a **turning point**.
231227
232228
Thus, let
233-
229+
<!--
234230
$$
235231
T_t(Y(\omega)) := \left\{
236232
\begin{array}{c}
237233
\ 1 & \text{if } Y_{t-2}(\omega)> Y_{t-1}(\omega) > Y_{t}(\omega) \ \text{and } \ Y_{t}(\omega) < Y_{t+1}(\omega) < Y_{t+2}(\omega) \\
238-
\ -1 & \text{if } Y_{t-2}(\omega)< Y_{t-1}(\omega) < Y_{t}(\omega) \ \text{and } \ Y_{t}(\omega) > Y_{t+1}(\omega) > Y_{t+2}(\omega) \\
234+
\ -1 & \text{if } Y_{t-2}(\omega)< Y_{t-1}(\omega) < Y_{t}(\omega) \ \text{and } \ Y_{t}(\omega) > Y_{t+1}(\omega) > Y_{t+2}(\omega) \\
239235
0 & \text{otherwise}
240236
\end{array} \right.
241-
$$
237+
$$ -->
242238
243239
Define a **positive turning point today or tomorrow** statistic as
244240
245-
$$
241+
<!-- $$
246242
P_t(\omega) := \left\{
247243
\begin{array}{c}
248244
\ 1 & \text{if } T_t(\omega)=1 \ \text{or} \ T_{t+1}(\omega)=1 \\
249245
0 & \text{otherwise}
250246
\end{array} \right.
251-
$$
247+
$$ -->
252248
253249
This is designed to express the event
254250
255-
- ``after one or two decrease(s), $Y$ will grow for two consecutive quarters''
256-
251+
- ``after one or two decrease(s), $Y$ will grow for two consecutive quarters''
257252
258253
Following {cite}`wecker1979predicting`, we can use simulations to calculate probabilities of $P_t$ and $N_t$ for each period $t$.
259254
260255
## A Wecker-Like Algorithm
261256
262-
263257
The procedure consists of the following steps:
264258
265259
* index a sample path by $\omega_i$
@@ -270,11 +264,9 @@ $$
270264
Y(\omega_i) = \left\{ Y_{t+1}(\omega_i), Y_{t+2}(\omega_i), \dots, Y_{t+N}(\omega_i)\right\}_{i=1}^I
271265
$$
272266
273-
* for each path $\omega_i$, compute the associated value of $W_t(\omega_i), W_{t+1}(\omega_i), \dots$
267+
* for each path $\omega_i$, compute the associated value of $W_t(\omega_i), W_{t+1}(\omega_i), \dots$
274268
275-
* consider the sets $
276-
\{W_t(\omega_i)\}^{T}_{i=1}, \ \{W_{t+1}(\omega_i)\}^{T}_{i=1}, \ \dots, \ \{W_{t+N}(\omega_i)\}^{T}_{i=1}
277-
$ as samples from the predictive distributions $f(W_{t+1} \mid \mathcal y_t, \dots)$, $f(W_{t+2} \mid y_t, y_{t-1}, \dots)$, $\dots$, $f(W_{t+N} \mid y_t, y_{t-1}, \dots)$.
269+
* consider the sets $\{W_t(\omega_i)\}^{T}_{i=1}, \ \{W_{t+1}(\omega_i)\}^{T}_{i=1}, \ \dots, \ \{W_{t+N}(\omega_i)\}^{T}_{i=1}$ as samples from the predictive distributions $f(W_{t+1} \mid \mathcal y_t, \dots)$, $f(W_{t+2} \mid y_t, y_{t-1}, \dots)$, $\dots$, $f(W_{t+N} \mid y_t, y_{t-1}, \dots)$.
278270
279271
280272
## Using Simulations to Approximate a Posterior Distribution
@@ -283,7 +275,6 @@ The next code cells use `pymc` to compute the time $t$ posterior distribution of
283275
284276
Note that in defining the likelihood function, we choose to condition on the initial value $y_0$.
285277
286-
287278
```{code-cell} ipython3
288279
def draw_from_posterior(sample):
289280
"""
@@ -328,7 +319,6 @@ The graphs on the left portray posterior marginal distributions.
328319
329320
## Calculating Sample Path Statistics
330321
331-
332322
Our next step is to prepare Python codeto compute our sample path statistics.
333323
334324
```{code-cell} ipython3
@@ -455,9 +445,9 @@ plt.show()
455445
## Extended Wecker Method
456446
457447
Now we apply we apply our "extended" Wecker method based on predictive densities of $y$ defined by
458-
{eq}`eq4` that acknowledge posterior uncertainty in the parameters $\rho, \sigma$.
448+
{eq}`ar1-tp-eq4` that acknowledge posterior uncertainty in the parameters $\rho, \sigma$.
459449
460-
To approximate the intergration on the right side of {eq}`eq4`, we repeately draw parameters from the joint posterior distribution each time we simulate a sequence of future values from model {eq}`eq1`.
450+
To approximate the intergration on the right side of {eq}`ar1-tp-eq4`, we repeately draw parameters from the joint posterior distribution each time we simulate a sequence of future values from model {eq}`ar1-tp-eq1`.
461451
462452
```{code-cell} ipython3
463453
def plot_extended_Wecker(post_samples, initial_path, N, ax):
@@ -525,4 +515,3 @@ plot_extended_Wecker(post_samples, initial_path, 1000, ax)
525515
plt.legend()
526516
plt.show()
527517
```
528-

lectures/bayes_nonconj.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ SVI_num_steps = 5000
888888
true_theta = 0.8
889889
```
890890
891-
#### Beta Prior and Posteriors
891+
### Beta Prior and Posteriors:
892892
893893
Let's compare outcomes when we use a Beta prior.
894894
@@ -944,7 +944,7 @@ Here the MCMC approximation looks good.
944944
945945
But the VI approximation doesn't look so good.
946946
947-
* even though we use the beta distribution as our guide, the VI approximated posterior distributions do not closely resemble the posteriors that we had just computed analytically.
947+
* even though we use the beta distribution as our guide, the VI approximated posterior distributions do not closely resemble the posteriors that we had just computed analytically.
948948
949949
(Here, our initial parameter for Beta guide is (0.5, 0.5).)
950950
@@ -960,8 +960,6 @@ BayesianInferencePlot(true_theta, num_list, BETA_numpyro).SVI_plot(guide_dist='b
960960
```
961961
962962
963-
964-
965963
## Non-conjugate Prior Distributions
966964
967965
Having assured ourselves that our MCMC and VI methods can work well when we have conjugate prior and so can also compute analytically, we

0 commit comments

Comments
 (0)