Search

Search papers and lessons

Mixture Model

Level 2 · 10 min read

A mixture model describes data as coming from one of several hidden groups, each with its own distribution, so one bell curve no longer has to fit a lumpy dataset.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Learning basics
  • Discrete math
  • Inference
  • Sign in to see your progress

TL;DR

A mixture model says that each data point came from one of several hidden groups, and each group has its own distribution. The overall density is a weighted sum of the group densities. This fits data that looks like several bumps, where a single Gaussian would sit in the wrong place and be too wide.

Where you'll see it: clustering with soft assignments, density estimation for multimodal data such as speaker voices or heights of mixed populations, and the output layer of mixture density networks that predict several possible answers at once.

Intuition

Two groups in one pile

Measure the heights of a group of adults and suppose the sample includes both men and women. The histogram has two humps, one near the women's typical height and one near the men's. A single bell curve fitted to that pile would sit in the middle, where there are few people, and it would be too wide to describe either hump. It answers the question "what is a typical height?" badly.

A mixture model asks a different question. It supposes that each person belongs to a hidden group, which we cannot see in the data. Given the group, the height is roughly Gaussian with that group's mean and spread. The group is a latent variable: a quantity that shapes the data but is never measured. The overall distribution of heights is what you get when you average over the unseen groups, weighted by how common each group is.

That is the whole idea. Pick KK groups, give each one a bell curve, and give each one a weight that says how common it is. The data then decides the rest.

flowchart TD
    Z[Hidden group z] -->|with probability pi_k| X[Observed value x]
    Z --> C1[Component 1: N mu1 sigma1]
    Z --> C2[Component 2: N mu2 sigma2]
    C1 --> X
    C2 --> X

The diagram reads from top to bottom. A group is drawn first, then a value is drawn from that group's distribution. The model is only ever shown the value at the bottom.

The math

A mixture of KK components has density

p(x)=∑k=1Kπk pk(x∣θk)p(x) = \sum_{k=1}^{K} \pi_k \, p_k(x \mid \theta_k)

where πk≥0\pi_k \ge 0 are the mixing weights, with ∑kπk=1\sum_{k} \pi_k = 1, and pkp_k is the density of component kk with its own parameters θk\theta_k. In the Gaussian mixture, which is the common case, θk=(μk,σk2)\theta_k = (\mu_k, \sigma_k^2) and pkp_k is the normal density

pk(x)=12πσk2exp⁡(−(x−μk)22σk2)p_k(x) = \frac{1}{\sqrt{2\pi \sigma_k^2}} \exp\left(-\frac{(x - \mu_k)^2}{2\sigma_k^2}\right)

The weights πk\pi_k are the probability that a random point comes from group kk. The latent variable z∈{1,…,K}z \in \{1, \dots, K\} is the group label, with P(z=k)=πkP(z = k) = \pi_k. Then p(x)=∑kP(z=k) p(x∣z=k)p(x) = \sum_k P(z = k) \, p(x \mid z = k), which is the law of total probability applied to the hidden label. This is the sense in which a mixture is a latent variable model.

Responsibilities

Given an observed xx, the posterior probability that it came from component kk is a Bayes rule calculation:

γk(x)=P(z=k∣x)=πk pk(x)∑j=1Kπj pj(x)\gamma_k(x) = P(z = k \mid x) = \frac{\pi_k \, p_k(x)}{\sum_{j=1}^{K} \pi_j \, p_j(x)}

These numbers are called responsibilities. They sum to 1 for each xx. A point near the centre of one bump has responsibility close to 1 for that bump and close to 0 for the others. A point between the bumps is shared.

Fitting by maximum likelihood

Given data x1,…,xnx_1, \dots, x_n assumed independent, the log-likelihood is

ℓ(π,μ,σ)=∑i=1nlog⁡∑k=1Kπk pk(xi)\ell(\pi, \mu, \sigma) = \sum_{i=1}^{n} \log \sum_{k=1}^{K} \pi_k \, p_k(x_i)

The sum sits inside the logarithm, so there is no closed-form maximiser. Setting the derivatives to zero gives equations that involve the responsibilities, which in turn depend on the parameters. The usual way out is the expectation-maximisation (EM) algorithm, which alternates between computing responsibilities and re-estimating the parameters. For the Gaussian case, the re-estimates are weighted versions of the familiar formulas:

πk←1n∑i=1nγk(xi),μk←∑iγk(xi) xi∑iγk(xi)\pi_k \leftarrow \frac{1}{n}\sum_{i=1}^{n} \gamma_k(x_i), \qquad \mu_k \leftarrow \frac{\sum_i \gamma_k(x_i)\, x_i}{\sum_i \gamma_k(x_i)}

with a similar weighted average for σk2\sigma_k^2. Each EM step does not decrease the likelihood, but EM can reach a local maximum, so the result depends on the starting values.

Worked examples

Example
Responsibilities for one point

Take two components, N(0,1)N(0, 1) with weight 0.40.4 and N(4,1)N(4, 1) with weight 0.60.6. Consider the point x=1x = 1.

  1. Component 1 density: ϕ(1−0)=ϕ(1)=0.24197\phi(1 - 0) = \phi(1) = 0.24197, where ϕ\phi is the standard normal density.
  2. Component 2 density: ϕ(1−4)=ϕ(−3)=ϕ(3)=0.004432\phi(1 - 4) = \phi(-3) = \phi(3) = 0.004432.
  3. Weighted terms: 0.4×0.24197=0.0967880.4 \times 0.24197 = 0.096788 and 0.6×0.004432=0.0026590.6 \times 0.004432 = 0.002659.
  4. Mixture density: p(1)=0.096788+0.002659=0.099447p(1) = 0.096788 + 0.002659 = 0.099447.
  5. Responsibility of component 1: 0.096788/0.099447≈0.9730.096788 / 0.099447 \approx 0.973.
  6. Responsibility of component 2: 0.002659/0.099447≈0.0270.002659 / 0.099447 \approx 0.027. The two add to 1.

The point is assigned mostly to component 1, even though component 2 has the larger weight. The mean of component 2 is far away, so its density at x=1x = 1 is tiny. Weights matter less than location once a point is far from a component.

Example
One M-step on four points (illustrative responsibilities)

Use the data x=0.5,1.0,3.5,4.5x = 0.5, 1.0, 3.5, 4.5. Suppose the E-step gave component 2 these responsibilities: 0.02,0.10,0.90,0.980.02, 0.10, 0.90, 0.98. These numbers are chosen for illustration.

  1. Weight for component 2: the mean of the responsibilities is (0.02+0.10+0.90+0.98)/4=2.00/4=0.50(0.02 + 0.10 + 0.90 + 0.98) / 4 = 2.00 / 4 = 0.50. So π2=0.5\pi_2 = 0.5.
  2. Sum of responsibilities: ∑iγ2(xi)=2.00\sum_i \gamma_2(x_i) = 2.00.
  3. Weighted sum of data: 0.02×0.5+0.10×1.0+0.90×3.5+0.98×4.5=0.01+0.10+3.15+4.41=7.670.02 \times 0.5 + 0.10 \times 1.0 + 0.90 \times 3.5 + 0.98 \times 4.5 = 0.01 + 0.10 + 3.15 + 4.41 = 7.67.
  4. Mean: μ2=7.67/2.00=3.835\mu_2 = 7.67 / 2.00 = 3.835.

The new mean of component 2 is 3.8353.835, pulled toward the two large points on the right. The points at 0.5 and 1.0 barely affect it because their responsibilities are small. One EM iteration repeats this for component 1 with the complementary responsibilities, then recomputes the responsibilities from the new parameters.

Example
Why one Gaussian is the wrong model (illustrative)

Take two equal groups of people, with heights centred at 160 cm and 180 cm, each with standard deviation 5 cm. Their mixture has equal weights 0.50.5.

  1. Mixed mean: (160+180)/2=170(160 + 180) / 2 = 170 cm.
  2. Mixed variance: within-group variance 2525 plus the between-group spread 102=10010^2 = 100 gives 125125. So the standard deviation is about 11.1811.18 cm.
  3. Density of the single fitted Gaussian at 170 cm: 1/(11.182π)≈0.03571 / (11.18 \sqrt{2\pi}) \approx 0.0357.
  4. True mixture density at 170 cm: each component is 10 cm from the centre, which is 2 standard deviations, with density ϕ(2)/5=0.05399/5=0.0108\phi(2) / 5 = 0.05399 / 5 = 0.0108. Averaging the two equal-weight components gives 0.01080.0108.
  5. The single Gaussian overstates the density at the centre by a factor of about 0.0357/0.0108≈3.30.0357 / 0.0108 \approx 3.3.

The plot below shows both curves. The two-hump density is what the data looks like, and the single curve is what a model with one group would predict.

15016017018019000.020.040.060.08height (cm)density
  • two-group mixture
  • single Gaussian
Both curves have the same mean and total area. The single Gaussian misses the dip at 170 cm, where the mixture has its lowest density between the two groups.

Tradeoffs and failure modes

SituationProblemBetter choice
The number of groups KK is unknownA wrong KK either merges real groups or splits one group in twoCompare fits with an information criterion such as BIC, or use a Bayesian nonparametric model
Groups are not roughly GaussianGaussian components fit the wrong shapeUse a different family per component, or a flexible density estimator
Data has outliersA narrow component can latch onto a single point and shrink its variance toward zeroPut a floor on variances, or use a robust component such as a Student-t
You only need hard cluster labelsResponsibilities are more than the task needsA hard method such as K-Means Clustering, which is a limiting case with equal spherical components
Starting values are poorEM converges to a poor local maximumRun several random starts and keep the highest likelihood
Components collapse onto the same dataTwo components become identical and the model wastes capacityInitialise means with k-means, and check for near-identical components

Failure modes to watch: label switching, where the fitted components are the same but their labels are permuted between runs, so comparing components across fits needs matching; and degenerate solutions, where one component explains a single point with a very small variance and the likelihood rises without limit. The second is why practical implementations regularise the variances.

History

The idea of fitting a mixture of normal distributions goes back to the 1890s. Karl Pearson fitted a mixture of two normal curves to a set of measurements on crabs, showing that a single curve could hide two populations. The method was used in biology and astronomy for decades, but estimating the parameters was hard without computers.

The modern algorithm came later. In 1977 Dempster, Laird and Rubin described the expectation-maximisation framework, which gave a general recipe for maximum likelihood with hidden variables. Mixtures of Gaussians became a standard application: they were easy to write down, EM gave a reasonable fit, and they could be used for clustering, density estimation and as building blocks in speech recognition systems from the 1980s and 1990s.

Check yourself

QuestionRecall: what is a mixing weight, and what must the weights of a mixture add up to?Show answer

A mixing weight πk\pi_k is the probability that a random observation comes from component kk, so it is a fraction of the data expected from that group. The weights are non-negative and sum to 1, so that the mixture is a proper probability density.

QuestionComputation: a mixture has weights 0.30.3 and 0.70.7. At a point xx, component 1 has density 0.20.2 and component 2 has density 0.50.5. What is the responsibility of component 1 at xx, to three decimal places?Show answer

The mixture density is 0.3×0.2+0.7×0.5=0.06+0.35=0.410.3 \times 0.2 + 0.7 \times 0.5 = 0.06 + 0.35 = 0.41. The responsibility is 0.06/0.41=0.1460.06 / 0.41 = 0.146.

QuestionWhy can't the log-likelihood of a mixture be maximised by setting its derivative to zero and solving directly?Show answer

The log of a sum has no simple form. The derivative with respect to a component's mean or variance involves the responsibilities, which themselves depend on all the parameters. The equations are coupled and have no closed-form solution, which is why iterative methods such as EM are used.

QuestionWhy does a mixture model with one component very narrow around a single data point give an unbounded likelihood, and what does that imply for fitting?Show answer

As the variance of that component goes to zero, its density at the matching point goes to infinity, so the likelihood can grow without bound. The maximum likelihood estimate is then degenerate. In practice, the variances are bounded below, or the fit uses a prior, so that a single point cannot dominate the model.

QuestionApplied: you have the heights of 2,000 people, and the histogram shows two humps with an unknown number of groups. How would you choose the model, and what would you check before believing it?Show answer

Fit mixtures with two, three and more components, each by EM from several random starts, and compare them with BIC or held-out likelihood. Choose the number of components that improves the fit enough to justify the extra parameters. Before believing the result, check that the components are not nearly identical, that the variances are not collapsing, and that the fitted groups have a plausible meaning in the data, such as a known split by sex or age. Plot the fitted density over the histogram as a final check.

Sources