Generating log_like from mixture model


#1

Hello,
I have a simple Poisson mixture model that I am using to demonstrate some model checking techniques. I have been able to generate samples from the posterior predictive distribution, but I am running into some issues in creating the log likelihood in the generated quantities. Not sure how to translate the target syntax in the generated quantities block.

Here is the code

// Simple 3 component poisson mixture
data {
  int N;
  int<lower=0> y[N];
  int n_groups;
}
parameters {
  ordered[n_groups] mu;
  simplex[n_groups] Theta;
}
model {
  vector[n_groups] contributions;
  // priors
  mu ~ normal(0, 10);
  Theta ~ dirichlet(rep_vector(2.0, n_groups));
  
  
  // likelihood
  for(n in 1:N) {
    for(k in 1:n_groups) {
      contributions[k] = log(Theta[k]) + poisson_log_lpmf(y[n] | mu[k]);
    }
    target += log_sum_exp(contributions);
  }
}

generated quantities {
    int z; // class index
    int<lower=0> y_rep[N];
    vector[N] log_lik;
  
    for (n in 1:N) {
    log_lik[n]= log_sum_exp(contributions);  //stuck here
    }
    
    for (i in 1:N) {
        z = categorical_rng(Theta);
        y_rep[i] = poisson_log_rng(mu[z]);
    }
}

Thanks for any help you can provide!!
-Patrick


#2

Would this work?

vector[N] log_lik;
for(n in 1:N){
  vector[n_groups] contributions;
  for(k in 1:n_groups){
    contributions[k] = log(Theta[k]) + poisson_log_lpmf(y[n] | mu[k]);
  }
  log_lik[n] = log_sum_exp(contributions);
}

?


#3

That worked thanks!! I was trying something similar but I must of ran into a syntax error before. I am still trying to get the hang of generated quantities. My initial idea was not re creating contributions but I just realized that it was created in the model block and not the parameters block.