Search

Search papers and lessons

Posterior Distribution

Level 2 · 10 min read

The posterior distribution is your updated belief about an unknown quantity after seeing data, obtained by multiplying the prior by the likelihood and rescaling.

On this page

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

TL;DR

The posterior distribution is what you believe about an unknown quantity after you have seen some data. You start from a prior, which is your belief before the data. You then multiply it by how well each possible value explains the data, the likelihood, and rescale so the beliefs add up to one.

Where you'll see it: in any Bayesian estimate, such as the probability that a coin is biased after a few flips, the updated spam score for an email, or the error bars reported alongside a fitted model.

Intuition

Start from a question you already ask

Suppose a friend hands you a coin and says it might be fair or might be loaded toward heads. Before flipping, you would say both are equally likely. Now you flip it and get heads. Did that change your mind? A little, toward the loaded coin, because a loaded coin makes heads more likely. The posterior is the precise version of this change of mind: a new set of beliefs, one for each possible coin, that combines what you thought before with what the flip showed.

Beliefs as a set of weights

Think of each possible value of the unknown quantity as a candidate, with a weight. The prior gives each candidate its starting weight. The data then reweights the candidates: a candidate that makes the data likely gains weight, and a candidate that makes the data surprising loses weight. Finally, the weights are scaled so they sum to one. The result is still a list of candidates with weights, which is the posterior.

flowchart LR
  A[Prior: belief before data] --> C[Multiply weight by likelihood]
  B[Likelihood: how well each value explains the data] --> C
  C --> D[Rescale so weights sum to 1]
  D --> E[Posterior: belief after data]
  E -.->|becomes the prior for the next batch of data| A

The last arrow is important. The posterior from one batch of data is a perfectly good prior for the next batch. This is how Bayesian learning accumulates evidence without starting over.

The math

Definition

Definition: Posterior distribution

Let θ\theta be an unknown quantity and xx the observed data. The posterior distribution of θ\theta given xx is

p(θ∣x)=p(x∣θ) p(θ)p(x)p(\theta \mid x) = \frac{p(x \mid \theta)\, p(\theta)}{p(x)}

where p(θ)p(\theta) is the prior, p(x∣θ)p(x \mid \theta) is the likelihood, and p(x)p(x) is the evidence.

Here θ\theta stands for any candidate value of the unknown, such as a coin's heads probability. The symbol p(θ)p(\theta) is the prior: how plausible each θ\theta was before the data. The symbol p(x∣θ)p(x \mid \theta) is a conditional probability and serves as the likelihood: how probable the observed data would be if θ\theta were the true value. The left-hand side p(θ∣x)p(\theta \mid x) is the posterior, the plausibility of each θ\theta after seeing xx. This is Bayes' rule applied to a quantity we want to learn.

The evidence is a normaliser

The denominator p(x)p(x) does not depend on θ\theta. It is the total weight of all candidates together, and it makes the posterior sum (or integrate) to one. For a discrete set of candidates, the law of total probability gives

p(x)=∑θp(x∣θ) p(θ).p(x) = \sum_{\theta} p(x \mid \theta)\, p(\theta).

Because p(x)p(x) is only a scaling factor, the shortcut used in practice is

p(θ∣x)∝p(x∣θ) p(θ),p(\theta \mid x) \propto p(x \mid \theta)\, p(\theta),

read "the posterior is proportional to likelihood times prior". Compute the product for every candidate, then divide by the sum of the products.

Continuous quantities

When θ\theta can take any value, the sum becomes an integral and the posterior is a density. The same rule holds: multiply prior density by likelihood, then divide by the integral of that product over all θ\theta. Most of the difficulty in Bayesian statistics is in computing that integral, which is why approximate methods exist. The examples below use a few candidate values so every number can be checked by hand.

Posterior and maximum likelihood

The maximum likelihood estimate picks the θ\theta with the largest likelihood and ignores the prior. The posterior mode (the most probable θ\theta after seeing the data) is the value that maximises likelihood times prior. The two agree when the prior is flat (every candidate starts with the same weight). The posterior gives more than a single best value: it gives a weight for every candidate, so you can see how uncertain the answer is.

Worked examples

Example
Example 1: one flip, two coins (tiny)

Two candidate coins: a fair coin with heads probability 0.50.5, or a loaded coin with heads probability 0.80.8. Before flipping, you judge them equally likely, so p(fair)=p(loaded)=0.5p(\text{fair}) = p(\text{loaded}) = 0.5. You flip once and get heads.

  1. Likelihoods of heads: p(H∣fair)=0.5p(H \mid \text{fair}) = 0.5 and p(H∣loaded)=0.8p(H \mid \text{loaded}) = 0.8.
  2. Multiply by the prior: fair gives 0.5×0.5=0.250.5 \times 0.5 = 0.25, and loaded gives 0.5×0.8=0.400.5 \times 0.8 = 0.40.
  3. Add the products to get the evidence: p(H)=0.25+0.40=0.65p(H) = 0.25 + 0.40 = 0.65.
  4. Divide each product by the evidence. Posterior for fair: 0.25/0.65=5/13≈0.3850.25 / 0.65 = 5/13 \approx 0.385. Posterior for loaded: 0.40/0.65=8/13≈0.6150.40 / 0.65 = 8/13 \approx 0.615.
  5. Check: 0.385+0.615=10.385 + 0.615 = 1.

Interpretation: one head moved belief toward the loaded coin, from 50% to about 62%. It did not settle the question, because one flip is weak evidence. Notice that the posterior is not the likelihood: the loaded coin has the higher likelihood, but the posterior weighs it against the prior too.

Example
Example 2: three candidate biases (the standard case)

Suppose the heads probability θ\theta is one of three values, 0.20.2, 0.50.5 or 0.80.8, with a uniform prior of 1/31/3 each. You flip five times and see four heads and one tail. The likelihood of a specific sequence with hh heads and tt tails is θh(1−θ)t\theta^h (1-\theta)^t. Here h=4h = 4 and t=1t = 1, so the likelihood is θ4(1−θ)\theta^4 (1-\theta). The count of orderings is the same for every θ\theta, so it cancels and we can ignore it.

  1. For θ=0.2\theta = 0.2: 0.24×0.8=0.0016×0.8=0.001280.2^4 \times 0.8 = 0.0016 \times 0.8 = 0.00128.
  2. For θ=0.5\theta = 0.5: 0.54×0.5=0.0625×0.5=0.031250.5^4 \times 0.5 = 0.0625 \times 0.5 = 0.03125.
  3. For θ=0.8\theta = 0.8: 0.84×0.2=0.4096×0.2=0.081920.8^4 \times 0.2 = 0.4096 \times 0.2 = 0.08192.
  4. Multiply each by the prior 1/31/3, which is the same factor for all three, so we can skip it and simply sum the unnormalised weights: 0.00128+0.03125+0.08192=0.114450.00128 + 0.03125 + 0.08192 = 0.11445.
  5. Divide each weight by 0.114450.11445:
    • θ=0.2\theta = 0.2: 0.00128/0.11445≈0.0110.00128 / 0.11445 \approx 0.011
    • θ=0.5\theta = 0.5: 0.03125/0.11445≈0.2730.03125 / 0.11445 \approx 0.273
    • θ=0.8\theta = 0.8: 0.08192/0.11445≈0.7160.08192 / 0.11445 \approx 0.716
  6. Check: 0.011+0.273+0.716=1.0000.011 + 0.273 + 0.716 = 1.000.

Interpretation: the data favour the high bias, and the posterior puts about 72% on 0.80.8. The candidate 0.20.2 is almost ruled out, since four heads in five flips would be very unlikely from it. Had the prior been different, the answer would move, which is the point of stating the prior openly.

Example
Example 3: is this email spam? (realistic)

A mail filter has a prior: 40% of incoming mail is spam. Among spam, the word "free" appears in 30% of messages. Among ordinary mail, it appears in 2%. An email arrives containing "free". What is the posterior probability that it is spam?

  1. Prior: p(spam)=0.40p(\text{spam}) = 0.40 and p(ham)=0.60p(\text{ham}) = 0.60, where ham means ordinary mail.
  2. Likelihood of the data under each hypothesis: p(free∣spam)=0.30p(\text{free} \mid \text{spam}) = 0.30 and p(free∣ham)=0.02p(\text{free} \mid \text{ham}) = 0.02.
  3. Multiply: spam gives 0.40×0.30=0.1200.40 \times 0.30 = 0.120, and ham gives 0.60×0.02=0.0120.60 \times 0.02 = 0.012.
  4. Evidence: p(free)=0.120+0.012=0.132p(\text{free}) = 0.120 + 0.012 = 0.132.
  5. Posterior for spam: 0.120/0.132=10/11≈0.9090.120 / 0.132 = 10/11 \approx 0.909.
  6. Posterior for ham: 0.012/0.132≈0.0910.012 / 0.132 \approx 0.091. The two add to 1.

Interpretation: the word raises the spam probability from 40% to about 91%. Two facts drive this. The word is much more common in spam (a likelihood ratio of 0.30/0.02=150.30 / 0.02 = 15), and the prior is not extreme. A filter that also sees other words would apply the same rule again, with this posterior as the new prior.

Tradeoffs and failure modes

The posterior is only as good as the prior and the likelihood behind it. The table lists common issues.

SituationWhat goes wrongBetter choice
The prior is strong and the data are fewThe posterior mostly repeats the priorCollect more data, or state the prior's influence openly
The prior is badly chosenA wrong belief can dominate the resultCheck sensitivity: rerun with several reasonable priors
The model of the data is wrongThe likelihood is misleading, so the posterior is confidently wrongCheck the model against held-out data
The parameter space is largeThe evidence integral cannot be computed exactlyUse approximations, such as sampling or variational methods

A practical point: a posterior is a full distribution, so report it, not just its peak. A narrow posterior and a wide one can share the same most-probable value.

History

Thomas Bayes wrote down the rule that bears his name in an essay published after his death in 1763, in the context of inferring causes from observed effects. Pierre-Simon Laplace developed the general method of updating beliefs with data in the late 1700s and early 1800s and popularised it widely. The word "posterior" itself comes from the Latin for "later": it is the belief held after the data arrive. For much of the twentieth century, frequentist methods dominated statistics, and Bayesian methods were regarded as subjective. Computing power changed that. Sampling methods made it practical to compute posteriors for complex models, and posteriors are now routine in fields from genetics to machine learning.

Recap

Bayes' rule says the posterior is proportional to likelihood times prior. The prior encodes what was believed before the data, and the likelihood measures how well each candidate explains the data. Dividing by the evidence makes the weights add to one. The posterior is the object that Bayesian estimates, uncertainty ranges and sequential updating are all built from.

Check yourself

QuestionRecall: what are the three ingredients in Bayes' rule for a posterior, and which one is the normaliser?Show answer

The three ingredients are the prior p(θ)p(\theta), the likelihood p(x∣θ)p(x \mid \theta) and the evidence p(x)p(x). The evidence is the normaliser: it does not depend on the candidate θ\theta, and it makes the posterior weights sum (or integrate) to one.

QuestionComputation: a coin is either fair (heads probability 0.50.5) or loaded toward tails (heads probability 0.30.3), each with prior probability 0.50.5. You flip once and get tails. What is the posterior probability that the coin is loaded?Show answer

The likelihood of tails is 0.50.5 for the fair coin and 0.70.7 for the loaded coin. Multiply by the prior: fair gives 0.250.25 and loaded gives 0.350.35. The evidence is 0.25+0.35=0.600.25 + 0.35 = 0.60. The posterior probability of loaded is 0.35/0.60=7/12≈0.5830.35 / 0.60 = 7/12 \approx 0.583.

QuestionWhy: why is the posterior mode not always the same as the maximum likelihood estimate?Show answer

The posterior weights each candidate by likelihood times prior, while maximum likelihood uses the likelihood alone. When the prior is flat, the two rank candidates identically and the peaks coincide. When the prior favours some candidates, it shifts the posterior peak toward them, so the two estimates can differ.

QuestionApplied: a spam filter sees a second word, a word that is three times as common in spam as in ordinary mail. How should the posterior for the email be updated?Show answer

Use the posterior from the first word as the new prior, then multiply by the likelihood of the second word under each hypothesis, and normalise again. The second word's effect is the ratio of its likelihoods under spam and ordinary mail, which is 3 in this case, so the odds of spam are multiplied by 3. This assumes the two words are independent given the class, which is a modelling choice that should be checked.

Sources