motivation I’d like check I can recover parameters of a simulation of a model, after quite some testing I find it’s quite annoying for complex models, and I’d like to find a way to write a single model file. For purposes of discussion, a simple simulation file
model { }
generated quantities {
real mu = normal_rng(0.0, 1.0);
real x = normal_rng(mu, 1.0);
}
a corresponding model
data {
real x;
}
parameters {
real mu_h;
}
model {
mu_h ~ normal(0.0, 1.0);
x ~ normal(mu_h, 1.0);
}
I can open both files in my editor and be confident they’re consistent, but only for simple cases. For more complex cases, the translation of _rng
and sampling statements (and their maintenance) is onerous (though HOFs or a language feature to allow for a function to be used both as _lp
and _rng
would ameliorate the situation).
question I started to think I could use a single file for both, and a flag variable in the data
section to switch on/off the model, with the assumption that, if I’m not predicting the data, the sampler will generate samples equivalent to those _rng
calls,
data {
real x;
int use_data;
}
parameters {
real mu_h;
}
model {
mu_h ~ normal(0.0, 1.0);
if (use_data == 1)
x ~ normal(mu_h, 1.0);
}
generated quantities {
real x_pp = normal_rng(mu_h, 1.0);
}
Obviously, when doing inference on simulated data, I’d s/x_pp/x/g but are there other obvious problems with this second, single file approach? My guess is the sampling for simulation is a bit costlier but not much compared to guarantee that the simulation and inference is consistent.
If there are better ways, or examples I should’ve found I’m all ears; I did try searching for this but didn’t turn up much.
edit there’s still duplicated code between the if (use_data ==1)
sampling statements and the GQ _rng
section, though, so any tips to reduce that would be great too.