# Generating log\_like from mixture model

**URL:** <https://discourse.mc-stan.org/t/generating-log-like-from-mixture-model/2487>\
**Category:** Modeling\
**Created:** [November 11, 2017, 9:59pm UTC](https://discourse.mc-stan.org/t/generating-log-like-from-mixture-model/2487 "2017-11-11T21:59:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@Patrick](https://discourse.mc-stan.org/u/Patrick)\
**Post date:** [November 11, 2017, 9:59pm UTC](https://discourse.mc-stan.org/t/generating-log-like-from-mixture-model/2487/1 "2017-11-11T21:59:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)\
**Post date:** [November 12, 2017, 6:18am UTC](https://discourse.mc-stan.org/t/generating-log-like-from-mixture-model/2487/2 "2017-11-12T06:18:34Z")

</div>

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);
}

```

?

---

<div class="post-metadata">

**Author:** ![Patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@Patrick](https://discourse.mc-stan.org/u/Patrick)\
**Post date:** [November 12, 2017, 5:06pm UTC](https://discourse.mc-stan.org/t/generating-log-like-from-mixture-model/2487/3 "2017-11-12T17:06:15Z")

</div>

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.
