Search

Search papers and lessons

Prior Distribution

Level 2 · 10 min read

A prior distribution is your belief about an unknown quantity before you look at the data, written as a probability distribution over the values it could take.

On this page

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

TL;DR

A prior distribution is what you believe about an unknown quantity before you have seen any data. You write that belief as a probability distribution over the values the quantity could take. When data arrives, Bayes' rule combines the prior with how well each value explains the data, and the result is the updated belief, called the posterior.

Not being sure is still a belief. A prior can be sharp, saying the answer is almost certainly near one value, or flat, saying every value is equally plausible.

Where you'll see it: estimating the chance that a coin lands heads, choosing a penalty that keeps a regression from fitting noise, and any model that reports how uncertain its answer is.

Intuition

Start from a question you already know

Someone hands you a coin and asks: what is the chance it lands heads? Before flipping it, most people say "about one half." You have a belief, even though you have no data yet. You also would not be shocked if it were a trick coin with a heavier side, so your belief has some spread around one half.

Flip it ten times and see seven heads. Your belief should move towards "more likely heads than tails," but not all the way to certainty, because ten flips is a small sample. The prior is the starting point for that move. It says how far the data has to push before your answer changes.

Why we need it at all

The data alone can mislead. Ten flips, seven heads, suggests a chance of 0.7. But if you flipped a coin twice and got two heads, the same reasoning would suggest a chance of 1.0, which almost nobody believes. The prior keeps small samples from producing extreme answers. Combined with the data it gives a sensible answer that improves as the data grows. The usual estimate that uses no prior at all is covered in Maximum Likelihood Estimation, and the gap between the two is the subject of this lesson.

Two sources of information

Bayesian reasoning splits the work into two parts.

  1. The prior is what you knew before the data, written as a distribution over the unknown.
  2. The likelihood is how probable the observed data is for each possible value of the unknown.

Multiply the two, rescale so the total is one, and you have the posterior. Nothing else is needed.

00.20.40.60.8100.511.522.5chance of headsdensity
  • prior: flat
  • after one head
  • after three heads, one tail
A flat prior over the chance of heads, and the posteriors after two different sets of flips. More data pulls the curve towards the values the flips support.

The math

Let θ\theta stand for the unknown quantity, here the chance of heads. Let DD stand for the observed data.

Definition: Prior distribution

The prior p(θ)p(\theta) is a probability density over the possible values of θ\theta, written before the data is seen. Its values are non-negative, and its integral over all possible θ\theta is 11.

Bayes' rule gives the posterior:

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

Here p(D∣θ)p(D \mid \theta) is the likelihood: the chance of seeing data DD if the unknown were θ\theta (a conditional probability). The denominator p(D)p(D) is a normalising constant. It makes the posterior integrate to one, and it can be computed as the sum or integral of the numerator over all θ\theta. Because p(D)p(D) does not depend on θ\theta, the posterior is proportional to likelihood times prior:

p(θ∣D)∝p(D∣θ) p(θ).p(\theta \mid D) \propto p(D \mid \theta)\, p(\theta).

That proportionality is the working rule. Compute the product, then rescale.

Discrete hypotheses

When the unknown takes a few values, the prior is a list of numbers, one per hypothesis. The same rule applies with sums in place of integrals. If H1H_1 and H2H_2 are two hypotheses with priors p(H1)p(H_1) and p(H2)p(H_2), then

p(H1∣D)=p(D∣H1) p(H1)p(D∣H1) p(H1)+p(D∣H2) p(H2).p(H_1 \mid D) = \frac{p(D \mid H_1)\, p(H_1)}{p(D \mid H_1)\, p(H_1) + p(D \mid H_2)\, p(H_2)}.

The uniform prior

A prior that gives every value of θ\theta on [0,1][0, 1] the same density is the uniform prior, p(θ)=1p(\theta) = 1 on that interval (see Uniform Distribution). It says you have no preference. It is common in teaching because the arithmetic is simple, although it is not automatically the "neutral" choice, as the tradeoffs section explains.

The MAP estimate

The posterior is a full distribution, but a single answer is often wanted. The most probable value of θ\theta under the posterior is the maximum a posteriori (MAP) estimate, covered in Maximum a Posteriori (MAP) Estimation. Maximising p(D∣θ) p(θ)p(D \mid \theta)\, p(\theta) is the same as maximising the likelihood when the prior is flat, and it adds the prior's preference when it is not.

Worked examples

Example
A box of two kinds (discrete)

A box is either type A or type B. Before drawing, your prior says A is more likely: p(A)=0.7p(A) = 0.7 and p(B)=0.3p(B) = 0.3. A ball drawn from A is red with chance 0.20.2. A ball drawn from B is red with chance 0.60.6. You draw one ball and it is red. Which box is it more likely to be?

  1. Likelihood of red under each box: p(red∣A)=0.2p(\text{red} \mid A) = 0.2 and p(red∣B)=0.6p(\text{red} \mid B) = 0.6.
  2. Multiply each likelihood by its prior: for A, 0.2×0.7=0.140.2 \times 0.7 = 0.14; for B, 0.6×0.3=0.180.6 \times 0.3 = 0.18.
  3. Total: 0.14+0.18=0.320.14 + 0.18 = 0.32. This is p(red)p(\text{red}).
  4. Posterior for A: 0.14/0.32=0.43750.14 / 0.32 = 0.4375. Posterior for B: 0.18/0.32=0.56250.18 / 0.32 = 0.5625.

Interpretation: the prior favoured A, but the red ball is three times more likely from B than from A. The evidence overturned the prior, though not by much, because the prior was not extreme.

Example
Chance of heads with a flat prior (textbook)

Let θ\theta be the chance of heads. Use a uniform prior, p(θ)=1p(\theta) = 1 on [0,1][0, 1]. Flip the coin and get three heads and one tail. The likelihood of one specific sequence with hh heads and tt tails is θh(1−θ)t\theta^h (1 - \theta)^t.

  1. Likelihood: θ3(1−θ)\theta^3 (1 - \theta).
  2. Multiply by the prior, which is 11: the unnormalised posterior is θ3(1−θ)\theta^3 (1 - \theta).
  3. Normalising constant: ∫01θ3(1−θ) dθ=∫01(θ3−θ4) dθ=14−15=120\int_0^1 \theta^3 (1 - \theta)\, d\theta = \int_0^1 (\theta^3 - \theta^4)\, d\theta = \frac{1}{4} - \frac{1}{5} = \frac{1}{20}.
  4. Posterior density: p(θ∣D)=20 θ3(1−θ)p(\theta \mid D) = 20\, \theta^3 (1 - \theta). Check: it integrates to 20×120=120 \times \frac{1}{20} = 1.
  5. Posterior mean: ∫01θ⋅20 θ3(1−θ) dθ=20(15−16)=20⋅130=23\int_0^1 \theta \cdot 20\, \theta^3 (1 - \theta)\, d\theta = 20\left(\frac{1}{5} - \frac{1}{6}\right) = 20 \cdot \frac{1}{30} = \frac{2}{3}.
  6. For comparison, the most likely single value, the maximum likelihood estimate, is 3/43/4.

Interpretation: the posterior mean 2/32/3 sits between the prior's centre, 1/21/2, and the data's 3/43/4. The prior pulls the estimate back towards one half, which is most useful when the data is scarce.

Example
A prior as a penalty on a fitted line (realistic)

A common use of a prior is to stop a fitted coefficient from growing too large. Suppose you fit a line through the origin, y=wxy = wx (a tiny Linear Regression), and observe two points (x,y)=(1,2)(x, y) = (1, 2) and (2,3)(2, 3). Noise is Gaussian with variance σ2=1\sigma^2 = 1 (see Gaussian (Normal) Distribution). Put a Gaussian prior on the slope ww with mean 00 and variance τ2=1\tau^2 = 1.

  1. The negative log of the posterior is, up to a constant, 12σ2∑i(yi−wxi)2+12τ2w2\frac{1}{2\sigma^2} \sum_i (y_i - w x_i)^2 + \frac{1}{2\tau^2} w^2. Maximising the posterior means minimising this.
  2. With σ2=τ2=1\sigma^2 = \tau^2 = 1, minimise (2−w)2+(3−2w)2+w2(2 - w)^2 + (3 - 2w)^2 + w^2.
  3. Take the Derivative with respect to ww and set it to zero: −2(2−w)−4(3−2w)+2w=0-2(2 - w) - 4(3 - 2w) + 2w = 0, which gives −16+12w=0-16 + 12 w = 0, so w=4/3w = 4/3.
  4. Without the prior (maximum likelihood), w=∑xiyi∑xi2=2+61+4=1.6w = \frac{\sum x_i y_i}{\sum x_i^2} = \frac{2 + 6}{1 + 4} = 1.6.

Interpretation: the prior shrinks the slope from 1.61.6 to 4/3≈1.334/3 \approx 1.33. The penalty term w2/(2τ2)w^2/(2\tau^2) is exactly the L2 penalty used in practice to fight Overfitting, and the amount of shrinkage depends on the ratio σ2/τ2\sigma^2/\tau^2. A tighter prior, with smaller τ\tau, pulls harder.

Tradeoffs and failure modes

A prior is a choice, and a choice can be wrong. The table lists situations you will meet.

SituationProblemBetter choice
Little data and a strong priorThe answer mostly reflects the priorCheck the prior's influence, and collect more data if it dominates
"Flat" prior on a quantity with a natural scaleFlat in one parameterisation (for example θ\theta versus θ2\theta^2) is not flat in anotherPick the prior on the scale where you have knowledge, or use a standard reference prior chosen for that purpose
Prior ruled out the true valueNo amount of data recovers itGive the prior some spread across plausible values
Only the most probable value is neededA full posterior is more work than neededUse Maximum a Posteriori (MAP) Estimation
Prior is unknown and must be learnedHyperparameters (the settings of the prior itself, such as τ\tau) can overfit the dataPut a prior on them too (a hierarchical model) or Bayesian Model Averaging

The most common objection is that priors are subjective. That is true, and it is also a feature: the assumptions are stated where anyone can criticise them, and they can be tested by checking how much the answer changes when the prior changes. A flat prior is not automatically objective either, since uniform in one scale is not uniform in another.

History

The idea goes back to Thomas Bayes, an English minister whose essay on the problem of inverse probability was published in 1763, two years after his death, through the efforts of Richard Price. Pierre-Simon Laplace developed the approach in the 1770s and applied it widely in astronomy and population studies, using a flat prior when nothing else was known. The term "prior" and the broad programme of updating beliefs with data became the basis of Bayesian statistics. The approach was for a long time overshadowed by frequentist methods, and the spread of computing in the late twentieth century, which made posteriors feasible to compute, brought it back into wide use.

Recap

A prior is the belief you hold before the data. Bayes' rule multiplies it by the likelihood and rescales, which gives the posterior. A flat prior produces simple arithmetic, and a shaped prior encodes real knowledge. On a regression, a Gaussian prior on each coefficient becomes a penalty, which is why it reappears as shrinkage. The next step is to pick one answer from the posterior with Maximum a Posteriori (MAP) Estimation, or to average over models with Bayesian Model Averaging.

Check yourself

QuestionIn one sentence, what is the difference between the prior and the posterior?Show answer

The prior is the belief about the unknown before the data, and the posterior is the belief after combining the prior with the likelihood of the data. The prior is an input to Bayes' rule, and the posterior is its output.

QuestionA prior gives probability 0.5 to hypothesis A and 0.5 to B. The data is three times more likely under A than under B. What is the posterior probability of A?Show answer

Let the likelihood of the data under B be LL, so under A it is 3L3L. Multiply by the priors: A gives 3L×0.5=1.5L3L \times 0.5 = 1.5L, and B gives L×0.5=0.5LL \times 0.5 = 0.5L. The total is 2L2L. So p(A∣D)=1.5L/2L=0.75p(A \mid D) = 1.5L / 2L = 0.75.

QuestionWhy does a prior prevent a conclusion from two heads in two flips that the coin always lands heads?Show answer

The likelihood alone would favour a chance of one, because that makes two heads most probable. A prior that puts little weight near one, such as a flat or a centred prior, keeps the posterior spread out and away from the extreme. The data is weak, so the prior's pull towards the centre dominates. With more flips the likelihood would overpower the prior.

QuestionYou want a single estimate of a coefficient with a Gaussian prior centred on zero. Name the estimate and say what the prior does to it compared with no prior.Show answer

The estimate is the maximum a posteriori (MAP) value. The prior pulls the estimate towards zero, shrinking it relative to the maximum likelihood estimate. The amount of shrinkage grows as the prior becomes tighter relative to the noise.

Sources