Memra

Bayes’ theorem, MAP, and naive Bayes

◈ 4 cards

Bayes’ theorem with normalization, the maximum-a-posteriori (MAP) estimate, the naive conditional-independence assumption, and why full Bayesian inference is intractable.

Bayes’ theorem: inverting a conditional

We can usually measure how often the evidence shows up when a hypothesis is true — but we want how likely the hypothesis is given the evidence we saw. Bayes’ theorem inverts the conditional: for a hypothesis drawn from a mutually exclusive, exhaustive set ,

Name every part — examiners ask for this:

  • — the posterior: what we want, our belief after seeing .
  • — the likelihood: how well predicts the evidence (usually measurable from data).
  • — the prior: belief before the evidence (e.g. a disease’s base-rate prevalence).
  • — the normalizing constant, the total probability of the evidence across all hypotheses, which forces the posteriors to sum to 1.

It is powerful because the easy-to-collect quantity (how often meningitis patients have headaches) yields the hard-to-collect one (what fraction of headache patients have meningitis).

MAP: drop the denominator

If all we want is the most likely hypothesis (not its exact probability), the denominator is identical for every , so it can be dropped. What remains is the maximum a posteriori (MAP) estimate: The result is no longer a true probability (it won’t sum to 1), but it correctly identifies the winner — and it is used pervasively in classification, speech recognition, and NLP.

The independence trick: naive Bayes

With several pieces of evidence , the joint likelihood needs a table that grows exponentially in . Naive Bayes makes the simplifying assumption that the features are conditionally independent given the class: This turns an exponential table into a product of small per-feature estimates — linear in . Plugging that into MAP gives the classifier The assumption is almost never literally true (symptoms correlate), yet the classifier works remarkably well — Domingos and Pazzani showed it only needs the ranking of hypotheses to be right, not the exact probabilities, and the ranking often survives even when independence is violated.

Worked example — a tiny spam classifier

Train on a few labeled messages, count how often each word appears in spam vs ham, add Laplace (+1) smoothing so an unseen word doesn’t zero out the whole product, then for a new message multiply the class prior by the per-word likelihoods and take the argmax. For the input "cheap meds now", the spam-laden words dominate and the model returns spam with a high posterior. The code exercise builds exactly this and prints the prediction with its normalized posterior.

class hprior P(h)cheapP(cheap|h)medsP(meds|h)nowP(now|h)No edge joins two words — that absence IS the naive assumption.
Naive Bayes drawn as a structure. Every arrow leaves the class; no arrow joins two words. That <em>missing</em> edge is the conditional-independence assumption itself, and it is what replaces one exponential joint likelihood with the product P(h)·P(cheap|h)·P(meds|h)·P(now|h) — linear in the number of features.
NORMAL ~/memra/learn/comp-456/bayes-theorem-map-naive-bayes utf-8 LF