# 1. Laten Variable
coin_data <- data.frame(
  H = c(9, 8, 7, 4, 5),
  T = c(1, 2, 3, 6, 5)
)

coin_data
##   H T
## 1 9 1
## 2 8 2
## 3 7 3
## 4 4 6
## 5 5 5
# 2. Set Parameter

theta_A <- 0.8
theta_B <- 0.3

# 3. E-Step

change <- 1

iter <- 1

tol <- 0.0001

history <- data.frame(
  Iteration = numeric(),
  Theta_A = numeric(),
  Theta_B = numeric(),
  Change = numeric()
)

while(change > tol){
  prob_A <- c()
  prob_B <- c()
  
  for(i in 1:nrow(coin_data)){
    
    H <- coin_data$H[i]
    T <- coin_data$T[i]
    
    like_A <- (theta_A^H) * ((1-theta_A)^T)
    like_B <- (theta_B^H) * ((1-theta_B)^T)
    
    weight_A <- like_A / (like_A + like_B)
    weight_B <- like_B / (like_A + like_B)
    
    prob_A[i] <- weight_A
    prob_B[i] <- weight_B
  }
  
# 4. M Step
  
  expected_H_A <- sum(prob_A * coin_data$H)
  expected_T_A <- sum(prob_A * coin_data$T)
  
  expected_H_B <- sum(prob_B * coin_data$H)
  expected_T_B <- sum(prob_B * coin_data$T)
  
  new_theta_A <- expected_H_A /
    (expected_H_A + expected_T_A)
  
  new_theta_B <- expected_H_B /
    (expected_H_B + expected_T_B)
  
# 5. Convergence Check
  
change <- sqrt(
    (new_theta_A - theta_A)^2 +
    (new_theta_B - theta_B)^2
  )

  history <- rbind(
    history,
    data.frame(
      Iteration = iter,
      Theta_A = new_theta_A,
      Theta_B = new_theta_B,
      Change = change
    )
  )

  theta_A <- new_theta_A
  theta_B <- new_theta_B

  iter <- iter + 1
}
  
# Result

final_result <- data.frame(
  Set = 1:nrow(coin_data),
  Head = coin_data$H,
  Tail = coin_data$T,
  Prob_Coin_A = round(prob_A, 4),
  Prob_Coin_B = round(prob_B, 4)
)

final_result$Prediction <- ifelse(
  final_result$Prob_Coin_A >
    final_result$Prob_Coin_B,
  "Coin A",
  "Coin B"
)

print(final_result)
##   Set Head Tail Prob_Coin_A Prob_Coin_B Prediction
## 1   1    9    1      0.9521      0.0479     Coin A
## 2   2    8    2      0.8457      0.1543     Coin A
## 3   3    7    3      0.6018      0.3982     Coin A
## 4   4    4    6      0.0307      0.9693     Coin B
## 5   5    5    5      0.1031      0.8969     Coin B
plot( history$Iteration,
      history$Theta_A, 
      type = "b", ylim = c(0,1), 
      xlab = "Iteration", 
      ylab = "Parameter Value", 
      main = "EM Parameter Convergence" ) 

lines( history$Iteration, 
       history$Theta_B, 
       type = "b", 
       col = "red" ) 

legend( "bottomright", 
        legend = c("Theta A", "Theta B"), 
        col = c("black", "red"), lty = 1 )

Grafik konvergensi menunjukkan bahwa parameter Theta A dan Theta B secara bertahap menuju nilai yang stabil setelah beberapa iterasi. Theta A konvergen pada nilai sekitar 0.79, sedangkan Theta B konvergen pada nilai sekitar 0.52. Hal ini menunjukkan bahwa algoritma Expectation-Maximization berhasil mengestimasi parameter probabilitas secara konsisten. Perubahan parameter yang semakin kecil pada iterasi akhir menandakan bahwa algoritma telah mencapai kondisi konvergen, sehingga iterasi tambahan tidak memberikan perubahan estimasi yang signifikan.