TL;DR
A latent variable model says that the data we see were produced by something we do not see. Each observation comes from a hidden variable , the latent variable, plus some randomness. We cannot read off the data, so the model has to describe how and relate and then work backwards to guess from .
Where you'll see it: in the hidden code of a variational autoencoder, in the hidden states of a hidden Markov model, and in the unknown cluster label behind each point in a mixture of Gaussians.
Intuition
Suppose you record the height of every person in a room. You see a single list of numbers, and it looks messy: some heights are near 160 cm, others near 180 cm. Often the mess has a simple cause you did not record, such as the sex of each person. Sex is hidden. Heights are visible. Each height is produced by picking a hidden group, then drawing a height from that group's distribution.
That is a latent variable model. The hidden thing is a label or a small set of numbers that summarise the cause. The visible thing is the data. The model says how the hidden cause shapes the data, and it lets you ask two useful questions: what the hidden cause probably was for a given observation, and what the data look like overall.
Why bother with the hidden layer at all? Because it can make a complicated distribution simple. The overall shape of the heights, with two bumps, is hard to describe directly. Given the hidden group, each bump is just a plain bell curve. The hidden variable turns one hard problem into several easy ones.
flowchart LR Z[hidden cause z] -->|generates| X[visible data x] X -->|inference: what was z?| Z
The arrow from to is the generative story. The arrow back is inference, and it is the hard part.
The math
Let be the observed data and the latent variable, both random variables. A latent variable model specifies a joint distribution over both, usually as a prior over the hidden variable times a likelihood for the data given it:
Here is the prior, how likely each hidden value is before seeing data. The term is the likelihood, how likely the data are if the hidden value is . The symbol reads "given".
We never observe , so to get the distribution of the data alone we sum or integrate it out. This is the Law of Total Probability applied to the joint distribution. For a continuous hidden variable:
Each contributes a weighted share of the density of , weighted by how likely was. For a discrete the integral becomes a sum.
The reverse question, what the hidden cause probably was, is answered by Bayes' Rule:
This posterior is the central object of the whole subject. Its numerator is the joint distribution, and its denominator is the marginal from the previous equation, which is often the hard part to compute. When can take only a few values, the integral is a short sum. When it cannot, the denominator is intractable, and most of the modern methods (variational inference, sampling) exist to approximate it.
Mixture of Gaussians
The simplest useful example has a discrete latent variable and Gaussian likelihoods. With weights , means and standard deviation ,
Here is the Gaussian density with mean and variance . Each component is one hidden group, and says which group a point came from. The plot shows a two-component mixture: the sum has two bumps, while each piece is a plain bell.
- mixture p(x)
- z = 1 part
- z = 2 part
Two things to check
The latent variable does not have to be unique or meaningful. Two different hidden structures can give the same , so the model's hidden variables may be labelled differently from the real causes. This is called identifiability, and it is a real limit on interpreting .
Also, the hidden variable is a modelling choice. Pick it too small and the model cannot fit the data. Pick it too large and it fits noise. Training likelihood alone always rewards more components, so use held-out likelihood or a penalised criterion such as BIC to choose the size.
Worked examples
You have two coins. Coin A lands heads with probability and coin B with probability . You pick one at random (each with probability ), flip it, and see heads. The hidden variable is , the coin you picked. The observation is heads.
- Prior: .
- Likelihood: and .
- Marginal, by the law of total probability: .
- Posterior, by Bayes' rule: .
Interpretation: before the flip, you were 50% sure of each coin. After seeing heads, you are about 82% sure it was coin A. The observation moved your belief about the hidden variable, and the posterior is exactly that update.
Use the mixture above: weights and , means and , and . Observe .
Useful values: is the standard normal density, so and .
- Likelihood under component 1: .
- Likelihood under component 2: .
- Marginal: .
- Posterior for component 1: .
Interpretation: the point lies closer to the first component's centre at than to the second at . Component 1 has the smaller prior weight (0.3 against 0.7), but its likelihood at is about 12 times larger, which more than makes up for it. The posterior is about 84% to 16% in favour of component 1. Repeat the calculation at and it drops to about 3% for component 1, since is now much nearer the second centre. The prior shifts the balance, but where the observation sits matters more.
Tradeoffs and failure modes
Latent variable models are flexible, but the flexibility has costs. The main ones come from the posterior: once it is hard to compute, everything else gets harder.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Few discrete hidden states, such as a small mixture | Exact posterior is cheap, so there is little to worry about | Use exact inference and [[maximum-likelihood-estimation |
| Continuous hidden variable with a nonlinear link to the data | The integral for has no closed form, so the posterior is intractable | Variational inference, see Variational Lower Bound (ELBO), or sampling methods such as Monte Carlo Method |
| Too many hidden components | The model fits noise and generalises badly | Held-out likelihood, or a prior that switches off unused components |
| Hidden states are only identified up to relabelling | Interpreting a single as a real cause can mislead | Report results that do not depend on the labels, such as predicted densities |
| Gradient-based training through sampled | Sampling blocks gradients from reaching the encoder | Reparameterization Trick, which moves the randomness out of the sample |
| Likelihood is multimodal | The optimiser can converge to a poor local solution | Several random starts, or better initialisation such as k-means |
The core rule is simple. Adding a hidden variable buys you a simpler conditional model, and the price is that you must infer the hidden variable and marginalise it, and both steps can be expensive.
History
The idea of explaining observed variety through hidden structure is old. In 1894 Karl Pearson fitted a mixture of two normal distributions to a set of crab measurements, using the method of moments. It was one of the first mixture models ever fitted. Charles Spearman's factor analysis in the early 1900s, where many test scores are explained by a few hidden factors, is another early latent variable model.
Mixtures were hard to fit by hand until the expectation-maximisation algorithm, described by Arthur Dempster, Nan Laird and Donald Rubin in 1977. EM alternates between computing the posterior over the hidden variables and updating the parameters, and it gave the field a general method. Hidden Markov models, which use a hidden chain of states, were developed in the late 1960s and 1970s and became standard in speech recognition.
Latent variable models came back into the spotlight in machine learning with the variational autoencoder, proposed by Diederik Kingma and Max Welling in 2013. They used a neural network to approximate the posterior, which made latent variable models practical for high-dimensional data such as images. Much of modern generative modelling is built on this idea.
Check yourself
QuestionRecall: in the notation , which factor is the prior and which is the likelihood?Show answer
is the prior, how likely each hidden value is before the data arrive. is the likelihood, how probable the observed data are for a given hidden value.
QuestionComputation: a latent variable takes values 1 and 2 with probabilities and . Given , has likelihood . Given , it has likelihood . Compute for the observed .Show answer
By the law of total probability, .
QuestionWhy: why is the posterior harder to compute than the joint ?Show answer
The joint is just a product of two terms you specified. The posterior divides by , which requires summing or integrating the joint over every possible value of . For a continuous or high-dimensional that sum has no closed form and must be approximated.
QuestionApplied: you fit a mixture of three Gaussians to data that look like two clusters. The model fits well on training data but gives poor predictions on new points. What is a likely cause, and what would you check?Show answer
The model has more hidden components than the data support, so it is modelling noise in the training set. Check held-out likelihood as you vary the number of components, and prefer the smallest number that predicts new data well. A prior that shrinks unused component weights toward zero is another option.
QuestionApplied: in a variational autoencoder, the encoder outputs a mean and variance and you sample from that Gaussian. Why can't gradients flow through the sample directly, and what fix is used?Show answer
Sampling is a random operation, so the output is not a smooth function of the encoder's parameters, and the gradient cannot be propagated through the draw. The reparameterization trick writes with drawn from a fixed standard normal. The randomness now sits in , which does not depend on the parameters, so the path from and to the loss is differentiable.
Sources
- Bishop, Pattern Recognition and Machine Learning, ch. 9
- Latent variable (Wikipedia)
- Kingma and Welling, Auto-Encoding Variational Bayes (2013)