noirritchandra
noirritchandra

Reputation: 115

Variable "cholesky_factor_cov" does not exist in STAN model block

I am trying to simulate from the posterior distribution of marginalized Gaussian mixture model. Here is my code:


data {
  int<lower=0> d;
  int<lower=0> n;         
  int<lower=0> k;
  row_vector[d] y[n];
  vector[d] mu0;
  cov_matrix[d] sig0;
  real<lower=0> alpha;
}
parameters {
  vector[d] mu[k];
  cov_matrix[d] sig[k];
  // simplex[k] pi;
}


model {
   cholesky_factor_cov[d] chol_sig[k];
  for(i in 1:k)
    chol_sig[i]=cholesky_decompose(sig[i]);

  vector[k] lps;

  for( i in 1:k){
    target+= multi_normal_cholesky_lpdf(mu[i] | mu0, chol_sig[i]);
    target+= inv_wishart_lpdf(sig[i] | d+10, sig0);
  }


  for (i in 1:n) {
    // lps = log_thet;
    for (j in 1:k)
      lps[j] = multi_normal_cholesky_lpdf(y[i] | mu[j], chol_sig[j]);
    target += log_sum_exp(lps);
  }
}

It is throwing the following error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable "cholesky_factor_cov" does not exist.
 error in 'model728c4ff59f3c_mixture' at line 34, column 22
  -------------------------------------------------
    32: // }
    33: model {
    34:    cholesky_factor_cov[d] chol_sig[k];
                             ^
    35:   for(i in 1:k)
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  :
  failed to parse Stan model 'mixture' due to the above error.

Can I not declare variables of type ``cholesky_factor_cov" inside STAN model? Please help.

Upvotes: 2

Views: 446

Answers (1)

Ben Goodrich
Ben Goodrich

Reputation: 4990

You are not allowed to declare specializations of matrices or vectors inside the model block of a Stan program because they are not checked for validity. But you can just declare it as an array of square matrices, like

model {
  matrix[d,d] chol_sig[k];
  ...

Upvotes: 1

Related Questions