2017-02-22 23:59:43 +00:00
|
|
|
data {
|
|
|
|
|
int T; // Sample size
|
|
|
|
|
int<lower=1> K; // Number of seasonal vectors
|
2017-03-09 16:47:59 +00:00
|
|
|
real t[T]; // Day
|
|
|
|
|
real y[T]; // Time-series
|
2017-02-28 08:08:37 +00:00
|
|
|
int S; // Number of changepoints
|
2017-03-09 16:47:59 +00:00
|
|
|
real A[T, S]; // Split indicators
|
2017-02-28 08:08:37 +00:00
|
|
|
real t_change[S]; // Index of changepoints
|
2017-03-09 16:47:59 +00:00
|
|
|
real X[T,K]; // season vectors
|
2017-02-22 23:59:43 +00:00
|
|
|
real<lower=0> sigma; // scale on seasonality prior
|
|
|
|
|
real<lower=0> tau; // scale on changepoints prior
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parameters {
|
|
|
|
|
real k; // Base growth rate
|
|
|
|
|
real m; // offset
|
2017-03-09 16:47:59 +00:00
|
|
|
real delta[S]; // Rate adjustments
|
2017-02-22 23:59:43 +00:00
|
|
|
real<lower=0> sigma_obs; // Observation noise (incl. seasonal variation)
|
2017-03-09 16:47:59 +00:00
|
|
|
real beta[K]; // seasonal vector
|
2017-02-22 23:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transformed parameters {
|
2017-03-09 16:47:59 +00:00
|
|
|
real gamma[S]; // adjusted offsets, for piecewise continuity
|
2017-02-22 23:59:43 +00:00
|
|
|
|
|
|
|
|
for (i in 1:S) {
|
2017-02-28 08:08:37 +00:00
|
|
|
gamma[i] = -t_change[i] * delta[i];
|
2017-02-22 23:59:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model {
|
2017-03-09 16:47:59 +00:00
|
|
|
real Y[T];
|
|
|
|
|
|
2017-02-22 23:59:43 +00:00
|
|
|
//priors
|
|
|
|
|
k ~ normal(0, 5);
|
|
|
|
|
m ~ normal(0, 5);
|
|
|
|
|
delta ~ double_exponential(0, tau);
|
|
|
|
|
sigma_obs ~ normal(0, 0.5);
|
|
|
|
|
beta ~ normal(0, sigma);
|
|
|
|
|
|
|
|
|
|
// Likelihood
|
2017-03-09 16:47:59 +00:00
|
|
|
for (i in 1:T) {
|
|
|
|
|
Y[i] = (dot_product(A[i], delta) + k) * t[i] + (dot_product(A[i], gamma) + m) + dot_product(X[i], beta);
|
|
|
|
|
}
|
|
|
|
|
y ~ normal(Y, sigma_obs);
|
2017-02-22 23:59:43 +00:00
|
|
|
}
|