
  post = matrix(nrow=n, ncol=K) # to store the posterior probabilities
  lik = rep(0,K) # a vector to store the likelihoods
  for(i in 1:n){
    for(k in 1:K){
    ...
    }
    ...
  }
  return(post)
}</code></pre>
<p>Here we can run this on the example data, and compare the posterior probabilities with the true labels.</p>
<pre class="r"><code>set.seed(123)
sim = r_simplemix(n=20,P)
posterior = posterior_prob_assignment(sim$x,P,w=c(0.5,0.5))
plot(sim$z,posterior[,2],xlab=&quot;true population&quot;, ylab