Uncertainty/Bayes' Rule and Naive Bayes

Lesson 4.22,087 words

Bayes' Rule and Naive Bayes

Bayes' rule inverts a causal model into a diagnostic one, turning "how a cause produces its symptoms" into "which cause explains what I observed. " Ignoring the prior is the base-rate fallacy behind overconfident test results.

╌╌╌╌

This builds on Quantifying Uncertainty, which laid out degrees of belief, the full joint distribution, and plain independence. There we left off needing a subtler tool: a way to turn a model of how causes produce effects into an answer about which cause is present, and a way to fold in several observations at once without the joint distribution exploding. Both come from one equation and one refinement of independence.

Bayes' rule

Write the product rule both ways — and — set the right-hand sides equal, and divide. The result is Bayes' rule.

On its face Bayes' rule computes one conditional from three other numbers, which looks like a step backwards. It matters in a specific, common situation: we perceive an effect and want to infer its unknown cause. Rewriting the rule with those roles,

The quantity is causal knowledge — how the world works, how a cause produces its symptoms. The quantity we want, , is diagnostic — reasoning from symptom back to cause. Bayes' rule turns the first into the second.

Bayes' rule inverts the causal direction. A model gives (how a cause produces symptoms); Bayes' rule turns it into the diagnostic we need, weighted by the prior .

For example, take the medical version. Meningitis () causes a stiff neck () about of the time, so is the doctor's causal knowledge. The priors are and . Then

Fewer than in stiff-necked patients has meningitis — even though meningitis strongly indicates a stiff neck, the disease stays rare because its prior is tiny compared with the prior on stiff necks. The prior matters: dropping it (a common intuitive error) inflates the estimate by orders of magnitude.

Why prefer Bayes' rule to just storing the diagnostic number directly? Because causal knowledge is more stable than diagnostic knowledge. If an epidemic strikes, the prior jumps, and with it the correct posterior . A doctor who memorized from pre-epidemic statistics has no way to update it. A doctor who computes it from , , and sees at once that it should rise in step with — because the causal fact , how meningitis affects the body, is unchanged by the epidemic.1

If we do not have we can avoid it by computing the posterior for both values of the query and normalizing, exactly as in the joint-table example:

A worked medical test: the base-rate trap

The meningitis numbers were handed to us. In practice the causal quantities come from a diagnostic test described by two rates, and the arithmetic exposes a common error. Suppose a disease has prevalence — one person in a thousand has it. A test for it has sensitivity (true-positive rate) : it catches of sick people. Its specificity (true-negative rate) is , so its false-positive rate is . You test positive. What is the probability you are sick, ?

The reflex answer is about — the test is accurate. It is wrong by almost two orders of magnitude, and Bayes' rule shows why. Compute the two unnormalized terms:

Now normalize over the two values of ; their sum is :

A positive result raises the probability of disease from to about — higher than the prior, but nowhere near . The reason is entirely in the base rate. The disease is so rare that the false-positive rate, applied to the huge healthy majority, produces about ten times as many false alarms as there are true positives. Reasoning with a concrete population makes this unmistakable: out of people, are sick and of them test positive; of the healthy, — about — also test positive. So people test positive, and only of them are sick: , matching the Bayes computation exactly.

The base-rate trap as a population of 100,000. Only 100 are sick (left), and 99 of them test positive; but 999 of the 99,900 healthy also test positive. Among the 1098 positives, only 99 are truly sick, so P(disease given positive) is about 9 percent, not 99 percent.

This is the general shape of screening: a test can be excellent (high sensitivity and specificity) and a positive result still mean the patient probably does not have the disease, whenever the disease is rare enough. Dropping the prior — treating as if it equalled the sensitivity — is the base-rate fallacy, the very mistake Bayes' rule forces you not to make: the prior appears explicitly in the numerator and again inside the normalizer. The same computation governs spam filters, fraud alarms, and any rare-event detector: the rarer the event, the more the base rate dominates the test.

Combining evidence: conditional independence and naive Bayes

Bayes' rule handles one piece of evidence cleanly. But real diagnosis piles up symptoms — a toothache and a catch and an X-ray shadow — and the naive way of folding them all in brings back the exponential table we were trying to escape. The fix is the conditional independence promised at the end of the previous lesson: once the common cause is fixed, the symptoms stop informing each other, and the evidence combines by simple multiplication. Consider a dentist who observes both a toothache and a catch and wants . Bayes' rule reformulates it as — but now we need the joint likelihood of the two symptoms for each value of , and with symptoms there are such combinations to store. We are back to an exponential table. This is what led researchers away from probability theory in the first place: combining evidence naively seems to require a full joint over all the evidence.

The solution is a refinement of independence. and are not independent — both are more likely when there is a cavity. But they are independent once we fix whether there is a cavity. The cavity is the common cause of both; a toothache reflects the state of the nerve, a catch reflects the geometry the probe meets, and given the cavity neither tells us anything further about the other.

Conditional independence, not marginal independence. Toothache and Catch are correlated on their own because each signals a cavity (left); but once the cavity is fixed, the common cause is removed and the two become independent (right). Fixing Cavity blocks the only path between them.

With , the troublesome joint likelihood factors, , and the query collapses to a product of per-symptom terms:

Each symptom now contributes its own factor, and we never build the exponential joint. This pattern — one cause directly influencing several effects that are conditionally independent given the cause — is common enough to have a name.

The naive Bayes model. One cause points to each effect ; the effects are conditionally independent given , so the joint is times a product of the per-effect terms . The representation grows as , not .

Carry real numbers through to see the factoring work. Suppose , and given a cavity the two symptoms have and ; without a cavity, and . Observing both symptoms, the two unnormalized terms are

Normalizing, . Two mutually reinforcing symptoms push the belief from a prior of to about — and we combined them by multiplying two one-symptom tables, never building the joint over both. Each symptom multiplied in its own likelihood ratio: the toothache contributed , the catch , so together they shifted the prior odds to posterior odds , i.e. a probability of . The odds form of Bayes' rule shows exactly why conditionally independent evidence combines by multiplying likelihood ratios.

Odds form of Bayes' rule with conditionally independent evidence. The prior odds are multiplied by one likelihood ratio per symptom; for the dentistry numbers, 0.25 times 6 times 4.5 gives posterior odds 6.75, a probability of about 0.87.

The saving is substantial. For effects conditionally independent given the cause, the representation needs one prior and conditional tables — size rather than . Conditional independence, unlike absolute independence, is common: most large probabilistic domains decompose into weakly connected pieces because each variable is directly affected by only a few others. This decomposition of a huge joint into small, locally connected factors is one of the most important developments in the recent history of AI.2

The naive Bayes model appears throughout applied AI. In natural-language processing it is the classic naive Bayes text classifier: the document's sentiment is the hidden cause, each word an effect assumed conditionally independent of the others given the sentiment, and a document is scored by the same product . The independence assumption is false — words are correlated — yet the classifier is hard to beat for the effort.

Toward Bayesian networks

The naive Bayes model is the simplest interesting case of a general idea: represent a joint distribution not as one exponential table but as a product of small local factors, each capturing how a variable depends on a handful of others. Naive Bayes hard-codes a single common cause with conditionally independent effects. Lift that restriction — let each variable depend on an arbitrary small set of parents, with conditional independencies read off the graph — and you get a Bayesian network: a directed graph whose structure encodes the conditional independencies, plus a small conditional table at each node.

The full joint factors along the graph, , so a domain that would need numbers as a flat table often needs only a few dozen. That is the compact, factored representation the next lesson develops in full, and the engine behind reasoning over time and the decision-theoretic agents that close the module.

Probabilistic programming and Bayesian ML

Inference by enumeration is , and even the Bayesian networks of the next lesson are worst-case intractable to solve exactly. The developments that made probabilistic reasoning practical at scale, after AIMA's third edition, share a single move: give up on exact summation and sample instead, or approximate the posterior with optimization.

Approximate inference by sampling. Rather than sum every entry of a joint, draw many random possible worlds consistent with the evidence and estimate the posterior as a frequency. Markov-chain Monte Carlo methods make this efficient even in high dimensions; Hamiltonian Monte Carlo and its self-tuning variant the No-U-Turn Sampler (Hoffman & Gelman, JMLR 2014) are the samplers inside most modern Bayesian toolkits. For posteriors where sampling is too slow, variational inference turns the problem into optimization — fit a simple distribution to the true posterior by minimizing a divergence — and stochastic variants scale it to large data (Hoffman et al., Stochastic Variational Inference, JMLR 2013).

Probabilistic programming. The idea that unifies these is to treat a probabilistic model as a program: you write down the generative story in a language with random primitives, and a general-purpose inference engine computes posteriors over the program's latent variables. Stan (Carpenter et al., Journal of Statistical Software 2017) compiles a model to a tuned HMC sampler; Pyro (Bingham et al., JMLR 2019) builds probabilistic programs on top of a deep-learning framework so that neural networks and probabilistic models compose. The naive Bayes model of this lesson is one line in such a language; the same engine then handles models far too large to enumerate by hand.

Two escapes from exact inference. Exact enumeration sums all 2^n worlds and does not scale; sampling (MCMC) estimates the posterior from random draws, and variational inference fits a simple distribution q to the true posterior p by optimization. Both trade exactness for tractability.

The Bayesian view in machine learning. The same calculus reframes learning itself. A Bayesian treats model parameters as a random variable with a prior ; data updates it to a posterior — Bayes' rule again, with parameters as the cause and data as the effect. Predictions average over the posterior instead of committing to one parameter setting, which yields calibrated uncertainty rather than a bare point estimate (MacKay's 1992 work on Bayesian neural networks is the early reference; the idea is standard in modern probabilistic ML). This is the direct ancestor of the probabilistic-learning methods later in the course, which is why the material here is foundational.3

Two cautions apply. Approximate methods trade a guarantee for speed: a sampler that has not mixed, or a variational family too simple to fit the true posterior, returns a confident but wrong answer, so diagnostics matter. And the choice of prior is a modeling decision, not a fact read off the data — a poorly chosen prior biases the posterior, which is a feature (it lets you inject knowledge) and a hazard (it lets you inject error) at once.

Probability completes the account of rational agency. Logic gave the agent a way to reason when it knew the rules exactly. Probability gives it the calculus of rational belief when it does not — degrees of belief that obey the axioms, updated by conditioning and Bayes' rule as evidence arrives, factored by independence into something an agent can actually compute.

Footnotes

  1. Russell & Norvig, §13.5.1 — Applying Bayes' rule: the meningitis/stiff-neck example, and why causal knowledge is more robust than memorized diagnostic knowledge under changes in the prior.
  2. Russell & Norvig, §13.5.2 — Using Bayes' rule and combining evidence; conditional independence, the naive Bayes model , and the -versus- saving from decomposition.
  3. M. Hoffman & A. Gelman, The No-U-Turn Sampler, JMLR 15 (2014), and M. Hoffman et al., Stochastic Variational Inference, JMLR 14 (2013) — scalable approximate posterior inference by sampling and by optimization. B. Carpenter et al., Stan: A Probabilistic Programming Language, Journal of Statistical Software 76 (2017), and E. Bingham et al., Pyro: Deep Universal Probabilistic Programming, JMLR 20 (2019) — probabilistic programming systems with general-purpose inference engines. D. MacKay, A Practical Bayesian Framework for Backpropagation Networks, Neural Computation 4 (1992) — the Bayesian treatment of model parameters as a posterior updated by Bayes' rule.

╌╌ END ╌╌