prophet.stan (prophet-0.7) | : | prophet.stan (prophet-1.0) | ||
---|---|---|---|---|
skipping to change at line 30 | skipping to change at line 30 | |||
while ((cp_idx <= S) && (t[i] >= t_change[cp_idx])) { | while ((cp_idx <= S) && (t[i] >= t_change[cp_idx])) { | |||
a_row[cp_idx] = 1; | a_row[cp_idx] = 1; | |||
cp_idx = cp_idx + 1; | cp_idx = cp_idx + 1; | |||
} | } | |||
A[i] = a_row; | A[i] = a_row; | |||
} | } | |||
return A; | return A; | |||
} | } | |||
// Logistic trend functions | // Logistic trend functions | |||
vector logistic_gamma(real k, real m, vector delta, vector t_change, int S) { | vector logistic_gamma(real k, real m, vector delta, vector t_change, int S) { | |||
vector[S] gamma; // adjusted offsets, for piecewise continuity | vector[S] gamma; // adjusted offsets, for piecewise continuity | |||
vector[S + 1] k_s; // actual rate in each segment | vector[S + 1] k_s; // actual rate in each segment | |||
real m_pr; | real m_pr; | |||
// Compute the rate in each segment | // Compute the rate in each segment | |||
k_s = append_row(k, k + cumulative_sum(delta)); | k_s = append_row(k, k + cumulative_sum(delta)); | |||
// Piecewise offsets | // Piecewise offsets | |||
m_pr = m; // The offset in the previous segment | m_pr = m; // The offset in the previous segment | |||
skipping to change at line 65 | skipping to change at line 64 | |||
vector t_change, | vector t_change, | |||
int S | int S | |||
) { | ) { | |||
vector[S] gamma; | vector[S] gamma; | |||
gamma = logistic_gamma(k, m, delta, t_change, S); | gamma = logistic_gamma(k, m, delta, t_change, S); | |||
return cap .* inv_logit((k + A * delta) .* (t - (m + A * gamma))); | return cap .* inv_logit((k + A * delta) .* (t - (m + A * gamma))); | |||
} | } | |||
// Linear trend function | // Linear trend function | |||
vector linear_trend( | vector linear_trend( | |||
real k, | real k, | |||
real m, | real m, | |||
vector delta, | vector delta, | |||
vector t, | vector t, | |||
matrix A, | matrix A, | |||
vector t_change | vector t_change | |||
) { | ) { | |||
return (k + A * delta) .* t + (m + A * (-t_change .* delta)); | return (k + A * delta) .* t + (m + A * (-t_change .* delta)); | |||
} | } | |||
// Flat trend function | ||||
vector flat_trend( | ||||
real m, | ||||
int T | ||||
) { | ||||
return rep_vector(m, T); | ||||
} | ||||
} | } | |||
data { | data { | |||
int T; // Number of time periods | int T; // Number of time periods | |||
int<lower=1> K; // Number of regressors | int<lower=1> K; // Number of regressors | |||
vector[T] t; // Time | vector[T] t; // Time | |||
vector[T] cap; // Capacities for logistic trend | vector[T] cap; // Capacities for logistic trend | |||
vector[T] y; // Time series | vector[T] y; // Time series | |||
int S; // Number of changepoints | int S; // Number of changepoints | |||
vector[S] t_change; // Times of trend changepoints | vector[S] t_change; // Times of trend changepoints | |||
matrix[T,K] X; // Regressors | matrix[T,K] X; // Regressors | |||
vector[K] sigmas; // Scale on seasonality prior | vector[K] sigmas; // Scale on seasonality prior | |||
real<lower=0> tau; // Scale on changepoints prior | real<lower=0> tau; // Scale on changepoints prior | |||
int trend_indicator; // 0 for linear, 1 for logistic | int trend_indicator; // 0 for linear, 1 for logistic, 2 for flat | |||
vector[K] s_a; // Indicator of additive features | vector[K] s_a; // Indicator of additive features | |||
vector[K] s_m; // Indicator of multiplicative features | vector[K] s_m; // Indicator of multiplicative features | |||
} | } | |||
transformed data { | transformed data { | |||
matrix[T, S] A; | matrix[T, S] A; | |||
A = get_changepoint_matrix(t, t_change, T, S); | A = get_changepoint_matrix(t, t_change, T, S); | |||
} | } | |||
parameters { | parameters { | |||
real k; // Base trend growth rate | real k; // Base trend growth rate | |||
real m; // Trend offset | real m; // Trend offset | |||
vector[S] delta; // Trend rate adjustments | vector[S] delta; // Trend rate adjustments | |||
real<lower=0> sigma_obs; // Observation noise | real<lower=0> sigma_obs; // Observation noise | |||
vector[K] beta; // Regressor coefficients | vector[K] beta; // Regressor coefficients | |||
} | } | |||
transformed parameters { | ||||
vector[T] trend; | ||||
if (trend_indicator == 0) { | ||||
trend = linear_trend(k, m, delta, t, A, t_change); | ||||
} else if (trend_indicator == 1) { | ||||
trend = logistic_trend(k, m, delta, t, cap, A, t_change, S); | ||||
} else if (trend_indicator == 2) { | ||||
trend = flat_trend(m, T); | ||||
} | ||||
} | ||||
model { | model { | |||
//priors | //priors | |||
k ~ normal(0, 5); | k ~ normal(0, 5); | |||
m ~ normal(0, 5); | m ~ normal(0, 5); | |||
delta ~ double_exponential(0, tau); | delta ~ double_exponential(0, tau); | |||
sigma_obs ~ normal(0, 0.5); | sigma_obs ~ normal(0, 0.5); | |||
beta ~ normal(0, sigmas); | beta ~ normal(0, sigmas); | |||
// Likelihood | // Likelihood | |||
if (trend_indicator == 0) { | y ~ normal( | |||
y ~ normal( | trend | |||
linear_trend(k, m, delta, t, A, t_change) | ||||
.* (1 + X * (beta .* s_m)) | .* (1 + X * (beta .* s_m)) | |||
+ X * (beta .* s_a), | + X * (beta .* s_a), | |||
sigma_obs | sigma_obs | |||
); | ); | |||
} else if (trend_indicator == 1) { | ||||
y ~ normal( | ||||
logistic_trend(k, m, delta, t, cap, A, t_change, S) | ||||
.* (1 + X * (beta .* s_m)) | ||||
+ X * (beta .* s_a), | ||||
sigma_obs | ||||
); | ||||
} | ||||
} | } | |||
End of changes. 7 change blocks. | ||||
14 lines changed or deleted | 22 lines changed or added |