Using the example in the Stan manual of marginalising out the discrete time parameter, I created a change point model for UK / South Korean trade (details here if you are interested). @rabaath suggested modelling time as continuous. Doing so results in a simpler model and to me makes more sense (to me at any rate) as time really is continuous.
I attach the Stan below. I am happy to create a PR for the Stan manual but I don’t wish to put effort into doing this if it’s not a good idea.
data {
int<lower=1> N;
vector[N] x;
vector[N] y;
}
parameters {
real<lower=0, upper=N+1> tau;
real mu1;
real mu2;
real gamma1;
real gamma2;
real<lower=0> sigma1;
real<lower=0> sigma2;
}
model {
real mu;
real gamma;
real sigma;
real weight;
mu1 ~ normal(0, 10);
mu2 ~ normal(0, 10);
gamma1 ~ normal(0, 10);
gamma2 ~ normal(0, 10);
sigma1 ~ normal(0, 10);
sigma2 ~ normal(0, 10);
for (i in 1:N) {
weight = inv_logit(2 * (i - tau));
mu = weight * mu2 + (1 - weight) * mu1;
gamma = weight * gamma2 + (1 - weight) * gamma1;
sigma = weight * sigma2 + (1 - weight) * sigma1;
y[i] ~ normal(mu * x[i] + gamma, sigma);
}
}