Skip to content

Instantly share code, notes, and snippets.

@twolodzko
Last active March 26, 2018 06:50
Show Gist options
  • Select an option

  • Save twolodzko/77be6b6de994478f741407b05a6d6e20 to your computer and use it in GitHub Desktop.

Select an option

Save twolodzko/77be6b6de994478f741407b05a6d6e20 to your computer and use it in GitHub Desktop.
Holt time-series forecasting in Stan
data {
int<lower=3> n;
vector[n] y;
int<lower=0> h;
}
parameters {
real<lower=0, upper=1> alpha;
real<lower=0, upper=1> beta;
real<lower=0> sigma;
vector[2] mu_init;
}
transformed parameters {
real l;
real lp;
real b;
vector[n] mu;
lp = y[1];
l = lp;
b = y[2] - y[1];
mu[1:2] = mu_init;
for (t in 2:(n-1)) {
l = alpha * y[t] + (1 - alpha) * (lp + b);
b = beta * (l - lp) + (1 - beta) * b;
mu[t+1] = l + b;
lp = l;
}
}
model {
y ~ normal(mu, sigma);
}
generated quantities {
vector[h] yhat;
for (k in 1:h)
yhat[k] = normal_rng(l + b*k, sigma);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment