Track 16 · Advanced Foundations · Lesson 1 of 8

Bayesian thinking: prior, likelihood, posterior

Combine a prior distribution with the likelihood of observed data to obtain a posterior distribution, and explain how the prior and the data share the influence.

35 minAdvanced8 graded exercisesFree

A quality engineer already suspects that a new supplier is probably fine, but after 40 parts with 4 defects her suspicion should change — by how much? Bayesian inference turns “what I believed” plus “what I saw” into “what I should now believe”, with the arithmetic made explicit.

In the methods of the earlier tracks, a parameter such as a defect rate θ\theta is a fixed unknown number and probability describes how data vary from sample to sample. Bayesian inference also uses probability to describe what we know about the parameter. Before the data, that knowledge is the prior distribution. The data enter through the likelihood: how probable the observed data would be for each possible value of θ\theta. Bayes' rule combines them into the posterior distribution: what we know about θ\theta after seeing the data.

p(θ∣data)=p(data∣θ) p(θ)p(data)  ∝  p(data∣θ)⏟likelihood×p(θ)⏟priorp(\theta \given \text{data}) = \frac{p(\text{data} \given \theta)\, p(\theta)}{p(\text{data})} \;\propto\; \underbrace{p(\text{data} \given \theta)}_{\text{likelihood}} \times \underbrace{p(\theta)}_{\text{prior}}
Bayes' rule for a parameter: the posterior is proportional to likelihood × prior
p(θ)p(\theta)
the prior: plausibility of each value of θ\theta before seeing the data
p(data∣θ)p(\text{data} \given \theta)
the likelihood: probability of the observed data if θ\theta were the true value
p(θ∣data)p(\theta \given \text{data})
the posterior: plausibility of each value of θ\theta after seeing the data
p(data)p(\text{data})
the total probability of the data, summed over all values of θ\theta; it only rescales the products so the posterior adds to 1

Worked example

Is the new supplier good, average or poor?

From experience with similar suppliers, an engineer gives prior probabilities 0.5 to “good” (defect rate 2%), 0.3 to “average” (defect rate 5%), 0.2 to “poor” (defect rate 10%). She inspects 40 parts and finds 4 defective. What are the posterior probabilities?

  1. Likelihood of the data under each state (binomial, n=40n = 40, k=4k = 4): P(4∣0.02)=0.0071P(4 \given 0.02) = 0.0071; P(4∣0.05)=0.0901P(4 \given 0.05) = 0.0901; P(4∣0.1)=0.2059P(4 \given 0.1) = 0.2059.

  2. Multiply by the prior: good 0.5×0.0071=0.003530.5 \times 0.0071 = 0.00353; average 0.3×0.0901=0.027040.3 \times 0.0901 = 0.02704; poor 0.2×0.2059=0.041180.2 \times 0.2059 = 0.04118.

  3. Normalise by the total 0.071750.07175 so the posterior adds to 1: good 0.049, average 0.377, poor 0.574.

  4. Compare with the prior. “Good” fell from 0.5 to 0.05 and “poor” rose from 0.2 to 0.57: an observed rate of 4/40 = 10% is far more probable for an average or poor supplier.

Posterior: good 0.049, average 0.377, poor 0.574. With equal prior probabilities (1/3 each) the posterior would have been 0.023, 0.297, 0.679 — the prior still matters with only 40 parts.

StateDefect rate θPriorLikelihoodPrior × likelihoodPosterior
good0.020.50.00710.003530.049
average0.050.30.09010.027040.377
poor0.10.20.20590.041180.574
The same calculation as a table: prior × likelihood, then rescale

You already know one famous Bayesian update: a diagnostic test (see Track 4, Probability). The prevalence is the prior probability of the condition; the test's sensitivity and specificity give the likelihood of a positive result; the positive predictive value is the posterior. A second, independent positive test then starts from that posterior as its prior.

Loading the interactive…
Prior (prevalence) → likelihood (sensitivity and specificity) → posterior (PPV), shown as 1 000 people.

Try this

  • Read off the PPV for prevalence 2%. Now set the prevalence to that PPV — this is the prior for a person who tests positive a second time. What is the new PPV?

  • Raise the specificity from 0.95 to 0.99 at prevalence 2%. Why does the posterior change so much more than when you raise the sensitivity by the same amount?

The same logic works for a continuous parameter. Suppose a delivery route's mean time μ\mu has a normal prior with mean μ0\mu_0 and standard deviation τ0\tau_0, and individual deliveries vary around μ\mu with a known standard deviation σ\sigma. After nn deliveries with mean xˉ\bar{x}, the posterior for μ\mu is again normal, and its mean is a precision-weighted average of the prior mean and the data mean (precision = 1 / variance).

μn=w xˉ+(1−w) μ0,w=n/σ21/τ02+n/σ2,τn=11/τ02+n/σ2\mu_n = w\,\bar{x} + (1 - w)\,\mu_0,\qquad w = \frac{n/\sigma^2}{1/\tau_0^2 + n/\sigma^2},\qquad \tau_n = \sqrt{\frac{1}{1/\tau_0^2 + n/\sigma^2}}
Normal prior, normal data with known σ: the posterior mean is a compromise
μ0,τ0\mu_0, \tau_0
prior mean and prior standard deviation of μ\mu
xˉ,n,σ\bar{x}, n, \sigma
sample mean, sample size, and the known SD of individual observations
ww
the weight on the data: it grows towards 1 as nn grows
μn,τn\mu_n, \tau_n
posterior mean and posterior standard deviation of μ\mu

With prior mean 42 minutes (SD 3), σ=9\sigma = 9 and a sample mean of 50 minutes: from 4 deliveries the data weight is w=0.308w = 0.308 and the posterior mean is 44.46 minutes (SD 2.50); from 100 deliveries w=0.917w = 0.917 and the posterior mean is 49.34 minutes (SD 0.86). A little data barely moves a confident prior; a lot of data overwhelms it.

Quick check

After the Bayesian update above, the posterior probability that the supplier is “poor” is 0.57. What does 0.57 describe?

Exercises

The lesson is complete when every exercise is answered correctly. Hints are free; viewing a worked solution before solving means the exercise won't count towards mastery.

0 of 8 exercises correct (not saved — sign in to keep your progress)

Exercise 1 · Guided, difficulty 1 of 3

Update a three-state prior

Not started

Prior probabilities for a new supplier's state are good (defect rate 2%) 0.5, average (5%) 0.3 and poor (10%) 0.2. An inspection of 50 parts finds 3 defective.

Using binomial likelihoods, find the posterior probabilities that the supplier is good and that it is poor (3 decimal places).

Exercise 2 · Practice, difficulty 2 of 3

A second positive test

Not started

A screening test has sensitivity 0.92 and specificity 0.94; the condition affects 3.0% of the people screened. A person tests positive, then takes a second test whose errors are independent of the first, and is positive again.

Give the probability that they have the condition after the first positive and after the second positive (3 decimal places).

Exercise 3 · Practice, difficulty 2 of 3

A normal prior for a mean

Not started

Experience with similar routes gives a normal prior for the mean delivery time μ\mu of a new route: mean 38 minutes, standard deviation 4 minutes. Individual deliveries vary around μ\mu with a known SD of σ=6\sigma = 6 minutes. The first 8 deliveries average xˉ=47.2\bar{x} = 47.2 minutes.

Find the posterior mean and posterior standard deviation of μ\mu (2 decimal places).

Exercise 4 · Application, difficulty 2 of 3

Prior and posterior in the screening data

Not started

Use the screening dataset, restricted to the 60+ age group (the exercise data are already filtered). Treat the proportion of people with the condition as the prior probability for a person in this group, and the proportion with the condition among those who tested positive as the posterior after a positive test.

Give both as proportions (3 decimal places), and the sensitivity P(+∣condition)P(+ \given \text{condition}) for comparison.

Data: screening (age_group = 60+) · Open in the lab to explore and calculate.

Exercise 5 · Practice, difficulty 1 of 3

Name the ingredient

Not started

A team estimates the click-through rate θ\theta of a new banner. Classify each quantity as part of the prior, the likelihood or the posterior.

Past banners had click-through rates mostly between 1% and 3%, so θ\theta is probably in that range
The probability of observing 38 clicks in 2 000 views if θ\theta were 2%
After the test, a 90% probability that θ\theta lies between 1.5% and 2.3%
The binomial model P(k∣θ)=(nk)θk(1−θ)n−kP(k \given \theta) = \binom{n}{k}\theta^k(1-\theta)^{n-k}
P(θ>2%∣38 clicks in 2 000 views)P(\theta > 2\% \given 38 \text{ clicks in } 2\,000 \text{ views})
A Beta(2, 98) distribution chosen before the test

Exercise 6 · Practice, difficulty 1 of 3

The Bayesian workflow

Not started

Put the steps of a Bayesian analysis in a sensible order.

Put the steps in order (use the arrows)

  1. 1Choose and justify a prior for θ before looking at the new data
  2. 2Check sensitivity: repeat with other reasonable priors and check the model's fit
  3. 3Collect the data and combine likelihood × prior into the posterior
  4. 4Specify the data model, which gives the likelihood
  5. 5Report posterior summaries (mean, interval, probabilities of interest) with the prior stated
  6. 6State the question and the parameter (for example, a defect rate θ)

Exercise 7 · Interpretation, difficulty 2 of 3

When does the prior matter?

Not started

For a route's mean delivery time, the prior is normal with mean 42 minutes (SD 3) and σ=9\sigma = 9. The sample mean is 50 minutes. With n=4n = 4 the posterior mean is 44.46; with n=100n = 100 it is 49.34. Which statements are correct? (Select all that apply.)

Select all that apply

Exercise 8 · Interpretation, difficulty 2 of 3

Explain the update to a manager

Not started

In the supplier example, the prior probability that the supplier is “poor” was 0.2; after 4 defects in 40 parts the posterior is 0.57. A manager asks: “Why isn't it 100% poor, when 10% of the parts were defective — exactly the poor rate?” Answer in two or three sentences.

0 words

What you can now do

  • Explain the roles of the prior, the likelihood and the posterior
  • Update a discrete prior with binomial data by multiplying and rescaling
  • Use a posterior as the prior for the next batch of data
  • Compute a normal–normal posterior as a precision-weighted compromise
  • Judge when the prior matters and when the data dominate