prophet_linear_growth.stan 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the BSD-style license found in the
  5. # LICENSE file in the root directory of this source tree. An additional grant
  6. # of patent rights can be found in the PATENTS file in the same directory.
  7. data {
  8. int T; // Sample size
  9. int<lower=1> K; // Number of seasonal vectors
  10. vector[T] t; // Day
  11. vector[T] y; // Time-series
  12. int S; // Number of split points
  13. matrix[T, S] A; // Split indicators
  14. int s_indx[S]; // Index of split points
  15. matrix[T,K] X; // season vectors
  16. real<lower=0> sigma; // scale on seasonality prior
  17. real<lower=0> tau; // scale on changepoints prior
  18. }
  19. parameters {
  20. real k; // Base growth rate
  21. real m; // offset
  22. vector[S] delta; // Rate adjustments
  23. real<lower=0> sigma_obs; // Observation noise (incl. seasonal variation)
  24. vector[K] beta; // seasonal vector
  25. }
  26. transformed parameters {
  27. vector[S] gamma; // adjusted offsets, for piecewise continuity
  28. for (i in 1:S) {
  29. gamma[i] = -t[s_indx[i]] * delta[i];
  30. }
  31. }
  32. model {
  33. //priors
  34. k ~ normal(0, 5);
  35. m ~ normal(0, 5);
  36. delta ~ double_exponential(0, tau);
  37. sigma_obs ~ normal(0, 0.5);
  38. beta ~ normal(0, sigma);
  39. // Likelihood
  40. y ~ normal((k + A * delta) .* t + (m + A * gamma) + X * beta, sigma_obs);
  41. }