Intro To em
Intro To em
Xingjie Shi
Dec 7, 23
Example
In this example, we will assume our mixture components are fully specified Gaussian distributions (i.e the
means and variances are known), and we are interested in finding the maximum likelihood estimates of the
πk ’s.
Assume we have K = 2 components, so that:
The true mixture proportions will be P (Zi = 0) = 0.25 and P (Zi = 1) = 0.75. First we simulate data from
this mixture model:
# mixture components
mu.true = c(5, 10)
sigma.true = c(1.5, 2)
# determine Z_i
Z = rbinom(500, 1, 0.75)
# sample from mixture model
1
Histogram of X
1500
1000
Frequency
500
0
0 5 10 15
X
Now we write a function to compute the log-likelihood for the incomplete data, assuming the parameters are
known. This will be used to determine convergence:
Xn X2
ℓ(θ) = log πk N (xi ; µk , σk2 )
| {z }
i=1 k=1
L[i,k]
Since the mixture components are fully specified, for each sample Xi we can compute the likelihood
P (Xi |Zi = 0) and P (Xi |Zi = 1). We store these values in the columns of L:
l <- function(X, mu, sigma) {
L = matrix(NA, nrow=length(X), ncol= 2)
L[, 1] = dnorm(X, mean=mu[1], sd = sigma[1])
L[, 2] = dnorm(X, mean=mu[2], sd = sigma[2])
return (L)
}
Finally, we implement the E and M step in the EM.iter function below. The mixture.EM function is the
driver which checks for convergence by computing the log-likelihoods at each step.
mixture.EM <- function(X, w.init, mu.init, sigma.init) {
2
# store log-likehoods for each iteration
log_liks <- c()
L <- l(X, mu.curr, sigma.curr)
ll <- compute.log.lik(L, w.curr)
log_liks <- c(log_liks, ll)
delta.ll <- 1e5
# M-step
w.next <- Nk/sum(Nk)
mu.next <- colSums(gam*X)/Nk
sigma.next <- numeric(K)
for(i in 1:K) {
sigma.next[i] <- sqrt(sum(gam[,i] * (X - mu.next[i])ˆ2)/Nk[i])
}
list (w.next = w.next,
mu.next = mu.next,
sigma.next = sigma.next)
}
#perform EM
ee <- mixture.EM(X, w.init=c(0.5,0.5), mu.init=c(4,12), sigma.init = c(2,2))
print(paste("proportion = (", round(ee[[1]][1],2), ",", round(ee[[1]][2],2), ")", sep=""))
3
Finally, we inspect the evolution of the log-likelihood and note that it is strictly increases:
plot(ee[[4]], ylab='marginal log-likelihood', xlab='iteration')
marginal log−likelihood
−26000
−28000
0 50 100 150
iteration
HW
Please provide an EM algorithm for GMM with arbitrary number of components and dimensions.