I’m looking to model some physiological data consisting of a periodic response induced by the heart’s pulsation. I’ve coded up a simple example here that achieves inference on the magnitude of the response and noise, but assumes a known constant pulse rate.
Here’s a version of the model that attempts to additionally do inference on the rate parameter:
functions{
// pulse: function to generate pulse-like curve; oversimplified in shape for initial model building/testing
vector pulse(vector time, real amplitude, real rate){
vector[num_elements(time)] position ;
vector[num_elements(time)] beta ;
real period = 1/rate ;
for(i in 1:num_elements(time)){
position[i] = fmod(time[i],period)/period ; //this needs to be looped as fmod isn't vectorized
beta[i] = beta_lpdf(position[i]|2,2) ; // this needs to be looped as beta_lpdf will sum it result to a real if done on a vector
}
return amplitude * exp(beta) ;
}
}
data{
int n;
vector[n] y;
vector[n] time;
}
parameters{
real<lower=0> amplitude;
real<lower=0> rate;
real<lower=0> noise;
}
model{
rate ~ weibull(2,2) ;
amplitude ~ weibull(2,2) ;
noise ~ normal(0,1) ;
y ~ normal( pulse(time,amplitude,rate) , noise ) ;
}
However, with this version Stan can’t initialize, producing the error Rejecting initial value: Gradient evaluated at the initial value is not finite. Stan can't start sampling from this initial value.
My usual solution to initialization troubles is to set init=0
in rstan::sampling()
, but this wouldn’t make sense in this scenario since zero makes little sense for the amplitude and rate parameters (also, just checked and init=0
& init=1
both yield the same error message as above). Any thoughts on how to get this to initialize properly?