Dear Stan users,
I’m trying to fit a model that describes my data as a series of univariate Normal distributions with varying mean and SD depending on a parameter SNR.
I hypothesize that for each SNR level (ranging from 2 to 5), the corresponding data would be distributed according to a Normal distribution with its own mean and SD, and that these means and SDs would vary monotonously with SNR within bounds.
This is the model I wrote.
There are strong convergence issues which have me guessing I wrote something poorly, or plain wrong.
I added the output of my model diagnostics below the Stan code here (default fitting options besides adapt_delta 0.99).
Could you point me in the right direction? I’m not sure what to do next.
functions{
}
data {
int Nobs; // number of data obervations
real mu_low; //prior knowledge from external data
real sigma_low; //prior knowledge from external data
vector[Nobs] dat; // data, one per row
int<lower = 2, upper = 6> SNR[Nobs]; // experimental factor
}
parameters {
vector[5] b;// unconstrained parameters
real<lower = 0> pos_b; // postive parameters
}
transformed parameters{
real mu;
real sigma;
for(i in 1:Nobs){
mu = b[1] + (b[2]-b[1])/(1+exp(-b[3]*(SNR[i] - b[4])));
sigma = pos_b/(1+exp(-b[5]*(SNR[i] - b[4])));
}
}
model {
b[1] ~ normal(mu_low,30); // min
b[2] ~ normal(mu_low,30); // max
b[3] ~ normal(0,10); // slope
b[4] ~ normal(4,10); // threshold
b[5] ~ normal(0,10); // slope sigma
pos_b ~ normal(sigma_low,30); // max sigma
//LLH
for(i in 1:Nobs){
target += normal_lpdf(dat[i] | mu,sigma);
}
}
generated quantities{
vector[Nobs] log_lik;
for (i in 1:Nobs){
log_lik[i] = normal_lpdf(dat[i]|mu, sigma);
}
}
Warning messages:
1: There were 34 divergent transitions after warmup. Increasing adapt_delta above 0.99 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
2: There were 492 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
http://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded
3: Examine the pairs() plot to diagnose sampling problems
>print(uni_stan, pars = c("b"), probs = c(0.025, 0.975),digits=3)
Inference for Stan model: unimodal.
4 chains, each with iter=2000; warmup=1000; thin=1;
post-warmup draws per chain=1000, total post-warmup draws=4000.
mean se_mean sd 2.5% 97.5% n_eff Rhat
b[1] 23.534 4.957 37.228 -57.559 57.219 56 1.050
b[2] 21.662 5.535 35.624 -56.190 56.867 41 1.109
b[3] 1.692 1.235 9.941 -18.292 21.061 65 1.041
b[4] 1.462 4.955 9.834 -16.486 21.333 4 1.605
b[5] 1.200 4.911 9.773 -18.194 19.233 4 1.584
Samples were drawn using NUTS(diag_e) at Wed Mar 25 16:05:13 2020.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).