Technical reference for the Stan models powering Murphet's time-series forecasting
This document provides a comprehensive reference for the Stan models that power Murphet's forecasting capabilities. Understanding these parameters will help you customize the model for specific time-series characteristics and optimize performance.
Murphet implements two primary likelihood models:
murphet_beta.stan
: Beta likelihood for bounded (0,1) values like conversion ratesmurphet_gauss.stan
: Gaussian/Student-t likelihood for unbounded valuesThe model has this overall structure:
y ~ Likelihood(ΞΌ, dispersion_params)
ΞΌ = f_trend(t) + f_seasonal(t) + f_AR(t)
Where each component is carefully parameterized with thoughtful priors.
Parameter | Dimensions | Purpose | Prior (Beta) | Prior (Gaussian) | Tuning Range |
---|---|---|---|---|---|
k |
scalar | Base slope | π©(0, 0.5) | π©(0, 0.5) | Not typically tuned |
m |
scalar | Intercept | π©(0, 5) | π©(0, 5) | Not typically tuned |
delta |
[num_cp] | Changepoint adjustments | Laplace(0, Ξ΄_scale) | Laplace(0, Ξ΄_scale) | β |
delta_scale |
scalar | Regularization strength | β | β | 0.01-0.6 |
gamma |
scalar | CP transition steepness | Ξ(3, 1/Ξ³_scale) | Ξ(3, 1/Ξ³_scale) | β |
gamma_scale |
scalar | Prior scale for Ξ³ | β | β | 0.5-15 |
Parameter | Dimensions | Purpose | Prior | Tuning Range |
---|---|---|---|---|
A_sin |
[total_harmonics] | Sine coefficients | π©(0, 10Β·season_scale) | β |
B_cos |
[total_harmonics] | Cosine coefficients | π©(0, 10Β·season_scale) | β |
season_scale |
scalar | Seasonality prior strength | β | 0.3-2.0 |
num_seasons |
scalar | Number of seasonal periods | β | β |
period |
[num_seasons] | Length of each period | β | β |
n_harmonics |
[num_seasons] | Fourier terms per period | β | 1-10 |
Parameter | Purpose | Prior (Beta) | Prior (Gaussian) | Constraints |
---|---|---|---|---|
rho |
AR(1) coefficient | π©(0, 0.3)β | π©(0, 0.3)β | (-1, 1) |
mu0 |
Initial AR state | π©(logit(Θ³), 1) | π©(Θ³, 1) | β |
β Prior SD automatically expands to 0.5 for quarterly data.
Parameter | Purpose | Prior | Notes |
---|---|---|---|
log_phi0 |
Base precision (log scale) | π©(log(20), 1) | Higher = narrower predictions |
beta_phi |
Heteroscedasticity strength | π©(0, 0.3) | 0 = homoscedastic |
Parameter | Purpose | Prior | Notes |
---|---|---|---|
log_sigma0 |
Base scale (log) | π©(log(sd(y)), 1) | Base noise level |
beta_sigma |
Heteroscedasticity strength | π©(0, 0.5) | 0 = homoscedastic |
nu |
Student-t degrees of freedom | Exp(1/30) | β₯2; larger = more Gaussian |
The Stan data block (automatically constructed by fit_churn_model
):
int<lower=1> N; // number of observations
vector[N] t; // time points
vector[N] y; // target values: 0<y<1 for Beta, β for Gaussian
int<lower=0> num_changepoints; // number of trend changepoints
vector[num_changepoints] s; // changepoint locations
real<lower=0> delta_scale, gamma_scale; // trend prior scales
int<lower=1> num_seasons; // number of seasonal components
array[num_seasons] int n_harmonics; // harmonics per seasonal component
array[num_seasons] real period; // period lengths
int<lower=1> total_harmonics; // sum(n_harmonics)
real<lower=0> season_scale; // seasonality prior scale
Parameter | Daily | Monthly | Quarterly |
---|---|---|---|
gamma_scale |
3.0-12.0 | 1.0-8.0 | 0.5-4.0 |
delta_scale |
0.05-0.6 | 0.02-0.4 | 0.01-0.3 |
n_changepoints |
β€14 | β€10 | β€6 |
season_scale |
0.3-2.0 | 0.3-2.0 | 0.3-1.5 |
Data Characteristic | Recommendation |
---|---|
High volatility | β delta_scale , β Gaussian nu |
Strong seasonality | β num_harmonics , β season_scale |
Abrupt changes | β gamma_scale , β n_changepoints |
Smooth trends | β delta_scale , β gamma_scale |
Stable errors | β beta_phi /beta_sigma |
Heteroscedastic errors | β beta_phi /beta_sigma |
Boundary violations: Ensure likelihood="beta"
for 0-1 bounded data.
Adaptation failures: If Stan reports "divergences" or "treedepth warnings":
beta_phi
(β€0.3)adapt_delta
(0.95-0.99)max_treedepth
(12-15)Excessive changepoints: Consider reducing n_changepoints
with shorter series.
Overfitting seasons: Reduce num_harmonics
and season_scale
for noisy data.
AR(1) instability: Keep rho
priors reasonably tight unless you have strong evidence for autocorrelation.
Version | Date | Key Changes |
---|---|---|
1.3.0 | 2025-04-19 | Heteroscedastic Ο/Ο, Student-t Ξ½, expanded AR(1) |
These posterior predictive quantities are available for NUTS inference:
Variable | Dimensions | Purpose |
---|---|---|
y_rep |
[N] | Posterior predictive sample (for PPC) |
log_lik |
[N] | Pointwise log-likelihood (for WAIC/LOO) |
Start simple: Begin with default priors before customizing.
Model selection: Use cross-validation or WAIC/LOO for comparing specifications.
Prior predictive checks: For advanced users, examine the behavior of priors with generated quantities
block.
Inference method progression: Start with MAP for exploration, then ADVI/NUTS for uncertainty.
Heteroscedasticity: Enable only after confirming residual variance pattern.
For practical examples and Optuna HPO recipes, see the companion documents on GitHub.