Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Probabilistic models

One EM step for a Gaussian mixture

Medium · Target 20 minutes · Python and NumPy

A 1-D Gaussian mixture has weights pi, means mu and variances var (each of length K). Given data x, perform one EM iteration and return [new_pi, new_mu, new_var, loglik]:

  • E-step: rik = πkN(xi; μk, σ²k) / Σj πjN(xi; μj, σ²j).
  • M-step: Nk = Σi rik; new πk = Nk/n; new μk = Σi rikxi/Nk; new σ²k = Σi rik(xi − new μk)²/Nk.
  • loglik: Σi log Σk πkN(xi; μk, σ²k), under the parameters you were given.

Examples

Input x = [-1, 1], pi = [0.5, 0.5], mu = [0, 0], var = [1, 1]

Output [[0.5, 0.5], [0, 0], [1, 1], -2.8379]

Two identical components: each takes half of every point, and the variance becomes the data's.

Input x = [0, 0.2, 10, 10.4], pi = [0.5, 0.5], mu = [0, 10], var = [1, 1]

Output [[0.5, 0.5], [0.1, 10.2], [0.01, 0.04], -6.5483]

Well separated: each component takes its own cluster.

Input x = [0, 1, 2, 9, 10, 11], pi = [0.3, 0.7], mu = [1, 9], var = [2, 2]

Output [[0.5, 0.5], [1, 10], [0.6667, 0.667], -14.025]

The weights move towards the true half-and-half split.

Constraints

  • 1 ≤ n ≤ 10⁵, 1 ≤ K ≤ 10
  • All weights and variances positive; no component receives zero total responsibility
  • Checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Build the (n, K) matrix of log π_k + log N(x_i; μ_k, σ²_k). Broadcasting x[:, None] against mu does it.

Hint 2

Normalise each row with log-sum-exp: r = exp(logp − lse). The sum over rows of lse is the log-likelihood.

Hint 3

The M-step is three weighted averages, with the columns of r as weights.

Where candidates go wrong

  • Computing the densities directly and dividing: a point far from every component gives 0/0.
  • Using the old means in the variance update. The new variance is around the new mean.
  • Reporting the log-likelihood after the update rather than under the parameters given.
  • Dividing by n instead of N_k in the mean and variance.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

EM is the standard example of latent-variable fitting, and research interviewers use it to see whether a candidate can derive an update rule, not only apply one. Writing a single step cleanly, with the responsibilities in log space, is the core of it.

The theory behind it is in the lesson Naive Bayes, EM, Viterbi and Markov chains from scratch.


Keep going