mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-07-23 19:32:25 +00:00
Add inst/stan
Signed-off-by: Kim Seonghyun <shyeon.kim@scipi.net>
This commit is contained in:
parent
3da0526cf8
commit
a3fc168464
2 changed files with 103 additions and 0 deletions
40
R/inst/stan/prophet_linear_growth.stan
Normal file
40
R/inst/stan/prophet_linear_growth.stan
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
data {
|
||||
int T; // Sample size
|
||||
int<lower=1> K; // Number of seasonal vectors
|
||||
vector[T] t; // Day
|
||||
vector[T] y; // Time-series
|
||||
int S; // Number of split points
|
||||
matrix[T, S] A; // Split indicators
|
||||
int s_indx[S]; // Index of split points
|
||||
matrix[T,K] X; // season vectors
|
||||
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
|
||||
vector[S] delta; // Rate adjustments
|
||||
real<lower=0> sigma_obs; // Observation noise (incl. seasonal variation)
|
||||
vector[K] beta; // seasonal vector
|
||||
}
|
||||
|
||||
transformed parameters {
|
||||
vector[S] gamma; // adjusted offsets, for piecewise continuity
|
||||
|
||||
for (i in 1:S) {
|
||||
gamma[i] = -t[s_indx[i]] * delta[i];
|
||||
}
|
||||
}
|
||||
|
||||
model {
|
||||
//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
|
||||
y ~ normal((k + A * delta) .* t + (m + A * gamma) + X * beta, sigma_obs);
|
||||
}
|
||||
63
R/inst/stan/prophet_logistic_growth.stan
Normal file
63
R/inst/stan/prophet_logistic_growth.stan
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
data {
|
||||
int T; // Sample size
|
||||
int<lower=1> K; // Number of seasonal vectors
|
||||
vector[T] t; // Day
|
||||
vector[T] cap; // Capacities
|
||||
vector[T] y; // Time-series
|
||||
int S; // Number of split points
|
||||
matrix[T, S] A; // Split indicators
|
||||
int s_indx[S]; // Index of split points
|
||||
matrix[T,K] X; // season vectors
|
||||
real<lower=0> sigma; // scale on seasonality prior
|
||||
real<lower=0> tau; // scale on changepoints prior
|
||||
}
|
||||
|
||||
|
||||
transformed data {
|
||||
int s_ext[S + 1]; // Segment endpoints
|
||||
for (j in 1:S) {
|
||||
s_ext[j] = s_indx[j];
|
||||
}
|
||||
s_ext[S + 1] = T + 1; // Used for the m_adj loop below.
|
||||
}
|
||||
|
||||
|
||||
parameters {
|
||||
real k; // Base growth rate
|
||||
real m; // offset
|
||||
vector[S] delta; // Rate adjustments
|
||||
real<lower=0> sigma_obs; // Observation noise (incl. seasonal variation)
|
||||
vector[K] beta; // seasonal vector
|
||||
}
|
||||
|
||||
|
||||
transformed parameters {
|
||||
vector[S] gamma; // adjusted offsets, for piecewise continuity
|
||||
vector[S + 1] k_s; // actual rate in each segment
|
||||
real m_pr;
|
||||
|
||||
// Compute the rate in each segment
|
||||
k_s[1] = k;
|
||||
for (i in 1:S) {
|
||||
k_s[i + 1] = k_s[i] + delta[i];
|
||||
}
|
||||
|
||||
// Piecewise offsets
|
||||
m_pr = m; // The offset in the previous segment
|
||||
for (i in 1:S) {
|
||||
gamma[i] = (t[s_indx[i]] - m_pr) * (1 - k_s[i] / k_s[i + 1]);
|
||||
m_pr = m_pr + gamma[i]; // update for the next segment
|
||||
}
|
||||
}
|
||||
|
||||
model {
|
||||
//priors
|
||||
k ~ normal(0, 5);
|
||||
m ~ normal(0, 5);
|
||||
delta ~ double_exponential(0, tau);
|
||||
sigma_obs ~ normal(0, 0.1);
|
||||
beta ~ normal(0, sigma);
|
||||
|
||||
// Likelihood
|
||||
y ~ normal(cap ./ (1 + exp(-(k + A * delta) .* (t - (m + A * gamma)))) + X * beta, sigma_obs);
|
||||
}
|
||||
Loading…
Reference in a new issue