hello, I just entered the forum.

I have data, then I model it with the mixture.

After getting the number of components of the mixture, the proportion of each component of the mixture, then how to know which each observation is included in which component?

See the Recovering posterior mixture proportions section of the SUG. That section links to code for change-point models, but here’s the same idea for the mixture model that section describes later:

```
data {
int<lower=1> K; // number of mixture components
int<lower=1> N; // number of data points
real y[N]; // observations
}
parameters {
simplex[K] theta; // mixing proportions
ordered[K] mu; // locations of mixture components
vector<lower=0>[K] sigma; // scales of mixture components
}
model {
vector[K] log_theta = log(theta); // cache log calculation
sigma ~ lognormal(0, 2);
mu ~ normal(0, 10);
for (n in 1:N) {
vector[K] lps = log_theta;
for (k in 1:K)
lps[k] += normal_lpdf(y[n] | mu[k], sigma[k]);
target += log_sum_exp(lps);
}
}
generated quantities{
matrix[K,N] membership_probs ;
{
vector[K] log_theta = log(theta); // cache log calculation
for (n in 1:N) {
vector[K] lps = log_theta;
for (k in 1:K)
lps[k] += normal_lpdf(y[n] | mu[k], sigma[k]);
membership_probs[,n] = exp(lps) ;
}
}
}
```

Correction: On reflection, the code I included will give you the `membership prob * likelihood`

for each observation, which if you check won’t necessarily sum to 1 across components for a given observation. That quantity might nonetheless be useful, and you can normalize each observations values to a probability-like scale by rescaling them to sum to one.

1 Like