Maximum-likelihood EM for a Gaussian mixture can drive a component's covariance to a singular matrix. Implement MAP-EM with a Dirichlet prior on the weights and a conjugate inverse-Wishart-type prior on the covariances (Murphy, Probabilistic Machine Learning).
em_gmm_map(X, K, means0, n_iter, alpha=1.0, nu0=None, S0=None) with data X of shape (N,D) and initial means means0 of shape (K,D). Let sd2=N1∑n(xnd−xˉd)2 be the pooled per-dimension variances.
- Defaults: ν0=D+2 and S0=K2/D1diag(s12,…,sD2).
- Initialization: weights πk=1/K, means
means0, every covariance diag(s2).
- Each iteration: E step responsibilities rnk∝πkN(xn∣μk,Σk) (computed in log space). M step with rk=∑nrnk:
μk=rk∑nrnkxn,Σk=ν0+rk+D+2S0+∑nrnk(xn−μk)(xn−μk)⊤,πk=N+Kα−Krk+α−1
(the new μk is used in the scatter; assume α≥1).
- After each M step record the MAP objective (up to a constant)
J=∑nlog∑kπkN(xn∣μk,Σk)+(α−1)∑klogπk−∑k[2ν0+D+2log∣Σk∣+21tr(S0Σk−1)]
Return (weights, means, covs, history): arrays of shapes (K,), (K,D), (K,D,D) and a list of n_iter floats.