I was reading chapter '14.2 Change Point Models' of the Stan manual, specifically the example and model related to the coal mining disasters.
Could the conditional operator be replaced by a continuous step mixing function in order to simplify the model?
I have implemented the example in full and the results are consistent with all the other solutions I have seen. Full code below.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
require(Rcpp)
require(rstan)
stanmodel <- '
data {
// signal data
int<lower=1> N;
int y[N]; // value
}
parameters {
real<lower=1> event; // change point day
real<lower=0> lambda[2]; // rates before and after change point
}
model {
event ~ uniform(1,112); // [1, max(day)]
lambda ~ uniform(0,10); // [1, max(y)]
for (i in 1:N)
{
real alpha;
real beta;
alpha = 1.0 / (1 + exp(event - i - 0.5)^15); // step function modeling change point
beta = lambda[1] *(1-alpha) + lambda[2]*alpha; // rates mixing
y[i] ~ poisson(beta);
}
}
'
# data
y <- c(4,5,4,1,0,4,3,4,0,6,
3,3,4,0,2,6,3,3,5,4,5,3,1,4,4,1,5,5,3,4,2,5,2,2,3,4,
2,1,3,2,1,1,1,1,1,3,0,0,1,0,1,1,0,0,3,1,0,3,2,2,0,1,
1,1,0,1,0,1,0,0,0,2,1,0,0,0,1,1,0,2,2,3,1,1,2,1,1,1,
1,2,4,2,0,0,0,1,4,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0)
N = length(y)
mdl <- stan(model_code=stanmodel, data= c("N", "y" ), iter=4000, chains=8, cores=8, control = list(adapt_delta = 0.95) )
# extra results and plot
t <- extract(mdl)$event # change point distribution'
day <- 1:N
par(mfrow=c(2,1))
hist(t, col='lightBlue', freq=F, xlab="year", ylab="P(t)", main="", breaks=40)
abline(v=mean(t), col='red')
plot(y, cex=0.8, xlab='year', ylab='d')
lines(y)
abline(v=mean(t), col='red')