As a follow up to one answer of the topic Expectation-Maximization with a coin toss: One of the user posted an R-code with MLE example almost a year ago (and his last online time here was 3 months ago, so I doubt he answers my question regarding his code).
In maximization step he multiplies likelihood of an A coin (row$weight_A
) by a log-likelihood (llf_A
). I do not understand why it is allowed to multiply a likelihood with a log-likelihood. I mean why wouldn't we multiple a likelihood with a likelihood or log-likelihood with a log-likelihood? And in the code he mixes it.
require("stats4");
## sample data from Do and Batzoglou
ds<-data.frame(heads=c(5,9,8,4,7),n=c(10,10,10,10,10),
coin=c("B","A","A","B","A"),weight_A=1:5*0)
## "baby likelihood" for a single observation
llf <- function(heads, n, theta) {
comb <- function(n, x) { #nCr function
return(factorial(n) / (factorial(x) * factorial(n-x)))
}
if (theta<0 || theta >1) { # probabilities should be in [0,1]
return(-Inf);
}
z<-comb(n,heads)* theta^heads * (1-theta)^(n-heads);
return (log(z))
}
## the "E-M" likelihood function
em <- function(theta_A,theta_B) {
# expectation step: given current parameters, what is the likelihood
# an observation is the result of tossing coin A (vs coin B)?
ds$weight_A <<- by(ds, 1:nrow(ds), function(row) {
llf_A <- llf(row$heads,row$n, theta_A);
llf_B <- llf(row$heads,row$n, theta_B);
return(exp(llf_A)/(exp(llf_A)+exp(llf_B)));
})
# maximisation step: given params and weights, calculate likelihood of the sample
return(- sum(by(ds, 1:nrow(ds), function(row) {
llf_A <- llf(row$heads,row$n, theta_A);
llf_B <- llf(row$heads,row$n, theta_B);
return(row$weight_A*llf_A + (1-row$weight_A)*llf_B);
})))
}
est<-mle(em,start = list(theta_A=0.6,theta_B=0.5), nobs=NROW(ds))