@@ -13,6 +13,12 @@ kernelspec:
13131414# Forecasting an AR(1) process
151516+```{code-cell} ipython3
17+:tags: [hide-output]
18+19+!pip install arviz pymc
20+```
21+1622This lecture describes methods for forecasting statistics that are functions of future values of a univariate autogressive process.
17231824The 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:
25312632- prospective values $y_{t+j}$ of a random process $\{y_t\}$ that is governed by the AR(1) process
273328-- 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$.
29353036**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')
5460logger.setLevel(logging.CRITICAL)
5561```
566257-## A Univariate First-Order Autoregressive Process
58-63+## A Univariate First-Order Autoregressive Process
59646065Consider the univariate AR(1) model:
61666267$$
6368y_{t+1} = \rho y_t + \sigma \epsilon_{t+1}, \quad t \geq 0
64-$$ (eq1)
69+$$ (ar1-tp-eq1)
65706671where 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$.
687369-The initial condition $y_{0}$ is a known number.
74+The initial condition $y_{0}$ is a known number.
707571-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
72777378$$
7479f(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)
798182+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
80838184$$
8285f(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)
848785-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
8789by conditioning on them.
88908991We also want to compute a predictive distribution that does not condition on $\rho,\sigma$ but instead takes account of our uncertainty about them.
909291-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)$
9294that conditions on an observed history $y^t = \{y_s\}_{s=0}^t$:
93959496$$
9597f(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)
9999100-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.
101101102-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 )$.
103103104-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
105105106106- 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.
117117118-119-120118## Implementation
121119122120First, we'll simulate a sample path from which to launch our forecasts.
123121124122In addition to plotting the sample path, under the assumption that the true parameter values are known,
125123we'll plot $.9$ and $.95$ coverage intervals using conditional distribution
126-{eq}`eq3` described above.
124+{eq}`ar1-tp-eq3` described above.
127125128126We'll also plot a bunch of samples of sequences of future values and watch where they fall relative to the coverage interval.
129127130-131-132128```{code-cell} ipython3
133129def AR1_simulate(rho, sigma, y0, T):
134130@@ -198,30 +194,30 @@ Wecker {cite}`wecker1979predicting` proposed using simulation techniques to char
198194199195He called these functions "path properties" to contrast them with properties of single data points.
200196201-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\}$.
202198203-The first was **time until the next turning point**
199+The first was **time until the next turning point**
204200205- * 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$.
206202207-To examine this statistic, let $Z$ be an indicator process
203+To examine this statistic, let $Z$ be an indicator process
208204209-$$
205+<!-- $$
210206Z_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) \\
2132090 & \text{otherwise}
214210\end{array} \right.
215-$$
211+$$ -->
216212217213Then the random variable **time until the next turning point** is defined as the following **stopping time** with respect to $Z$:
218214219215$$
220216W_t(\omega):= \inf \{ k\geq 1 \mid Z_{t+k}(\omega) = 1\}
221217$$
222218223-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
225221226222$$
227223M_t(\omega) := \min \{ Y_{t+1}(\omega); Y_{t+2}(\omega); \dots; Y_{t+8}(\omega)\}
@@ -230,36 +226,34 @@ $$
230226It is interesting to study yet another possible concept of a **turning point**.
231227232228Thus, let
233-229+<!--
234230$$
235231T_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) \\
2392350 & \text{otherwise}
240236\end{array} \right.
241-$$
237+$$ -->
242238243239Define a **positive turning point today or tomorrow** statistic as
244240245-$$
241+<!-- $$
246242P_t(\omega) := \left\{
247243\begin{array}{c}
248244\ 1 & \text{if } T_t(\omega)=1 \ \text{or} \ T_{t+1}(\omega)=1 \\
2492450 & \text{otherwise}
250246\end{array} \right.
251-$$
247+$$ -->
252248253249This is designed to express the event
254250255- - ``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''
257252258253Following {cite}`wecker1979predicting`, we can use simulations to calculate probabilities of $P_t$ and $N_t$ for each period $t$.
259254260255## A Wecker-Like Algorithm
261256262-263257The procedure consists of the following steps:
264258265259* index a sample path by $\omega_i$
@@ -270,11 +264,9 @@ $$
270264Y(\omega_i) = \left\{ Y_{t+1}(\omega_i), Y_{t+2}(\omega_i), \dots, Y_{t+N}(\omega_i)\right\}_{i=1}^I
271265$$
272266273-* 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$
274268275-* 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)$.
278270279271280272## 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
283275284276Note that in defining the likelihood function, we choose to condition on the initial value $y_0$.
285277286-287278```{code-cell} ipython3
288279def draw_from_posterior(sample):
289280 """
@@ -328,7 +319,6 @@ The graphs on the left portray posterior marginal distributions.
328319329320## Calculating Sample Path Statistics
330321331-332322Our next step is to prepare Python codeto compute our sample path statistics.
333323334324```{code-cell} ipython3
@@ -455,9 +445,9 @@ plt.show()
455445## Extended Wecker Method
456446457447Now 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$.
459449460-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`.
461451462452```{code-cell} ipython3
463453def plot_extended_Wecker(post_samples, initial_path, N, ax):
@@ -525,4 +515,3 @@ plot_extended_Wecker(post_samples, initial_path, 1000, ax)
525515plt.legend()
526516plt.show()
527517```
528-