# Data jumlah Head (H) dan Tail (T)
H <- c(5, 9, 8, 4, 7)
T <- c(5, 1, 2, 6, 3)
# Parameter awal
theta_A <- 0.7
theta_B <- 0.6
# Maksimum iterasi
max_iter <- 20
# Toleransi konvergensi
tol <- 0.0001
# Menyimpan hasil iterasi
hasil_iterasi <- data.frame()
for(iter in 1:max_iter){
# =========================
# E-STEP
# =========================
# Probabilitas data muncul dari koin A
prob_A <- (theta_A^H) * ((1-theta_A)^T)
# Probabilitas data muncul dari koin B
prob_B <- (theta_B^H) * ((1-theta_B)^T)
# Rasio probabilitas
ZA <- prob_A / (prob_A + prob_B)
ZB <- prob_B / (prob_A + prob_B)
# Estimasi jumlah Head dan Tail
exp_H_A <- ZA * H
exp_T_A <- ZA * T
exp_H_B <- ZB * H
exp_T_B <- ZB * T
# =========================
# M-STEP
# =========================
theta_A_new <- sum(exp_H_A) /
(sum(exp_H_A) + sum(exp_T_A))
theta_B_new <- sum(exp_H_B) /
(sum(exp_H_B) + sum(exp_T_B))
# Hitung perubahan parameter
perubahan <- sqrt((theta_A_new - theta_A)^2 +
(theta_B_new - theta_B)^2)
# Simpan hasil iterasi
hasil_iterasi <- rbind(
hasil_iterasi,
data.frame(
Iterasi = iter,
Theta_A = theta_A_new,
Theta_B = theta_B_new,
Perubahan = perubahan
)
)
# Tampilkan hasil iterasi
cat("\n=========================\n")
cat("Iterasi :", iter, "\n")
cat("=========================\n")
print(data.frame(
Set = 1:5,
H = H,
T = T,
P_E_ZA = round(prob_A, 5),
P_E_ZB = round(prob_B, 5),
P_ZA_E = round(ZA, 5),
P_ZB_E = round(ZB, 5)
))
cat("\nTheta A baru =", round(theta_A_new, 5), "\n")
cat("Theta B baru =", round(theta_B_new, 5), "\n")
cat("Perubahan =", round(perubahan, 6), "\n")
# Cek konvergensi
if(perubahan < tol){
cat("\nKonvergen pada iterasi ke-", iter, "\n")
break
}
# Update parameter
theta_A <- theta_A_new
theta_B <- theta_B_new
}
##
## =========================
## Iterasi : 1
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00041 0.00080 0.33902 0.66098
## 2 2 9 1 0.01211 0.00403 0.75020 0.24980
## 3 3 8 2 0.00519 0.00269 0.65878 0.34122
## 4 4 4 6 0.00018 0.00053 0.24797 0.75203
## 5 5 7 3 0.00222 0.00179 0.55379 0.44621
##
## Theta A baru = 0.72891
## Theta B baru = 0.58829
## Perubahan = 0.031196
##
## =========================
## Iterasi : 2
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00030 0.00083 0.26547 0.73453
## 2 2 9 1 0.01575 0.00347 0.81923 0.18077
## 3 3 8 2 0.00586 0.00243 0.70660 0.29340
## 4 4 4 6 0.00011 0.00058 0.16111 0.83889
## 5 5 7 3 0.00218 0.00170 0.56137 0.43863
##
## Theta A baru = 0.75294
## Theta B baru = 0.56603
## Perubahan = 0.032752
##
## =========================
## Iterasi : 3
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00022 0.00089 0.19940 0.80060
## 2 2 9 1 0.01922 0.00259 0.88129 0.11871
## 3 3 8 2 0.00631 0.00198 0.76061 0.23939
## 4 4 4 6 0.00007 0.00069 0.09633 0.90367
## 5 5 7 3 0.00207 0.00152 0.57624 0.42376
##
## Theta A baru = 0.77301
## Theta B baru = 0.54573
## Perubahan = 0.028548
##
## =========================
## Iterasi : 4
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00017 0.00094 0.15083 0.84917
## 2 2 9 1 0.02237 0.00195 0.91981 0.08019
## 3 3 8 2 0.00657 0.00162 0.80183 0.19817
## 4 4 4 6 0.00005 0.00078 0.05896 0.94104
## 5 5 7 3 0.00193 0.00135 0.58803 0.41197
##
## Theta A baru = 0.78585
## Theta B baru = 0.53218
## Perubahan = 0.018666
##
## =========================
## Iterasi : 5
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00013 0.00096 0.12367 0.87633
## 2 2 9 1 0.02448 0.00160 0.93858 0.06142
## 3 3 8 2 0.00667 0.00141 0.82570 0.17430
## 4 4 4 6 0.00004 0.00084 0.04192 0.95808
## 5 5 7 3 0.00182 0.00124 0.59491 0.40509
##
## Theta A baru = 0.79228
## Theta B baru = 0.52507
## Perubahan = 0.009579
##
## =========================
## Iterasi : 6
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00012 0.00096 0.11126 0.88874
## 2 2 9 1 0.02555 0.00144 0.94662 0.05338
## 3 3 8 2 0.00670 0.00130 0.83713 0.16287
## 4 4 4 6 0.00003 0.00087 0.03502 0.96498
## 5 5 7 3 0.00176 0.00118 0.59839 0.40161
##
## Theta A baru = 0.79503
## Theta B baru = 0.52186
## Perubahan = 0.004231
##
## =========================
## Iterasi : 7
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00011 0.00097 0.10619 0.89381
## 2 2 9 1 0.02601 0.00137 0.94987 0.05013
## 3 3 8 2 0.00671 0.00126 0.84207 0.15793
## 4 4 4 6 0.00003 0.00089 0.03235 0.96765
## 5 5 7 3 0.00173 0.00115 0.60006 0.39994
##
## Theta A baru = 0.79612
## Theta B baru = 0.52051
## Perubahan = 0.001737
##
## =========================
## Iterasi : 8
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00011 0.00097 0.10421 0.89579
## 2 2 9 1 0.02619 0.00134 0.95117 0.04883
## 3 3 8 2 0.00671 0.00124 0.84411 0.15589
## 4 4 4 6 0.00003 0.00089 0.03133 0.96867
## 5 5 7 3 0.00172 0.00114 0.60085 0.39915
##
## Theta A baru = 0.79654
## Theta B baru = 0.51996
## Perubahan = 0.000693
##
## =========================
## Iterasi : 9
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00011 0.00097 0.10346 0.89654
## 2 2 9 1 0.02626 0.00133 0.95168 0.04832
## 3 3 8 2 0.00671 0.00123 0.84494 0.15506
## 4 4 4 6 0.00003 0.00089 0.03094 0.96906
## 5 5 7 3 0.00171 0.00114 0.60121 0.39879
##
## Theta A baru = 0.7967
## Theta B baru = 0.51973
## Perubahan = 0.000273
##
## =========================
## Iterasi : 10
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00011 0.00097 0.10318 0.89682
## 2 2 9 1 0.02629 0.00133 0.95188 0.04812
## 3 3 8 2 0.00671 0.00123 0.84527 0.15473
## 4 4 4 6 0.00003 0.00090 0.03079 0.96921
## 5 5 7 3 0.00171 0.00113 0.60137 0.39863
##
## Theta A baru = 0.79675
## Theta B baru = 0.51964
## Perubahan = 0.000108
##
## =========================
## Iterasi : 11
## =========================
## Set H T P_E_ZA P_E_ZB P_ZA_E P_ZB_E
## 1 1 5 5 0.00011 0.00097 0.10307 0.89693
## 2 2 9 1 0.02630 0.00133 0.95196 0.04804
## 3 3 8 2 0.00671 0.00123 0.84540 0.15460
## 4 4 4 6 0.00003 0.00090 0.03074 0.96926
## 5 5 7 3 0.00171 0.00113 0.60144 0.39856
##
## Theta A baru = 0.79678
## Theta B baru = 0.51961
## Perubahan = 4.2e-05
##
## Konvergen pada iterasi ke- 11
# ==========================================
# HASIL AKHIR
# ==========================================
cat("\n=========================\n")
##
## =========================
cat("HASIL AKHIR EM\n")
## HASIL AKHIR EM
cat("=========================\n")
## =========================
print(hasil_iterasi)
## Iterasi Theta_A Theta_B Perubahan
## 1 1 0.7289138 0.5882874 0.0311960618
## 2 2 0.7529401 0.5660293 0.0327518720
## 3 3 0.7730112 0.5457281 0.0285480189
## 4 4 0.7858479 0.5321772 0.0186656443
## 5 5 0.7922760 0.5250747 0.0095794671
## 6 6 0.7950309 0.5218638 0.0042307184
## 7 7 0.7961219 0.5205118 0.0017373714
## 8 8 0.7965389 0.5199586 0.0006927209
## 9 9 0.7966958 0.5197347 0.0002734529
## 10 10 0.7967544 0.5196443 0.0001077268
## 11 11 0.7967762 0.5196078 0.0000424844
cat("\nEstimasi akhir:\n")
##
## Estimasi akhir:
cat("Theta A =", round(theta_A, 5), "\n")
## Theta A = 0.79675
cat("Theta B =", round(theta_B, 5), "\n")
## Theta B = 0.51964
Nilai akhir parameter menunjukkan bahwa: Koin A θA = 0.79675 Artinya, koin A memiliki probabilitas muncul sisi Head sebesar sekitar 79.7%. Dengan demikian, koin A dapat dikatakan sebagai koin yang cenderung menghasilkan Head. Koin B θB = 0.51964 Artinya, koin B memiliki probabilitas muncul sisi Head sebesar sekitar 52.0%. Nilai ini mendekati 0.5 sehingga koin B cenderung lebih seimbang atau mendekati koin adil.