The interview problem
Fit a two-component Gaussian mixture to unlabeled one-dimensional observations. Derive the E-step and M-step, compute one iteration for observations 0 and 2, and explain exactly what likelihood monotonicity guarantees. Diagnose identical initialization, collapsing variance and disagreement between training likelihood and useful clustering.
Original ML fundamentals practice, not a reported company interview. Numerical examples and derivations are original. Primary reference retrieved September 27, 2026; publication date not stated; interview date not applicable.
1. State the latent-variable model
For independent observations xi, let hidden component zi take values 1 through K with probabilities πk, where πk≥0 and Σkπk=1. Given zi=k, assume xi follows a Gaussian with mean μk and variance vk>0. The observed-data likelihood is a product of mixtures, not a mixture of products: p(X|θ)=Πi Σk πk N(xi;μk,vk).
The log likelihood is ℓ(θ)=Σi log Σk πk N(xi;μk,vk). The logarithm outside the component sum makes direct optimization less convenient. If the hidden assignments were known, the complete-data log likelihood would instead be a sum of component-specific terms. EM alternates posterior inference about those assignments with parameter fitting under the resulting soft weights.
A component is a statistical distribution, not automatically a meaningful real-world class. The number K is assumed fixed during this derivation. Selecting it is a separate model-selection problem.
2. Derive the E-step with Bayes' rule
At the current parameters θold, responsibility rik=P(zi=k|xi,θold)=πk N(xi;μk,vk)/Σj πj N(xi;μj,vj). Responsibilities sum to one across components for each observation. They are not the probability of the parameters themselves and are not arbitrary similarity scores.
The expected complete-data log likelihood is Q(θ|θold)=Σi,k rik[log πk−0.5 log(2πvk)−(xi−μk)²/(2vk)]. During the M-step, responsibilities are held fixed at the values computed using θold. Updating a responsibility halfway through the parameter update changes the algorithm.
Use log component weights ak=log πk+log N(xi;μk,vk) and normalize with log-sum-exp. Exponentiating tiny densities first can underflow to zero, making a well-defined posterior appear to divide zero by zero.
3. Derive weighted maximum-likelihood updates
Let Nk=Σi rik be the effective component count. Maximizing the weight part of Q subject to Σkπk=1 gives πknew=Nk/N. Differentiating the mean part gives μknew=Σi rik xi/Nk. Updating variance gives vknew=Σi rik(xi−μknew)²/Nk.
Use the new mean in the variance expression. The denominator is Nk, not Nk−1: this is a weighted maximum-likelihood update, not an unbiased sample-variance estimator. These formulas assume Nk>0 and an unconstrained positive variance solution. Empty or degenerate components need a defined policy rather than division by zero. An exactly zero mixture weight gives zero responsibilities under ordinary EM and does not revive on its own. Reinitializing a component is an extra intervention; check its effect rather than attributing an automatic monotonicity guarantee to it.
In multiple dimensions, replace the variance with the weighted outer-product covariance. Diagonal, tied or spherical covariance restrictions change the optimization problem and its updates. Derive or use the appropriate constrained update rather than taking the full-covariance formula and modifying it without explanation.
4. Compute one complete iteration
Use x1=0 and x2=2, with π1=π2=0.5, μ1=0, μ2=2 and v1=v2=1. For x=0, the component density ratio is exp(2), so its responsibility for component 1 is 1/(1+exp(−2))≈0.880797. For x=2, the responsibility is approximately 0.119203. Responsibilities for component 2 are the complements.
Thus N1=N2=1 and the weights remain 0.5. The new first mean is 0.880797×0+0.119203×2≈0.238406. By symmetry the second mean is 1.761594.
The first variance becomes 0.880797×(0−0.238406)²+0.119203×(2−0.238406)²≈0.419974. The second variance is identical. The centers move toward each other during this iteration because soft membership assigns some weight to the opposite point, while the fitted variances shrink.
Evaluating the actual observed log likelihood in natural-log units gives approximately −2.970315 before and −2.439441 after the update, an improvement of 0.530874. This checks the iteration numerically. It does not demonstrate that these two points provide enough information to fit a sensible unrestricted two-Gaussian model.
5. Explain monotonicity without promising a global optimum
Introduce a distribution q over hidden assignments. Define F(q,θ)=Eq[log p(X,Z|θ)]+H(q). The identity ℓ(θ)=F(q,θ)+KL(q||p(Z|X,θ)) gives a lower bound because KL is nonnegative, assuming the relevant quantities are well defined.
The exact E-step sets q to the posterior under θold, making the bound tight at the old parameters. An exact M-step maximizes F with q fixed. Therefore ℓ(θnew)≥F(q,θnew)≥F(q,θold)=ℓ(θold). Increasing Q without fully maximizing it also suffices for this inequality when the E-step is exact; this is a generalized EM update.
Nondecreasing likelihood does not imply a global optimum, convergence of all parameters, or even a finite optimum in an unconstrained Gaussian mixture. Approximate posterior inference or an arbitrary numerical update can break the argument. If optimizing a prior-regularized objective, identify that objective explicitly instead of claiming the same guarantee for unpenalized likelihood.
6. Diagnose symmetry and singularities
If both components start with identical means, variances and equal weights, every responsibility is 0.5. Their weighted updates remain identical. EM has no automatic symmetry-breaking step. Multiple initializations or suitably distinct initial centers can help, but do not certify the global optimum.
For the two-point example, one component can center on 0 and the other on 2 while their variances approach zero. Each point's density can then grow without bound, so training likelihood can keep improving toward a singularity. The numerical iteration above is not evidence that unrestricted fitting will converge to a useful finite solution.
Possible remedies include a justified minimum variance, covariance constraints or priors. Each changes the model or objective. A library's additive covariance regularization is not automatically identical to the exact constrained M-step for every chosen constraint. Track the actual objective and parameter diagnostics.
Component labels are exchangeable: swapping the component names leaves the mixture density unchanged. Do not count two label-swapped fits as different predictive models. Compare densities or align components before interpreting parameter movement. Nearly empty components also need attention: tiny Nk makes estimates unstable even before it becomes exactly zero.
7. Implement a diagnosable training loop
choose K, covariance family and explicit objective
↓
initialize several candidate parameter sets
↓
E-step: stable posterior responsibilities
↓
M-step: weighted sufficient statistics
↓
check likelihood, component counts and covariance
↓
stop / continue / flag degeneracy
↓
evaluate held-out density and task usefulnessFor a small hand example, store responsibilities and verify each row sums to one, weights sum to one and variances remain valid. On larger data, aggregate sufficient statistics in chunks under one fixed θold before applying a batch M-step. Updating parameters after every chunk is an online variant and does not inherit the same per-iteration guarantee without further assumptions.
For one million points and 20 components, a float64 responsibility matrix contains 20 million values, about 160 MB in decimal units. Chunking avoids retaining that entire matrix. Accumulate weighted means and centered moments with numerical care; subtracting two large nearly equal raw moments can lose precision.
Check both likelihood change and iteration limits, and inspect degeneracy flags. A small gain may mean a flat region or slow convergence. A large gain caused by a collapsing covariance is a failure signal rather than a reason to celebrate. Fit transformations and candidate initializations on training data. Use validation data for choosing K, constraints and other hyperparameters, then reserve an untouched test set for final evaluation.
8. Separate fit quality from semantic quality
Compare held-out log density across candidate K and covariance choices with consistent preprocessing. A model can represent a non-Gaussian population using several Gaussian components, so “number of components” need not equal “number of real categories.” If clustering is the application, evaluate stability and domain-relevant outcomes as well.
EM differs from hard assignment: a point can contribute to several components. K-means can arise in a limiting setup with common spherical variance and hard assignment behavior, but ordinary Gaussian-mixture EM also learns density shape and mixture weights. Replacing responsibilities with argmax assignments is a different procedure.
Follow-ups: Why is the E-step not an expectation of the observed data? Why use Nk rather than Nk−1? What happens at zero mixture weight? Can likelihood improve while clustering gets worse? Which part of the lower-bound proof fails with an approximate E-step? Why do random restarts not solve singularity by themselves?
9. Self-review rubric
- Write the product of mixtures and identify hidden assignments.
- Derive normalized responsibilities from the current parameters.
- Hold responsibilities fixed during weighted M-step updates.
- Use the new mean and maximum-likelihood variance denominator.
- Compute one iteration and verify observed likelihood.
- Explain the tight-bound monotonicity argument and its assumptions.
- Distinguish local behavior, label symmetry and unbounded likelihood.
- Evaluate held-out fit and task usefulness separately.
Sources and related practice
scikit-learn: Gaussian mixture models, publication date not stated; retrieved September 27, 2026. Primary implementation reference for Gaussian-mixture EM, covariance choices, initialization and singularity concerns. Derivations and numerical examples here are original worked practice; the monotonicity explanation states its mathematical assumptions explicitly.
Continue with Jensen's inequality and KL divergence direction.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.