Search

Search papers and lessons

Likelihood Function

Level 2 · 10 min read

The likelihood function scores each possible parameter value by how probable the observed data would be if that value were true.

On this page

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

TL;DR

A model of some data has one or more unknown settings, called parameters. The likelihood function takes the data as fixed and asks, for each possible setting, how probable the observed data would be if that setting were true. The result is a curve or surface over the parameters, not over the data. Its highest point is the setting that best explains what was seen.

The likelihood is the bridge between a model and a dataset. It is the object that maximum likelihood maximises and that Bayesian updating multiplies by a prior.

Where you'll see it: in fitting a Gaussian to measurements, in estimating a coin's bias from flips, and in the loss function of nearly every probabilistic model trained on data.

Intuition

Same formula, different question

Start with a familiar question. Flip a coin ten times and get seven heads. A probability question asks: if the coin's heads chance is 0.5, how likely are seven heads? The answer is a number, about 0.117. An inference question turns this around: the seven heads are known, so which heads chance would make them most plausible?

The formula is the same in both cases. Only the role of each piece changes. In the probability question the chance is fixed and the outcome varies. In the likelihood question the outcome is fixed and the chance varies. The likelihood function is that second reading.

Scoring candidates

Take a few candidate values of the heads chance pp: 0.1, 0.5 and 0.7. Under each one, the same observed data gets a different probability. Under 0.1, seven heads in ten flips is nearly impossible. Under 0.5, it is unusual but possible. Under 0.7, it is the most natural outcome. The likelihood simply records these scores for every candidate, and the candidate with the largest score is the best explanation.

The name is chosen carefully. "Likely" describes the data under a setting, so the likelihood is a measure of how well each setting explains the observations. It is not a probability that the setting is true. That distinction is the main source of confusion, and we will return to it.

flowchart LR
  A[Observed data, held fixed] --> B[Candidate parameter theta]
  B --> C[Probability of the data under theta]
  C --> D[Likelihood of theta]
  D --> E[Compare across all candidates]

The math

Definition

Suppose the data are x=(x1,…,xn)x = (x_1, \dots, x_n) and the model says each observation comes from a distribution with density f(x;θ)f(x; \theta), where θ\theta is the parameter. For a discrete model, ff is a probability mass function instead. The data are assumed to be independent, so their joint probability is a product.

Definition: Likelihood function

For fixed observed data x1,…,xnx_1, \dots, x_n, the likelihood of a parameter value θ\theta is

L(θ∣x1,…,xn)=∏i=1nf(xi;θ).L(\theta \mid x_1, \dots, x_n) = \prod_{i=1}^{n} f(x_i; \theta).

Here θ\theta is the variable and the data are constants. The joint density of the data, viewed as a function of xx for a fixed θ\theta, is the same expression; we only change which argument we vary.

Two rules follow directly from this definition.

  1. Only the ratio of likelihoods between two parameter values carries meaning. Multiplying LL by any constant that does not depend on θ\theta leaves all comparisons unchanged.
  2. The likelihood does not integrate or sum to 1 over θ\theta in general. It is not a probability distribution over the parameters.

The log-likelihood

Products of many numbers below 1 become tiny, and a computer may round them to zero. So we almost always work with the logarithm:

ℓ(θ)=log⁡L(θ)=∑i=1nlog⁡f(xi;θ).\ell(\theta) = \log L(\theta) = \sum_{i=1}^{n} \log f(x_i; \theta).

The logarithm is increasing, so the θ\theta that maximises LL also maximises ℓ\ell. It also turns the product into a sum, which is easier to differentiate. The Logarithm is used only for this convenience; it does not change which parameter is best.

Worked form: the coin

For nn flips (each a Bernoulli trial, so kk follows a binomial) with kk heads and heads chance pp, each flip contributes pp or 1−p1-p, so

L(p)=pk(1−p)n−k,ℓ(p)=klog⁡p+(n−k)log⁡(1−p).L(p) = p^{k} (1-p)^{n-k}, \qquad \ell(p) = k \log p + (n-k) \log(1-p).

The binomial coefficient is left out because it does not depend on pp (rule 1 above). The curve for seven heads in ten flips peaks at p=0.7p = 0.7 and is already quite wide, so many nearby chances remain plausible.

00.20.40.60.8100.00050.0010.00150.0020.0025p (heads chance)likelihood
  • L(p), 7 heads in 10 flips
The likelihood of seven heads in ten flips, as a function of the heads chance p. It peaks at p = 0.7 and the area under it is not 1.

Worked form: a Gaussian

For observations from N(μ,σ2)\mathcal{N}(\mu, \sigma^2), with θ=(μ,σ2)\theta = (\mu, \sigma^2),

ℓ(μ,σ2)=−n2log⁡(2πσ2)−12σ2∑i=1n(xi−μ)2.\ell(\mu, \sigma^2) = -\frac{n}{2} \log(2\pi\sigma^2) - \frac{1}{2\sigma^2} \sum_{i=1}^{n} (x_i - \mu)^2.

The likelihood is a surface over (μ,σ2)(\mu, \sigma^2). Its shape tells you which pairs of values are consistent with the data.

Worked examples

Example
Example 1: three flips (tiny)

Observe H, T, H, so n=3n = 3 and k=2k = 2. The likelihood is L(p)=p2(1−p)L(p) = p^2 (1-p).

  1. Try p=0.5p = 0.5: L=0.25×0.5=0.125L = 0.25 \times 0.5 = 0.125.
  2. Try p=2/3p = 2/3: L=(4/9)×(1/3)=4/27≈0.148L = (4/9) \times (1/3) = 4/27 \approx 0.148.
  3. Try p=0.9p = 0.9: L=0.81×0.1=0.081L = 0.81 \times 0.1 = 0.081.
  4. The largest of these three is at p=2/3p = 2/3. To find the exact peak, set the Derivative of ℓ(p)=2log⁡p+log⁡(1−p)\ell(p) = 2 \log p + \log(1-p) to zero: 2/p−1/(1−p)=02/p - 1/(1-p) = 0, which gives p=2/3p = 2/3.

Interpretation: among these heads chances, two-thirds explains "two heads in three" best. The likelihood rules out 0.90.9 as a poor explanation, but it does not say 2/32/3 is the true value.

Example
Example 2: a Gaussian sample (standard textbook case)

Four observations are 2, 4, 6 and 8. Assume they come from a Gaussian with unknown mean μ\mu and variance σ2\sigma^2.

  1. Write the log-likelihood: ℓ(μ,σ2)=−2log⁡(2πσ2)−12σ2∑(xi−μ)2\ell(\mu, \sigma^2) = -2 \log(2\pi\sigma^2) - \frac{1}{2\sigma^2} \sum (x_i - \mu)^2.
  2. Differentiate with respect to μ\mu and set to zero: ∑(xi−μ)=0\sum (x_i - \mu) = 0, so μ^=(2+4+6+8)/4=5\hat{\mu} = (2 + 4 + 6 + 8)/4 = 5.
  3. Compute the squared deviations from 5: 9,1,1,99, 1, 1, 9, summing to 20.
  4. Differentiate with respect to s=σ2s = \sigma^2 and set to zero: −n2s+S2s2=0-\frac{n}{2s} + \frac{S}{2s^2} = 0, where S=20S = 20 and n=4n = 4. So s^=S/n=5\hat{s} = S/n = 5.
  5. Take the square root for the standard deviation: σ^=5≈2.24\hat{\sigma} = \sqrt{5} \approx 2.24.

Interpretation: the likelihood peaks at mean 5 and variance 5. The divisor is nn, not n−1n-1, which makes this variance estimate slightly too small on average. That bias is a known feature of the maximum likelihood answer for the variance.

Example
Example 3: sensor readings (realistic)

A sensor reads a fixed voltage five times: 9.8, 10.1, 10.3, 9.9 and 10.4. Assume the readings are independent Gaussians with unknown mean and variance.

  1. The sum is 50.550.5, so the mean estimate is μ^=10.1\hat{\mu} = 10.1.
  2. The deviations from 10.1 are −0.3,0,0.2,−0.2,0.3-0.3, 0, 0.2, -0.2, 0.3. Their squares are 0.09,0,0.04,0.04,0.090.09, 0, 0.04, 0.04, 0.09, summing to 0.260.26.
  3. The variance estimate is s^=0.26/5=0.052\hat{s} = 0.26 / 5 = 0.052, so σ^≈0.228\hat{\sigma} \approx 0.228.
  4. To compare with a rival setting such as μ=10.5\mu = 10.5, the log-likelihood difference is ℓ(10.1)−ℓ(10.5)=12s^∑[(xi−10.5)2−(xi−10.1)2]\ell(10.1) - \ell(10.5) = \frac{1}{2\hat{s}} \sum \big[(x_i - 10.5)^2 - (x_i - 10.1)^2\big]. The sum equals 5×0.16=0.85 \times 0.16 = 0.8 in this case, so the difference is 0.8/(2×0.052)≈7.70.8 / (2 \times 0.052) \approx 7.7. The likelihood ratio is e7.7e^{7.7}, about 2200 to 1 in favour of μ=10.1\mu = 10.1.

Interpretation: the data overwhelmingly favour a mean near 10.1 over 10.5. This comparison holds the variance fixed at its estimate. The likelihood ratio (see Log-Likelihood Ratio) is a direct way to compare two settings, and it needs no prior.

Tradeoffs and failure modes

The likelihood is only as good as the model behind it. When the model family is wrong, the highest likelihood setting can still be the best of a bad set.

SituationWhat goes wrongBetter choice
Observations are not independentThe product in the likelihood double counts informationUse a model that includes the dependence, such as a time series or hierarchical model
The model family is misspecifiedThe best setting explains the data poorlyCheck residuals, or compare several model families
Few data pointsThe likelihood is flat, and many settings look similarReport the whole likelihood curve, not only its peak
Parameters are on the boundaryDerivatives may not be zero at the best pointCheck the boundary values directly

A practical point: always inspect the shape of the likelihood, not only its maximum. A sharp peak says the data pin the parameter down. A flat one says they do not.

History

The idea of comparing hypotheses by how well they predict observed data goes back to the eighteenth century, but the term "likelihood" as a technical concept distinct from probability was developed by the statistician R. A. Fisher in the 1920s. He argued that the likelihood, not the probability of a parameter, should drive inference when the parameter is unknown. The approach caught on because it gives a single, mechanical recipe for estimation and testing across many models. Later work on the likelihood ratio, which compares two settings directly, made it central to hypothesis testing.

Recap

The likelihood function fixes the observed data and varies the parameter. Its value at each candidate records how well that candidate explains what was seen. Working with its logarithm turns products into sums without changing the best candidate. Maximising the likelihood gives maximum likelihood estimates. Multiplying the likelihood by a prior and normalising gives the posterior distribution, so the likelihood is the piece of evidence that every Bayesian update uses.

Check yourself

QuestionRecall: in the likelihood function, which quantity is held fixed and which one varies?Show answer

The observed data are held fixed, and the parameter varies over its possible values. This is the opposite of a probability density, where the parameter is fixed and the outcome varies.

QuestionComputation: a coin shows four heads in five flips. Compare the likelihood of heads chance 0.50.5 with that of 0.80.8. Which is larger, and by what ratio?Show answer

The likelihood is p4(1−p)p^4 (1-p). At 0.50.5 it is 0.0625×0.5=0.031250.0625 \times 0.5 = 0.03125. At 0.80.8 it is 0.4096×0.2=0.081920.4096 \times 0.2 = 0.08192. The ratio is 0.08192/0.03125≈2.620.08192 / 0.03125 \approx 2.62, so 0.80.8 explains the data about 2.6 times better.

QuestionWhy is the likelihood not a probability distribution over the parameter, even though it is built from probabilities?Show answer

Its values do not need to sum or integrate to 1 across parameter values. It measures how well each parameter explains fixed data, not how likely each parameter is. Turning it into a distribution over parameters requires a prior and normalisation, which is the posterior step.

QuestionApplied: a student multiplies two thousand probabilities, each about 0.5, and the computer prints 0. What is the fix, and why does it not change the best parameter?Show answer

Compute the sum of logarithms instead of the product. Each log is around minus 0.7, so the sum is about minus 1,390, which is easily representable (the product, about 10−60210^{-602}, is smaller than the smallest number a standard double-precision float can hold). The logarithm is increasing, so the parameter that maximises the product also maximises the sum of logs.

Sources

Likelihood Function — explained simply | ungoon.me