Hi, I get two warnings from cmdstan:
-
38 of 16000 (0.24%) divergent transitions,
-
E-BFMI = 0.27 is smaller than 0.3.
Is it ok to publish with these results? If not, how can I fix this. Is there a way to reparametrize the model, as the warnings suggest? Thanks!
Code
gaussian_mixture.stan
// Guassian mixture model
data {
int<lower=1> N; // number of data points
real y[N]; // observed values
real uncertainties[N]; // uncertainties
}
parameters {
vector[N] y_true; // true value
real<lower=0,upper=1> p; // mixing proportion
ordered[2] mu; // locations of mixture components
real<lower=0> sigma; // spread of mixture components
}
model {
sigma ~ normal(0, 0.1);
mu[1] ~ normal(-1.4, 0.2);
mu[2] ~ normal(-1.0, 0.2);
p ~ beta(2, 2);
for (n in 1:N) {
target += log_mix(p,
normal_lpdf(y_true[n] | mu[1], sigma),
normal_lpdf(y_true[n] | mu[2], sigma));
}
y ~ normal(y_true, uncertainties); // Estimate true values
}
gaussian_mixture.py
"""
Run Gaussian mixture model
Usage
------
Instal cmdstanpy and run:
python gaussian_mixture.py
"""
from cmdstanpy import CmdStanModel
def get_data():
"""Returns data from observations"""
values = [-0.99, -1.37, -1.38, -1.51, -1.29, -1.34, -1.50,
-0.93, -0.83, -1.46, -1.07, -1.28, -0.73]
uncertainties = [ 0.12, 0.05, 0.11, 0.18, 0.03, 0.19, 0.18,
0.12, 0.19, 0.09, 0.11, 0.16, 0.08]
return {
"y": values,
"uncertainties": uncertainties,
"N": len(values)
}
def sample():
model = CmdStanModel(stan_file="gaussian_mixture.stan")
fit = model.sample(data=get_data(), seed=2,
adapt_delta=0.99, max_treedepth=10,
sampling_iters=4000, warmup_iters=2000,
chains=4, cores=4,
show_progress=True)
fit.diagnose()
print(fit.summary())
if __name__ == '__main__':
sample()
print('We are done')
Diagnose
Checking sampler transitions treedepth.
Treedepth satisfactory for all transitions.
Checking sampler transitions for divergences.
38 of 16000 (0.24%) transitions ended with a divergence.
These divergent transitions indicate that HMC is not fully able to explore the posterior distribution.
Try increasing adapt delta closer to 1.
If this doesn't remove all divergences, try to reparameterize the model.
Checking E-BFMI - sampler transitions HMC potential energy.
The E-BFMI, 0.27, is below the nominal threshold of 0.3 which suggests that HMC may have trouble exploring the target distribution.
If possible, try to reparameterize the model.
Effective sample size satisfactory.
Split R-hat values satisfactory all parameters.
Processing complete.
Summary
Mean MCSE StdDev 5% 50% 95% N_Eff N_Eff/s R_hat
name
lp__ -6.537390 0.275141 7.408600 -16.793700 -7.586300 7.200930 725.039 32.4301 1.00424
y_true[1] -0.976441 0.003064 0.126747 -1.237560 -0.952827 -0.807648 1711.500 76.5533 1.00239
y_true[2] -1.360220 0.000538 0.044592 -1.434220 -1.358910 -1.289390 6866.330 307.1230 1.00008
y_true[3] -1.354420 0.000832 0.076927 -1.483440 -1.352010 -1.234470 8558.720 382.8210 1.00034
y_true[4] -1.369540 0.002311 0.110534 -1.549620 -1.365010 -1.206390 2288.200 102.3480 1.00073
y_true[5] -1.299600 0.000471 0.030118 -1.348380 -1.300000 -1.248750 4082.230 182.5930 1.00059
y_true[6] -1.312740 0.002470 0.129519 -1.488850 -1.331420 -1.034680 2749.290 122.9720 1.00134
y_true[7] -1.370810 0.001268 0.103229 -1.546730 -1.363740 -1.215370 6627.500 296.4400 1.00051
y_true[8] -0.931585 0.001755 0.103812 -1.124750 -0.920968 -0.779502 3500.720 156.5830 1.00013
y_true[9] -0.938734 0.003833 0.148755 -1.267420 -0.913550 -0.741579 1506.470 67.3827 1.00257
y_true[10] -1.395070 0.001130 0.072108 -1.522230 -1.388680 -1.288920 4070.490 182.0680 1.00074
y_true[11] -1.063860 0.004761 0.149094 -1.322110 -1.034960 -0.856531 980.762 43.8683 1.00256
y_true[12] -1.294110 0.003268 0.128396 -1.458530 -1.318990 -1.013870 1543.310 69.0304 1.00491
y_true[13] -0.819926 0.001357 0.075550 -0.939482 -0.822643 -0.689664 3098.440 138.5900 1.00082
p 0.600030 0.001998 0.146755 0.342263 0.610543 0.824460 5394.000 241.2670 1.00194
mu[1] -1.344840 0.000956 0.060511 -1.441400 -1.344240 -1.246820 4008.850 179.3110 1.00167
mu[2] -0.924330 0.001880 0.099102 -1.119760 -0.914959 -0.780432 2778.630 124.2850 1.00200
sigma 0.092938 0.001604 0.054514 0.020375 0.082860 0.197310 1154.410 51.6353 1.00202