Nothing wrong in principle, but the algorithms tend to be
faster done separately because of coupling all the systems
step-by-step in HMC.
One thing you can try to do is pull out functionality into
functions, but don't (yet) have a way to set up a library of
functions to include (but it's on my to-do list for this year).
More on code inline.
- Bob
> On Mar 16, 2016, at 6:20 PM, Quinn Lathrop <
quinn....@gmail.com> wrote:
>
> Hi,
>
> In working with simulations, I often have competing models fit to the same data. Each model is kept in a different .stan file, but they often contain very similar syntax. My question here is, what are the consequences of putting multiple models within the same model block? More simply, is there any situation where this is a reasonable approach?
>
> I have found this helpful in communicating the differences in models and in understanding how to implement those models in stan. So the question is, is this a valid use of stan? Or should I never write model code like this ever again?
>
> ...
> data {
> int<lower=0> n_obs; //number of obs cases
> int<lower=0> n_mis; //number of missing cases
> vector[2] xy_obs[n_obs]; //xy for observed cases
> real x_mis[n_mis]; //x for cases with missing y
> }
>
> parameters {
> // Complete Case Parameters
> vector[2] mu_1;
> cov_matrix[2] sigma_1;
>
> // Available Data Parameters
> vector[2] mu_2;
> cov_matrix[2] sigma_2;
> real y_mis[n_mis];
> }
You really want to parameterize this in terms of Cholesky
factors. We strongly recommend scaling a Cholesky factor for a
correlation matrix (with an LKJ prior), but if you really want
the improper uniform on covariance matrices, it's
much more arithmetically stable and efficient to use the Cholesky
forms (of both variables and densities).
> transformed parameters{
> vector[2] xy_mis[n_mis]; //xy for cases with missing y
>
> // I could not figure out how to vectorize this loop
> // seems simple. Any tips?
> for(i in 1:n_mis){
> xy_mis[i,1] <- x_mis[i]; //x_mis is obsvered
> xy_mis[i,2] <- y_mis[i]; //y_mis is parameter
> }
> }
You can do this:
x[, 1] <- x_mis;
x[, 2] <- y_mis;
> // This model block has pedagogical qualities
I'd avoid this kind of comment, too --- comments overall make
code *less* readable if the code is reasonable.
> model {
>
> //Complete Case Model
> xy_obs ~ multi_normal(mu_1, sigma_1);
>
> //Available Data Model
> xy_obs ~ multi_normal(mu_2, sigma_2);
> xy_mis ~ multi_normal(mu_2, sigma_2);
Even more efficient if you append_put xy_mis together into
a single array of vectors. But in this case, I don't think
the xy_mis ~ ... line will affect the estimates of mu_2 and
sigma_2; instead, you could probably generate the predictions
for missing y in generated quantities more efficiently (with
help from someone better than me at conditioning a multivariate
normal). But what you have will also work.
- Bob