Skip to content

Commit 2322308

Browse files
author
Bob Carpenter
committed
added section to manual and example models/sim for ARMA models
1 parent c9a8c91 commit 2322308

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

misc/moving-avg/arma11-sim.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mu <- -1.25;
2+
sigma <- 0.75;
3+
theta <- 0.5;
4+
phi <- 0.2;
5+
6+
T <- 1000;
7+
8+
err <- rnorm(T,0,sigma);
9+
nu <- rep(0,T);
10+
y <- rep(0,T);
11+
y[1] <- err[1] + mu + phi * mu;
12+
for (t in 2:T)
13+
y[t] <- err[t] + (mu + phi * y[t-1] + theta * err[t-1]);
14+
15+
library('rstan')
16+
# fit <- stan('arma11.stan', data=list(T=T, y=y), iter=2000, chains=4);

misc/moving-avg/arma11.stan

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
data {
2+
int<lower=1> T; // number of observations
3+
real y[T]; // observed outputs
4+
}
5+
parameters {
6+
real mu; // mean term
7+
real phi; // autoregression coeff
8+
real theta; // moving avg coeff
9+
real<lower=0> sigma; // noise scale
10+
}
11+
model {
12+
vector[T] nu; // prediction for time t
13+
vector[T] err; // error for time t
14+
nu[1] <- mu + phi * mu; // assume err[0] == 0
15+
err[1] <- y[1] - nu[1];
16+
for (t in 2:T) {
17+
nu[t] <- mu + phi * y[t-1] + theta * err[t-1];
18+
err[t] <- y[t] - nu[t];
19+
}
20+
21+
// priors
22+
mu ~ normal(0,10);
23+
phi ~ normal(0,2);
24+
theta ~ normal(0,2);
25+
sigma ~ cauchy(0,5);
26+
27+
// likelihood
28+
err ~ normal(0,sigma);
29+
}
30+
31+
// alternative encoding:
32+
//
33+
// model {
34+
// err <- 0;
35+
// for (t in 2:T) {
36+
// err <- y[t-1] - (mu + phi * y[t-1] + theta * err[t-1]);
37+
// err ~ normal(0,sigma);
38+
// }
39+

0 commit comments

Comments
 (0)