That's
> On Mar 1, 2016, at 7:40 AM, Chris <
wel...@gmail.com> wrote:
>
> Hi Bob and others,
>
> I got a three component mixture working with
>
> for (n in 1:N) {
> increment_log_prob(log_sum_exp(log(theta) +
> normal_log(y[n], X[n,]*betas1, sigma[1]) +
> normal_log(y[n], X[n,]*betas2, sigma[2]) +
> normal_log(y[n], X[n,]*betas3, sigma[3])));
> }
>
I'm afraid that's not a 3-component mixture. Assuming theta is
a 3-simplex, the mixture would look as follows (assuming betas is made
into a vector):
for (n in 1:N) {
vector[3] lp;
lp <- log(theta);
for (k in 1:3)
lp[k] <- normal_log(y[n], X[n] * betas[k], sigma[k]);
increment_log_prob(log_sum_exp(lp));
}
There's a discussion of why the algebra is this way
in the manual chapter on mixtures.
> Can you explain to me please how I would proceed to define a mixture where the number of mixture components K
> is also estimated?
If you're thinking of Dirichlet processes, we don't really do
that in Stan. You could technically make a mixture of mixtures
and evaluate this, but it's usually not worth the effort.
> And how do I calculate the log likelihood for this model in the generated quantities part?
Just follow the likelihood definition in the model block,
only use foo_log(y, ...) rather than y ~ foo(...).
> the log likelihood of a three component mixture with distributions f and mixing proportion p for n observations is log(prod_n(p1*f1 + p2*f2 + p3*f3)).
I don't know what prod_n is.
- Bob