51. Posterior Distributions for AR(1) Parameters#

We’ll begin with some Python imports.

!pip install arviz pymc numpyro jax
Hide code cell output
Requirement already satisfied: arviz in /opt/conda/envs/quantecon/lib/python3.11/site-packages (0.16.1)
Collecting pymc
  Obtaining dependency information for pymc from https://files.pythonhosted.org/packages/d1/b6/ac103c3c5b178a98e1226137fef2bbceb1c59b6cbc45b35635fc157edb40/pymc-5.11.0-py3-none-any.whl.metadata
  Downloading pymc-5.11.0-py3-none-any.whl.metadata (10 kB)
Collecting numpyro
  Obtaining dependency information for numpyro from https://files.pythonhosted.org/packages/fe/09/90f8c640a0bc87c150e2a84ab24534f896b861efa8101a54892a8a4dbd57/numpyro-0.14.0-py3-none-any.whl.metadata
  Downloading numpyro-0.14.0-py3-none-any.whl.metadata (36 kB)
Requirement already satisfied: jax in /opt/conda/envs/quantecon/lib/python3.11/site-packages (0.4.23)
Requirement already satisfied: setuptools>=60.0.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (68.0.0)
Requirement already satisfied: matplotlib>=3.2 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (3.7.2)
Requirement already satisfied: numpy<2.0,>=1.21.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (1.24.3)
Requirement already satisfied: scipy>=1.8.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (1.11.1)
Requirement already satisfied: packaging in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (23.1)
Requirement already satisfied: pandas>=1.3.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (2.0.3)
Requirement already satisfied: xarray>=0.21.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (2023.6.0)
Requirement already satisfied: h5netcdf>=1.0.2 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (1.3.0)
Requirement already satisfied: typing-extensions>=4.1.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (4.7.1)
Requirement already satisfied: xarray-einstats>=0.3 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from arviz) (0.6.0)
Collecting cachetools>=4.2.1 (from pymc)
  Obtaining dependency information for cachetools>=4.2.1 from https://files.pythonhosted.org/packages/fb/2b/a64c2d25a37aeb921fddb929111413049fc5f8b9a4c1aefaffaafe768d54/cachetools-5.3.3-py3-none-any.whl.metadata
  Downloading cachetools-5.3.3-py3-none-any.whl.metadata (5.3 kB)
Requirement already satisfied: cloudpickle in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from pymc) (2.2.1)
Collecting fastprogress>=0.2.0 (from pymc)
  Obtaining dependency information for fastprogress>=0.2.0 from https://files.pythonhosted.org/packages/a7/8f/213223fdee199c55db81e2d0c669f30e8285c5be2526c4ed924de39247da/fastprogress-1.0.3-py3-none-any.whl.metadata
  Downloading fastprogress-1.0.3-py3-none-any.whl.metadata (5.6 kB)
Collecting pytensor<2.19,>=2.18.1 (from pymc)
  Obtaining dependency information for pytensor<2.19,>=2.18.1 from https://files.pythonhosted.org/packages/58/d4/4431a529ac09aa900df44cf801eaf043f3724e03299f173f44edbff77490/pytensor-2.18.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata
  Downloading pytensor-2.18.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.7 kB)
Requirement already satisfied: jaxlib>=0.4.14 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from numpyro) (0.4.23+cuda12.cudnn89)
Requirement already satisfied: multipledispatch in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from numpyro) (0.6.0)
Requirement already satisfied: tqdm in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from numpyro) (4.65.0)
Requirement already satisfied: ml-dtypes>=0.2.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from jax) (0.3.1)
Requirement already satisfied: opt-einsum in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from jax) (3.3.0)
Requirement already satisfied: h5py in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from h5netcdf>=1.0.2->arviz) (3.9.0)
Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (1.0.5)
Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (4.25.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (1.4.4)
Requirement already satisfied: pillow>=6.2.0 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (9.4.0)
Requirement already satisfied: pyparsing<3.1,>=2.3.1 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (3.0.9)
Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from matplotlib>=3.2->arviz) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from pandas>=1.3.0->arviz) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from pandas>=1.3.0->arviz) (2023.3)
Requirement already satisfied: filelock in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from pytensor<2.19,>=2.18.1->pymc) (3.9.0)
Collecting etuples (from pytensor<2.19,>=2.18.1->pymc)
  Downloading etuples-0.3.9.tar.gz (30 kB)
  Preparing metadata (setup.py) ... ?25l-
 done
?25hCollecting logical-unification (from pytensor<2.19,>=2.18.1->pymc)
  Downloading logical-unification-0.4.6.tar.gz (31 kB)
  Preparing metadata (setup.py) ... ?25l-
 done
?25hCollecting miniKanren (from pytensor<2.19,>=2.18.1->pymc)
  Downloading miniKanren-1.0.3.tar.gz (41 kB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/41.3 kB ? eta -:--:--
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.3/41.3 kB 7.7 MB/s eta 0:00:00
?25h
  Preparing metadata (setup.py) ... ?25l-
 done
?25hCollecting cons (from pytensor<2.19,>=2.18.1->pymc)
  Downloading cons-0.4.6.tar.gz (26 kB)
  Preparing metadata (setup.py) ... ?25l-
 done
?25hRequirement already satisfied: six in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from multipledispatch->numpyro) (1.16.0)
Requirement already satisfied: toolz in /opt/conda/envs/quantecon/lib/python3.11/site-packages (from logical-unification->pytensor<2.19,>=2.18.1->pymc) (0.12.0)
Downloading pymc-5.11.0-py3-none-any.whl (473 kB)
?25l   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/473.3 kB ? eta -:--:--
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 473.3/473.3 kB 18.3 MB/s eta 0:00:00
?25hDownloading numpyro-0.14.0-py3-none-any.whl (330 kB)
?25l   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/330.2 kB ? eta -:--:--
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 330.2/330.2 kB 49.3 MB/s eta 0:00:00
?25h
Downloading cachetools-5.3.3-py3-none-any.whl (9.3 kB)
Downloading fastprogress-1.0.3-py3-none-any.whl (12 kB)
Downloading pytensor-2.18.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB)
?25l   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/2.0 MB ? eta -:--:--
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 63.1 MB/s eta 0:00:00
?25h
Building wheels for collected packages: cons, logical-unification, etuples, miniKanren
  Building wheel for cons (setup.py) ... ?25l-
 \
 done
?25h  Created wheel for cons: filename=cons-0.4.6-py3-none-any.whl size=9100 sha256=648c45d9e63c72d45ad80e9992ed176e9a61838c20c113912c9394df27d72de2
  Stored in directory: /github/home/.cache/pip/wheels/95/8f/45/fe0a5b5e232401da571d514eb545833fbe220993ac8336c94e
  Building wheel for logical-unification (setup.py) ... ?25l-
 \
 done
?25h  Created wheel for logical-unification: filename=logical_unification-0.4.6-py3-none-any.whl size=13911 sha256=bae877f316de019efd0659dd2dd5cbc1f7f553c3e4753f8288370d1bad0dcb96
  Stored in directory: /github/home/.cache/pip/wheels/b8/34/a9/c11a21ef1f1b6d2e5ae518dd5d28c0bd2b131c5d6e5d4417c3
  Building wheel for etuples (setup.py) ... ?25l-
 \
 done
?25h  Created wheel for etuples: filename=etuples-0.3.9-py3-none-any.whl size=12617 sha256=0ac0301ec035514809f1012ec40a96c10d4af76c6a8ff0599ca5a0e5804cee34
  Stored in directory: /github/home/.cache/pip/wheels/11/5a/ad/ebef6395e95b1dfd742afb44db6d92bd5e8921a572b9ff8618
  Building wheel for miniKanren (setup.py) ... ?25l-
 \
 done
?25h  Created wheel for miniKanren: filename=miniKanren-1.0.3-py3-none-any.whl size=23910 sha256=03f255d30c2a7bff201954171c80ce94d63f9abe0acc4b418efa268aed96ec04
  Stored in directory: /github/home/.cache/pip/wheels/8d/86/58/7da4a186a7bb721885e4fb811a54b5a0065b0c624a5deb1259
Successfully built cons logical-unification etuples miniKanren
Installing collected packages: fastprogress, cachetools, logical-unification, numpyro, cons, etuples, miniKanren, pytensor, pymc
Successfully installed cachetools-5.3.3 cons-0.4.6 etuples-0.3.9 fastprogress-1.0.3 logical-unification-0.4.6 miniKanren-1.0.3 numpyro-0.14.0 pymc-5.11.0 pytensor-2.18.6
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

import arviz as az
import pymc as pmc
import numpyro
from numpyro import distributions as dist

import numpy as np
import jax.numpy as jnp
from jax import random
import matplotlib.pyplot as plt

import logging
logging.basicConfig()
logger = logging.getLogger('pymc')
logger.setLevel(logging.CRITICAL)

This lecture uses Bayesian methods offered by pymc and numpyro to make statistical inferences about two parameters of a univariate first-order autoregression.

The model is a good laboratory for illustrating consequences of alternative ways of modeling the distribution of the initial \(y_0\):

  • As a fixed number

  • As a random variable drawn from the stationary distribution of the \(\{y_t\}\) stochastic process

The first component of the statistical model is

(51.1)#\[ y_{t+1} = \rho y_t + \sigma_x \epsilon_{t+1}, \quad t \geq 0 \]

where the scalars \(\rho\) and \(\sigma_x\) satisfy \(|\rho| < 1\) and \(\sigma_x > 0\); \(\{\epsilon_{t+1}\}\) is a sequence of i.i.d. normal random variables with mean \(0\) and variance \(1\).

The second component of the statistical model is

(51.2)#\[ y_0 \sim {\cal N}(\mu_0, \sigma_0^2) \]

Consider a sample \(\{y_t\}_{t=0}^T\) governed by this statistical model.

The model implies that the likelihood function of \(\{y_t\}_{t=0}^T\) can be factored:

\[ f(y_T, y_{T-1}, \ldots, y_0) = f(y_T| y_{T-1}) f(y_{T-1}| y_{T-2}) \cdots f(y_1 | y_0 ) f(y_0) \]

where we use \(f\) to denote a generic probability density.

The statistical model (51.1)-(51.2) implies

\[\begin{split} \begin{aligned} f(y_t | y_{t-1}) & \sim {\mathcal N}(\rho y_{t-1}, \sigma_x^2) \\ f(y_0) & \sim {\mathcal N}(\mu_0, \sigma_0^2) \end{aligned} \end{split}\]

We want to study how inferences about the unknown parameters \((\rho, \sigma_x)\) depend on what is assumed about the parameters \(\mu_0, \sigma_0\) of the distribution of \(y_0\).

Below, we study two widely used alternative assumptions:

  • \((\mu_0,\sigma_0) = (y_0, 0)\) which means that \(y_0\) is drawn from the distribution \({\mathcal N}(y_0, 0)\); in effect, we are conditioning on an observed initial value.

  • \(\mu_0,\sigma_0\) are functions of \(\rho, \sigma_x\) because \(y_0\) is drawn from the stationary distribution implied by \(\rho, \sigma_x\).

Note: We do not treat a third possible case in which \(\mu_0,\sigma_0\) are free parameters to be estimated.

Unknown parameters are \(\rho, \sigma_x\).

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\).

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.

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.

Thus, we explore consequences of making these alternative assumptions about the distribution of \(y_0\):

  • 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\).

  • A second procedure assumes that \(y_0\) is drawn from the stationary distribution of a process described by (51.1) so that \(y_0 \sim {\cal N} \left(0, {\sigma_x^2\over (1-\rho)^2} \right) \)

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.

Basically, when \(y_0\) happens to be in a tail of the stationary distribution and we don’t condition on \(y_0\), the likelihood function for \(\{y_t\}_{t=0}^T\) adjusts the posterior distribution of the parameter pair \(\rho, \sigma_x \) to make the observed value of \(y_0\) more likely than it really is under the stationary distribution, thereby adversely twisting the posterior in short samples.

An example below shows how not conditioning on \(y_0\) adversely shifts the posterior probability distribution of \(\rho\) toward larger values.

We begin by solving a direct problem that simulates an AR(1) process.

How we select the initial value \(y_0\) matters.

  • If we think \(y_0\) is drawn from the stationary distribution \({\mathcal N}(0, \frac{\sigma_x^{2}}{1-\rho^2})\), then it is a good idea to use this distribution as \(f(y_0)\). Why? Because \(y_0\) contains information about \(\rho, \sigma_x\).

  • If we suspect that \(y_0\) is far in the tails of the stationary distribution – so that variation in early observations in the sample have a significant transient component – it is better to condition on \(y_0\) by setting \(f(y_0) = 1\).

To illustrate the issue, we’ll begin by choosing an initial \(y_0\) that is far out in a tail of the stationary distribution.

def ar1_simulate(rho, sigma, y0, T):

    # Allocate space and draw epsilons
    y = np.empty(T)
    eps = np.random.normal(0.,sigma,T)

    # Initial condition and step forward
    y[0] = y0
    for t in range(1, T):
        y[t] = rho*y[t-1] + eps[t]

    return y

sigma =  1.
rho = 0.5
T = 50

np.random.seed(145353452)
y = ar1_simulate(rho, sigma, 10, T)
plt.plot(y)
plt.tight_layout()
_images/a83cb95a851dbe184b9041f6aee4f5903f8cd2a365d397c6e4eb4190520a19d2.png

Now we shall use Bayes’ law to construct a posterior distribution, conditioning on the initial value of \(y_0\).

(Later we’ll assume that \(y_0\) is drawn from the stationary distribution, but not now.)

First we’ll use pymc4.

51.1. PyMC Implementation#

For a normal distribution in pymc, \(var = 1/\tau = \sigma^{2}\).

AR1_model = pmc.Model()

with AR1_model:

    # Start with priors
    rho = pmc.Uniform('rho', lower=-1., upper=1.) # Assume stable rho
    sigma = pmc.HalfNormal('sigma', sigma = np.sqrt(10))

    # Expected value of y at the next period (rho * y)
    yhat = rho * y[:-1]

    # Likelihood of the actual realization
    y_like = pmc.Normal('y_obs', mu=yhat, sigma=sigma, observed=y[1:])

pmc.sample by default uses the NUTS samplers to generate samples as shown in the below cell:

with AR1_model:
    trace = pmc.sample(50000, tune=10000, return_inferencedata=True)
100.00% [240000/240000 01:12<00:00 Sampling 4 chains, 0 divergences]
with AR1_model:
    az.plot_trace(trace, figsize=(17,6))
_images/64347bfdf26ad87ee17f650b6e077861d71f417c1228e4fdf7c859501f9fb079.png

Evidently, the posteriors aren’t centered on the true values of \(.5, 1\) that we used to generate the data.

This is a symptom of the classic Hurwicz bias for first order autoregressive processes (see Leonid Hurwicz [Hurwicz, 1950].)

The Hurwicz bias is worse the smaller is the sample (see [Orcutt and Winokur, 1969]).

Be that as it may, here is more information about the posterior.

with AR1_model:
    summary = az.summary(trace, round_to=4)

summary
mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
rho 0.5365 0.0710 0.4053 0.6732 0.0002 0.0001 172051.8003 128110.7684 1.0
sigma 1.0105 0.1062 0.8188 1.2098 0.0003 0.0002 163313.4607 134923.4555 1.0

Now we shall compute a posterior distribution after seeing the same data but instead assuming that \(y_0\) is drawn from the stationary distribution.

This means that

\[ y_0 \sim N \left(0, \frac{\sigma_x^{2}}{1 - \rho^{2}} \right) \]

We alter the code as follows:

AR1_model_y0 = pmc.Model()

with AR1_model_y0:

    # Start with priors
    rho = pmc.Uniform('rho', lower=-1., upper=1.) # Assume stable rho
    sigma = pmc.HalfNormal('sigma', sigma=np.sqrt(10))

    # Standard deviation of ergodic y
    y_sd = sigma / np.sqrt(1 - rho**2)

    # yhat
    yhat = rho * y[:-1]
    y_data = pmc.Normal('y_obs', mu=yhat, sigma=sigma, observed=y[1:])
    y0_data = pmc.Normal('y0_obs', mu=0., sigma=y_sd, observed=y[0])
with AR1_model_y0:
    trace_y0 = pmc.sample(50000, tune=10000, return_inferencedata=True)

# Grey vertical lines are the cases of divergence
100.00% [240000/240000 01:14<00:00 Sampling 4 chains, 61 divergences]
with AR1_model_y0:
    az.plot_trace(trace_y0, figsize=(17,6))
_images/5f8c01cddc624decdf4769405c3b83a1a0af02f05764718796a7800007976648.png
with AR1_model:
    summary_y0 = az.summary(trace_y0, round_to=4)

summary_y0
mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
rho 0.8757 0.0809 0.7322 0.9983 0.0002 0.0002 107258.3765 82441.7673 1.0001
sigma 1.4046 0.1478 1.1367 1.6822 0.0004 0.0003 112210.8552 98209.1627 1.0001

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.

Think about why this happens.

Hint

It is connected to how Bayes Law (conditional probability) solves an inverse problem by putting high probability on parameter values that make observations more likely.

We’ll return to this issue after we use numpyro to compute posteriors under our two alternative assumptions about the distribution of \(y_0\).

We’ll now repeat the calculations using numpyro.

51.2. Numpyro Implementation#

def plot_posterior(sample):
    """
    Plot trace and histogram
    """
    # To np array
    rhos = sample['rho']
    sigmas = sample['sigma']
    rhos, sigmas, = np.array(rhos), np.array(sigmas)

    fig, axs = plt.subplots(2, 2, figsize=(17, 6))
    # Plot trace
    axs[0, 0].plot(rhos)   # rho
    axs[1, 0].plot(sigmas) # sigma

    # Plot posterior
    axs[0, 1].hist(rhos, bins=50, density=True, alpha=0.7)
    axs[0, 1].set_xlim([0, 1])
    axs[1, 1].hist(sigmas, bins=50, density=True, alpha=0.7)

    axs[0, 0].set_title("rho")
    axs[0, 1].set_title("rho")
    axs[1, 0].set_title("sigma")
    axs[1, 1].set_title("sigma")
    plt.show()
def AR1_model(data):
    # set prior
    rho = numpyro.sample('rho', dist.Uniform(low=-1., high=1.))
    sigma = numpyro.sample('sigma', dist.HalfNormal(scale=np.sqrt(10)))

    # Expected value of y at the next period (rho * y)
    yhat = rho * data[:-1]

    # Likelihood of the actual realization.
    y_data = numpyro.sample('y_obs', dist.Normal(loc=yhat, scale=sigma), obs=data[1:])
# Make jnp array
y = jnp.array(y)

# Set NUTS kernal
NUTS_kernel = numpyro.infer.NUTS(AR1_model)

# Run MCMC
mcmc = numpyro.infer.MCMC(NUTS_kernel, num_samples=50000, num_warmup=10000, progress_bar=False)
mcmc.run(rng_key=random.PRNGKey(1), data=y)
plot_posterior(mcmc.get_samples())
_images/99287afe127cf5b5d34e033e8ad239079d78bafb8a91c4c11feba7b366b94cc0.png
mcmc.print_summary()
                mean       std    median      5.0%     95.0%     n_eff     r_hat
       rho      0.54      0.07      0.54      0.42      0.66  34735.88      1.00
     sigma      1.01      0.11      1.00      0.83      1.18  33245.42      1.00

Number of divergences: 0

Next, we again compute the posterior under the assumption that \(y_0\) is drawn from the stationary distribution, so that

\[ y_0 \sim N \left(0, \frac{\sigma_x^{2}}{1 - \rho^{2}} \right) \]

Here’s the new code to achieve this.

def AR1_model_y0(data):
    # Set prior
    rho = numpyro.sample('rho', dist.Uniform(low=-1., high=1.))
    sigma = numpyro.sample('sigma', dist.HalfNormal(scale=np.sqrt(10)))

    # Standard deviation of ergodic y
    y_sd = sigma / jnp.sqrt(1 - rho**2)

    # Expected value of y at the next period (rho * y)
    yhat = rho * data[:-1]

    # Likelihood of the actual realization.
    y_data = numpyro.sample('y_obs', dist.Normal(loc=yhat, scale=sigma), obs=data[1:])
    y0_data = numpyro.sample('y0_obs', dist.Normal(loc=0., scale=y_sd), obs=data[0])
# Make jnp array
y = jnp.array(y)

# Set NUTS kernal
NUTS_kernel = numpyro.infer.NUTS(AR1_model_y0)

# Run MCMC
mcmc2 = numpyro.infer.MCMC(NUTS_kernel, num_samples=50000, num_warmup=10000, progress_bar=False)
mcmc2.run(rng_key=random.PRNGKey(1), data=y)
plot_posterior(mcmc2.get_samples())
_images/8267e0e8617b777b1666e91dce1d0759daca82ee2c4960348275353f0015b7b7.png
mcmc2.print_summary()
                mean       std    median      5.0%     95.0%     n_eff     r_hat
       rho      0.88      0.08      0.89      0.76      1.00  29063.44      1.00
     sigma      1.40      0.15      1.39      1.17      1.64  25674.09      1.00

Number of divergences: 0

Look what happened to the posterior!

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) is telling numpyro to explain what it interprets as “explosive” observations early in the sample.

Bayes’ Law is able to generate a plausible likelihood for the first observation by driving \(\rho \rightarrow 1\) and \(\sigma \uparrow\) in order to raise the variance of the stationary distribution.

Our example illustrates the importance of what you assume about the distribution of initial conditions.