EM Algorithm for 1D Gaussian Mixture Model
Xem dạng PDFProblem Statement
Implement 5 iterations of the EM algorithm for a 1D two-component Gaussian Mixture Model (GMM) and return the final parameters.
E-step — compute responsibilities:
R[i,k] = π_k * N(x_i; μ_k, σ²_k) / Σ_j π_j * N(x_i; μ_j, σ²_j)
Use 1e-10 to avoid division by zero in the denominator.
M-step — update parameters:
N_k = Σ_i R[i,k]
μ_k = Σ_i R[i,k] * x_i / N_k
σ²_k = Σ_i R[i,k] * (x_i - μ_k)² / N_k
π_k = N_k / n
Add 1e-10 to N_k to prevent division by zero.
Function signature:
def gmm_em_5steps(X: np.ndarray, mu0: np.ndarray, var0: np.ndarray, pi0: np.ndarray) -> tuple:
# returns (means, vars, weights) after 5 EM iterations
Input Format
Line 1: n — number of data points
Line 2: n space-separated floats — data values
Line 3: mu_0 mu_1 — initial means
Line 4: var_0 var_1 — initial variances
Line 5: pi_0 pi_1 — initial mixture weights
Output Format
Three lines (8 decimal places each):
- Final means
- Final variances
- Final mixture weights
Example
Input:
6
1.0 1.5 2.0 8.0 8.5 9.0
1.5 8.5
1.0 1.0
0.5 0.5
Output:
1.50000000 8.50000000
0.16666667 0.16666667
0.50000000 0.50000000
Derivation
The EM algorithm maximizes the log-likelihood of a GMM by alternating between:
- E-step: compute the posterior probability (responsibility) that each data point belongs to each component.
- M-step: update means, variances, and weights as weighted averages using responsibilities as weights.
This guarantees non-decreasing log-likelihood convergence. With well-separated data (clusters at ~1.5 and ~8.5), the algorithm converges quickly.
Notes
- Use a small
eps = 1e-10for numerical stability in both E and M steps. - 5 iterations is sufficient for well-separated clusters to converge.
Bình luận