← Bayesian Methods

Bayesian AR(1) in Stan: why the mean gets lost when phi nears 1, and latent AR for counts

How the AR(1) coefficient controls memory and stationarity, why the process mean becomes almost impossible to estimate as phi approaches 1, and how to put a latent AR(1) process under Poisson counts in Stan.

First explored May 2020·4 min read

University lab From my coursework in the MSc in Statistics and Machine Learning at Linköping University, rewritten as a note to review later.

bayesian-inferencetime-seriesautoregressive-modelstanpoisson-regression

The last Bayesian Learning lab (pair work) was our first real use of Stan. We simulated AR(1) series, tried to recover their parameters, and then used an AR(1) process as a hidden layer under count data. The result that stuck with me: with the same amount of data, one parameter was estimated to within ±0.3\pm 0.3 for one series and was basically unknown for the other. The reason is worth understanding.

The AR(1) process

xt=μ+ϕ (xt−1−μ)+εt,εt∼N(0,σ2),x_t = \mu + \phi\,(x_{t-1} - \mu) + \varepsilon_t, \qquad \varepsilon_t \sim N(0, \sigma^2),

where μ\mu is the level the series is pulled towards, ϕ\phi controls how much of yesterday’s deviation from μ\mu survives to today, and σ2\sigma^2 is the variance of each new shock.

  • ϕ=0\phi = 0: no memory, just noise around μ\mu.
  • ϕ\phi close to 1: long memory. The series wanders away from μ\mu and comes back slowly.
  • ϕ\phi close to −1-1: the deviation flips sign each step, so the series zig-zags.
  • ∣ϕ∣<1|\phi| < 1 is needed for the process to be stationary. Then its long-run mean is μ\mu and its long-run variance is σ2/(1−ϕ2)\sigma^2/(1-\phi^2), which blows up as ∣ϕ∣→1|\phi| \to 1. At ϕ=1\phi = 1 it’s a random walk and μ\mu drops out of the equation entirely.

Why μ\mu gets lost

We simulated two series of length 200, one with ϕ=0.3\phi = 0.3 and one with ϕ=0.95\phi = 0.95, and fit both in Stan. For the first, the posterior for μ\mu was tight: mean 9.94 with a 95% interval of about (9.65,10.24)(9.65, 10.24) around the true 10, and ϕ\phi came out at 0.28. For the second, ϕ\phi was estimated fine (about 0.98), but the 95% interval for μ\mu ran from about −80-80 to 8080.

Our report noted that μ\mu was “harder to estimate” without saying why. Here’s why. Information about the mean comes from seeing the series cross back and forth over it many times. With strong persistence, 200 correlated points behave like very few independent ones. The long-run standard deviation of the sample mean is roughly

sd(xˉ)≈σ(1−ϕ)T,\text{sd}(\bar x) \approx \frac{\sigma}{(1-\phi)\sqrt{T}},

where TT is the series length. The factor 1/(1−ϕ)1/(1-\phi) is about 1.4 for ϕ=0.3\phi = 0.3 but 20 for ϕ=0.95\phi = 0.95. A quick simulation confirms it:

sim_ar1 <- function(T, mu, phi, sigma) {
  x <- numeric(T); x[1] <- mu
  for (t in 2:T) x[t] <- mu + phi * (x[t - 1] - mu) + rnorm(1, 0, sigma)
  x
}
set.seed(7); T <- 200; sigma <- sqrt(2)
for (phi in c(0.3, 0.95)) {
  means <- replicate(2000, mean(sim_ar1(T, mu = 10, phi = phi, sigma = sigma)))
  cat(sprintf("phi = %.2f  sd of sample mean: simulated %.2f, formula %.2f\n",
              phi, sd(means), sigma / ((1 - phi) * sqrt(T))))
}
# phi = 0.30  sd of sample mean: simulated 0.14, formula 0.14
# phi = 0.95  sd of sample mean: simulated 1.87, formula 2.00

That explains a standard deviation of about 2 for μ\mu. An interval of ±80\pm 80 needs one more ingredient: the model behind those numbers didn’t constrain ϕ\phi to (−1,1)(-1, 1), and its posterior for ϕ\phi reached past 1 (upper limit 1.007). Near and beyond ϕ=1\phi = 1 the term (1−ϕ)μ(1-\phi)\mu that ties the series to its mean vanishes, so the likelihood becomes flat in μ\mu and the posterior of μ\mu is left to the prior, which was vague. Stan also reported divergent transitions in this region, its way of saying the posterior geometry is awkward. Declaring real<lower=-1, upper=1> phi; (as in my own Stan file) keeps the sampler out of that region.

The lesson I took: a wide posterior isn’t a sampler failure. It’s the data honestly saying they contain little information about that parameter.

A latent AR(1) under counts

The second part used a series of infection counts. Counts need a Poisson model, but the rate clearly drifts over time. A neat way to model that is to let the log-rate follow an AR(1):

ct∣xt∼Poisson ⁣(ext),xt=μ+ϕ(xt−1−μ)+εt.c_t \mid x_t \sim \text{Poisson}\!\left(e^{x_t}\right), \qquad x_t = \mu + \phi(x_{t-1} - \mu) + \varepsilon_t.

Here ctc_t is the observed count and xtx_t the hidden log-intensity. This is a simple state-space model: a hidden process with its own dynamics, seen through a noisy observation model. In Stan the hidden xtx_t are just more parameters:

data { int<lower=0> N; array[N] int<lower=0> c; }
parameters {
  real mu;
  real<lower=-1, upper=1> phi;
  real<lower=0> sigma;
  vector[N] x;
}
model {
  mu ~ normal(0, 10);
  sigma ~ normal(0, 1);
  x[1] ~ normal(mu, sigma / sqrt(1 - phi^2));          // stationary start
  x[2:N] ~ normal(mu + phi * (x[1:(N - 1)] - mu), sigma);
  c ~ poisson_log(x);                                   // rate = exp(x)
}

Two details I’d fix compared with our lab files. First, we left x1x_1 without any distribution, which gives it an improper flat prior; drawing it from the stationary distribution is cleaner. Second, poisson_log(x) is the numerically stable way to write poisson(exp(x)). Plotting the posterior mean of exte^{x_t} with a 95% band over the data shows a smooth intensity that follows the counts without chasing every spike. How smooth depends on the prior for σ\sigma: a tight prior on small values forces a smoother curve.

Key takeaways

  • In AR(1), ϕ\phi is memory; ∣ϕ∣<1|\phi| < 1 means stationary, with variance σ2/(1−ϕ2)\sigma^2/(1-\phi^2).
  • As ϕ→1\phi \to 1, the mean μ\mu is weakly identified: sd(xˉ)≈σ/((1−ϕ)T)\text{sd}(\bar x) \approx \sigma / ((1-\phi)\sqrt T).
  • Constrain ϕ\phi to (−1,1)(-1, 1) if you want a stationary model; otherwise μ\mu can become completely unidentified.
  • A latent AR(1) on the log scale is an easy way to model counts with a drifting rate.
  • Give the first latent state a proper (stationary) distribution.

Resources