EM Algorithm for Gaussian Mixture
A Detailed Example of EM Algorithm for Mixture model
Given a mixture model with all Gaussian mixture, each of the observation density is defined as:
The parameters are the mixing probabilities and are the parameters for normal density function.
The expected complete-data log-likelihood now can be defined as:
Recall during the maximization step we need to find the parameter that maximizes the expected complete-data log likelihood. So we take partial derivative and set it to 0
For the mixing probability the process is a bit more involved. We can re-express the component probabilities in multinomial logit form and use the fact that mixing probabilities sums to 1:
Finally we can solve for all of them and obtain the following estimator:
EM_GaussMix2 <- function(par, y) {
# read the parameter
ps <- par[1:2]; means <- par[3:4]; sds <- par[5:6]
# compute a matrix with component density values (likelihood)
B <- cbind(dnorm(y,mean=means[1],sd=sds[1]),
dnorm(y,mean=means[2],sd=sds[2]))
# Log likelihood to check convergence
prevLL <- sum(log(colSums(apply(B,1,"*",ps))))
converge <- FALSE; i <- 1
while(!converge) {
# compute posterior state probabilities
gamma <- t(apply(B,1,"*",ps))
gamma <- gamma/rowSums(gamma)
# parameter estimates
for (j in 1:2) {
means[j] <- sum(gamma[,j]*y)/sum(gamma[,j])
sds[j] <- sqrt(sum(gamma[,j]*(y-means[j])^2)/ sum(gamma[,j]))
}
ps <- colMeans(gamma)
# component density values with new parameters
B <- cbind(dnorm(y,mean=means[1],sd=sds[1]),
dnorm(y,mean=means[2],sd=sds[2]))
# Compute Log-likelihood and check for convergence
LL <- sum(log(colSums(apply(B,1,"*",ps))))
if(LL - prevLL < 10e-8) converge <- TRUE
i <-i+1; prevLL <- LL
}
return(list(par = c(p1=ps[1], m1=means[1], m2=means[2], s1=sds[1], s2=sds[2]), niter = i, logL = LL))
}
Last updated