E ExamMaster

Artificial Intelligence · AI Foundations

Uncertainty and Bayes

In AI because reasoning under uncertainty (Bayes) is a core AI pillar that later feeds ML classifiers.

Logic gave the agent certainty; the world rarely does. This lesson is the machinery of belief under uncertainty — Bayes rule as the one coherent way to change your mind, the odds form engineers actually compute with, and the base-rate effects that make good tests produce surprising posteriors. Every number here is small on purpose, so each result can be computed by hand at least once.

  • Artificial Intelligence
  • Medium level
  • 6 concepts

1Why agents need probability

The logic of the previous lesson deals in certainty: a claim is true or false, and entailment either holds or it does not. An agent reading a sensor, a lab report or an email lives somewhere less tidy — the evidence is compatible with several states of the world at once, and acting well means weighing those states, not picking one and pretending.

Probability is the standard calculus for those weights. A degree of belief is a number between 0 and 1 attached to a proposition, and the rules of probability are what keep a set of such numbers coherent with one another: P(spam) = 0.9 reads as a strength of belief that obeys arithmetic.

Every quantity in this lesson plays one of three roles. The prior is belief before the evidence; the likelihood is how expected the evidence is under each hypothesis; the posterior is belief after. Bayes rule is nothing more than the bookkeeping that moves you from the first two to the third.

When not to reach for it: if the fact can be looked up or the rule is exact, probability adds nothing. Uncertainty calculus earns its cost only when the evidence genuinely underdetermines the answer.

Three roles
QuantityThe question it answers
Prior P(H)How plausible was the hypothesis before this evidence arrived?
Likelihood P(E|H)If the hypothesis were true, how expected is what I just observed?
Posterior P(H|E)Having observed the evidence, how plausible is the hypothesis now?

2Bayes rule

Bayes rule is how a rational agent changes its mind. Write the joint probability of a hypothesis H and evidence E in its two equivalent orders — P(E|H) P(H) = P(H|E) P(E) — and divide: P(H|E) = P(E|H)\,P(H) / P(E). Nothing else is going on; the rule is one joint probability read in two directions.

In update form: posterior ∝ likelihood × prior. Take what you believed before the evidence, weigh each hypothesis by how well it predicts the evidence, then renormalise so the beliefs sum to one. The denominator P(E) is just that normaliser — the total probability of seeing the evidence at all.

The direction of the conditioning is the whole game. P(E|H) is a property of the test or the sensor; P(H|E) is the question you actually care about, and the two can differ by an order of magnitude when the prior is small.

The screening example below is the canonical instance, worth computing once by hand: a genuinely good test, a rare condition, and a posterior that stays below ten per cent.

Figure. Screen 10,000 people: 100 are sick and the test catches 90 of them; 9,900 are healthy and 9% — 891 people — test positive anyway. A positive result therefore lands in a pool of 981 positives of which only 90 are sick: 90/981 ≈ 9.2%.

The update, in three moves

  1. Start from the priorBefore the test, a random person from this population carries P(D) = 0.01.
  2. Weigh by likelihoodMultiply each hypothesis by how well it predicts the positive: 0.90 for sick, 0.09 for healthy.
  3. RenormaliseDivide by the total probability of a positive so the two posteriors sum to one.

A positive test for a rare condition

A condition affects 1% of the people screened. The test catches 90% of true cases (sensitivity) and falsely fires on 9% of healthy people. A patient tests positive — how likely are they to have the condition?

  • Sick and positive: 0.90 × 0.010.0090
  • Healthy and positive: 0.09 × 0.990.0891
  • P(D|+) = 0.0090 / (0.0090 + 0.0891)0.0090 / 0.0981 ≈ 0.092

Pro tip. A positive result leaves the odds still roughly ten to one against being sick: positives from the huge healthy majority outnumber positives from the small sick minority. The test is good; the prior is doing the work.

Coding lab. Compute the posterior yourself runs in the app, with checks on your output.

A test comes back positive. Beyond how often the test fires for genuinely sick people, which quantity does Bayes rule say you still need?
  1. How common the illness is in the population being tested
  2. How many tests have already been run today
  3. How long the test takes to return its result
  4. Nothing further: the true-positive rate is already the answer

The posterior weighs the likelihood against the prior. Skip the prior and a rare disease with an excellent test still produces mostly false alarms.

3The odds form

Working engineers rarely divide by P(E); they use odds. Write belief as odds — the ratio of one hypothesis to the other — and Bayes rule collapses to a single multiplication: posterior odds = prior odds × likelihood ratio, where the likelihood ratio is P(E|H) divided by P(E|not H).

The normaliser vanishes because it multiplies both hypotheses equally, and independent pieces of evidence compose by multiplying their ratios. Take logarithms and the products become sums: a hypothesis accumulates additive evidence, which is exactly the shape logistic regression will give the score in the ML course.

The likelihood ratio is also a clean summary of test quality on its own. This screening test moves any prior by a factor of ten, whoever it is applied to — the ratio belongs to the test, the odds belong to the patient.

Figure. Prevalence 1% is prior odds 1 : 99. The screening test's likelihood ratio is 0.90 / 0.09 = 10. Posterior odds = (1 : 99) × 10 = 10 : 99, which is 10/109 ≈ 9.2% — the same posterior, one multiplication.

The same posterior, one multiplication

Redo the screening computation in odds. Prevalence 1% means prior odds of 1 : 99 in favour of sickness; the test's likelihood ratio is 0.90 / 0.09.

  • Likelihood ratio = 0.90 / 0.0910
  • Posterior odds = (1 : 99) × 1010 : 99
  • As a probability: 10 / (10 + 99)10/109 ≈ 0.092 — the same 9.2%

Pro tip. When two routes to a number disagree, one of them is wrong — so run both. The odds route has no denominator to fumble, which is why clinicians are taught it.

4Priors and likelihoods

The prior is what you believed before this piece of evidence arrived; the likelihood is how expected the evidence is under each hypothesis. They answer different questions, are measured by different people, and only the posterior belongs to the patient in front of you.

The classic error is reading the test's spec sheet as the patient's answer. Sensitivity P(+|D) is a property printed by the manufacturer; P(D|+) depends on who walked in the door. '99% of sick patients test positive' says nothing on its own about how likely a positive patient is to be sick.

Courtrooms make the same swap under a different name: the prosecutor's fallacy reads 'this match is very unlikely if innocent' as 'the accused is very unlikely to be innocent', silently dropping the prior over everyone else the evidence could have matched.

Where priors come from matters — prevalence tables, base rates in the training data, or an honest declared estimate. A posterior can never be better than the prior and likelihood that fed it.

Who owns which number
QuantityQuestion it answersIn the screening example
Prior P(D)How common is the condition among people like this?0.01
Likelihood P(+|D)How often does the test fire on the sick?0.90
Likelihood P(+|healthy)How often does it fire anyway?0.09
Posterior P(D|+)Given the positive, how likely is sickness?≈ 0.092
'99% of sick patients test positive, so a patient who tests positive is 99% likely to be sick.' What has been swapped?
  1. Nothing has been swapped: the two sentences say the same thing
  2. The prior has been substituted for the posterior odds
  3. The likelihood has been normalised twice over
  4. The chance of a positive given illness has been read as the chance of illness given a positive, and those diverge whenever the illness is rare

This is the classic reversal. Both conditionals are about the same two events, but the one you want conditions on what you observed, and getting there needs the prior.

5Base-rate neglect

The 9.2% surprises almost everyone the first time — trained clinicians included. The instinct is to hear 'the test is 90% accurate' and let that one number stand in for the answer; what it neglects is the base rate, the prior share of sick people among everyone being screened.

The posterior tracks the population as much as the test. Run the identical test where the condition affects 20% of those screened — a symptomatic clinic rather than mass screening — and the same positive now means 71%, not 9%.

This is why screening programmes confirm positives with a second, different test — and why an AI system's precision quoted on a balanced benchmark says little about deployment on rare events such as fraud at one transaction in a thousand. Same model, different prior, different posterior.

Figure. The identical test read in two populations: at 1% prevalence a positive means 9.2%; at 20% prevalence it means 71.4%. The bars differ by a factor of nearly eight, and the test's two error rates never changed.

Same test, different room

Move the test from mass screening (1% prevalence) to a symptomatic clinic where 20% of those tested have the condition. Sensitivity and false-positive rate are unchanged.

  • Sick and positive: 0.90 × 0.200.180
  • Healthy and positive: 0.09 × 0.800.072
  • P(D|+) = 0.180 / (0.180 + 0.072)0.180 / 0.252 ≈ 0.714

Pro tip. Nothing about the test changed — 90% sensitivity, 9% false alarms — yet the posterior moved from 9% to 71%. When a deployed model's precision collapses in production, ask what happened to the base rate before blaming the model.

6Evidence accumulates

Bayes rule chains: today's posterior is tomorrow's prior. A second, independent piece of evidence starts from where the first left off and multiplies in its own likelihood ratio — the update rule never changes, only the starting belief does.

Independence is the load-bearing word. A second test only moves the odds by its full factor if its errors are unrelated to the first test's; repeat the same assay on the same sample and you largely re-observe the same error, not new evidence. Deciding what counts as independent is a modelling judgement — and it is the exact judgement naive Bayes will make wholesale in the next lesson.

Run the numbers: one positive took the patient from 1% to 9.2%. A second independent positive multiplies the odds by ten again and lands near 50% — at which point a third, costlier definitive test is worth its price.

Figure. One positive took the patient from 1% to 9.2% (odds 10 : 99). A second independent positive multiplies the odds by 10 again: 100 : 99, about 50.3% — essentially a coin flip, and worth a definitive test.

The chain

  1. Posterior becomes priorAfter one positive: odds 10 : 99, about 9.2%.
  2. Multiply the new ratioA second independent positive multiplies the odds by 10 again.
  3. Read off the new beliefOdds 100 : 99 — essentially a coin flip, and worth a definitive test.

A second opinion, quantified

After one positive the odds are 10 : 99. An independent second test with the same likelihood ratio of 10 also comes back positive.

  • Posterior odds = (10 : 99) × 10100 : 99
  • As a probability: 100 / (100 + 99)100/199 ≈ 0.503
  • Belief moved 1% → 9.2% → 50.3%each positive multiplied the odds by 10

Pro tip. On the odds scale evidence is multiplicative, so on the log-odds scale it is additive — the same additive evidence scale a logistic model's score lives on.

Notes

  • Bayes rule as belief update: posterior is proportional to likelihood times prior, then renormalise.
  • Odds form: posterior odds = prior odds × likelihood ratio; independent evidence multiplies ratios.
  • Base rates dominate rare conditions: a 90%-sensitive test at 1% prevalence yields only a 9.2% posterior.

Formulas

  • P(H|E) = P(E|H)\,P(H) / P(E)
  • posterior odds = prior odds × likelihood ratio
  • LR = P(E|H) / P(E|\neg H)

Exam traps & shortcuts

  • Never read P(+|D) as P(D|+): the first is the test's spec sheet, the second is the patient's question, and they differ by the prior.
  • Check any posterior with the odds form: prior odds × likelihood ratio is one multiplication and hard to fumble.
  • A second opinion only counts in full if its errors are independent of the first — re-running the same test mostly re-observes the same error.

Recap

This lesson in brief:

Bayes rule
Posterior ∝ likelihood × prior, then renormalise — one joint probability read in two directions.
The odds form
Posterior odds = prior odds × likelihood ratio; independent evidence multiplies, log-odds add.
Base rates
The same positive means 9.2% at 1% prevalence and 71% at 20% — the posterior tracks the population as much as the test.
Evidence accumulates
Today's posterior is tomorrow's prior, but a second opinion only counts in full if its errors are independent of the first.

Practise Uncertainty and Bayes

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • 2 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.