Last active
March 26, 2018 06:50
-
-
Save twolodzko/77be6b6de994478f741407b05a6d6e20 to your computer and use it in GitHub Desktop.
Holt time-series forecasting in Stan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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