1.3.17 · D2Probability & Statistics

Visual walkthrough — Maximum a posteriori estimation (MAP)

2,470 words11 min readBack to topic

#ai-ml/probability-statistics #bayesian-inference #parameter-estimation

Before any math: a parameter is just an unknown number we want to guess. Call it (the Greek letter "theta"). For a coin, is the chance it lands heads — a number between and . That is all means. Everything below is about picking the best single value for that unknown number.


Step 1 — Draw the thing we are guessing

WHAT. Put on a horizontal line. Every point on that line is one possible guess for the coin's bias: (never heads), (fair), (always heads).

WHY. You cannot talk about "the best guess" until you can see all the guesses laid out. A picture of the parameter line is the stage on which the whole story happens.

PICTURE. The amber tick at marks a fair coin. We have not yet decided which value wins — the line is just the menu of options.

Figure — Maximum a posteriori estimation (MAP)

Step 2 — Height = "how believable". Two curves appear

WHAT. Above the line we draw heights. A tall height over some means "that value is believable"; a short height means "unlikely". Two different curves live here.

  • The likelihood — read "probability of the data , if the true value were ". Here is what we actually saw (7 heads in 10 flips). This curve peaks where the data is best explained.
  • The prior — read "how believable was before we saw any data". This is your common sense, drawn as a curve.

WHY. MLE (Maximum Likelihood Estimation (MLE)) only ever looks at the likelihood curve. But real belief has two sources — the data and what you knew before. We need both curves on the stage before we can combine them.

PICTURE. Cyan = likelihood (peaks near , the data's story). Amber = prior (peaks near , our earlier hunch). Notice their peaks are in different places — that tension is the whole point.

Figure — Maximum a posteriori estimation (MAP)

Let us write both curves down explicitly, because in Step 6 their exponents appear and you deserve to know where every number came from.

The likelihood for heads in flips is the binomial probability:

  • — the number of different ways 7 heads can land among 10 flips. It contains no , so it is a fixed constant .
  • — each head happens with probability , and there are 7 of them, multiplied together.
  • — each tail happens with probability , and there are 3.

The prior we pick is a Beta curve (see Beta Distribution). Its functional form is

  • The two shape numbers of Beta are and ; the exponents in the formula are always one less, giving and . That is where the "2" and the "1" you will see in Step 6 are born.
  • — a normalising constant with no in it, so it too is a fixed number we can later drop.

Step 3 — Bayes' theorem: flip the arrow

WHAT. We can compute (probability of data given a guess), but we actually want (probability of a guess given the data). Bayes' theorem flips this arrow:

WHY. The quantity on the left, the posterior , is exactly "how believable is after seeing the data". That is the curve whose peak we want. Bayes is the only lawful way to turn the two ingredients of Step 2 into it.

PICTURE. Think of it as a factory: two input curves go in the top (likelihood prior, multiplied height-by-height at each ), the evidence divides everything by one fixed number, and the posterior comes out the bottom.

Figure — Maximum a posteriori estimation (MAP)

Each symbol, right where it sits:

  • output, the posterior height at .
  • numerator, likelihood height times prior height, multiplied pointwise.
  • denominator, the total probability of the data added over all ; it does not contain , so it is one constant number.

Step 4 — Throw away the denominator (it never moves the peak)

WHAT. We are hunting the location of the tallest point, written — "the that makes the following as large as possible". Dividing every height by the same constant shrinks the whole curve uniformly but does not slide its peak sideways.

WHY. We want where the peak is, not how tall. Scaling by a positive constant is like zooming a photo — nothing horizontal changes. So the evidence, which cost us nothing to ignore, is dropped. The same reasoning also lets us drop and from Step 2 — both are -free constants. What survives is only the -carrying part: Multiplying powers of the same base adds their exponents: heads-worth, tails-worth. This combined curve is a Beta shape — the posterior.

PICTURE. The tall cyan curve and the short cyan curve are the same shape scaled; the amber dashed line drops from both peaks to the same . The peak's address is unchanged.

Figure — Maximum a posteriori estimation (MAP)

Step 5 — Take logs: multiply becomes add

WHAT. Apply (the logarithm) to the thing we maximize. The key property: . A product turns into a sum. Applied to our surviving curve : The rule pulls the exponents and out to the front — that is where the coefficients in Step 6 come from.

WHY two reasons.

  1. Sums are easier than products — the derivative of a sum is the sum of derivatives, so Step 6's calculus is clean.
  2. is monotonic (always increasing): if curve is taller than curve at some point, then is still taller than there. So the peak's location never moves when we take logs — exactly what we needed in Step 4's spirit.

PICTURE. Left panel: the original product-shaped curve. Right panel: its log — same peak position (amber line lines up), but the shape is now a sum of two gentle bowls, one from the data, one from the prior.

Figure — Maximum a posteriori estimation (MAP)

Step 6 — Find the peak: slope zero

WHAT. A smooth curve's highest point is where its slope is zero — flat on top. The slope is the derivative . Set it to zero and solve.

From Step 5 the log-posterior is (the Beta shape we assembled). Differentiate, using and :

WHY. At a hilltop you are momentarily walking on level ground — the slope is . Solving is the standard way calculus locates a maximum. The two fractions are literally two forces: one pushes up (evidence for heads), one pulls it down (evidence for tails). Balance = the estimate.

Solve it:

PICTURE. The posterior curve with a flat amber tangent line resting on its top, dropping to . Compare the cyan MLE peak at : the prior nudged the answer leftward, toward its own hunch of .

Figure — Maximum a posteriori estimation (MAP)

The prior mode of Beta is at ; the data alone says ; MAP lands between them at — closer to the data because there is more of it. (See Conjugate Priors, Posterior Distribution.)


Step 7 — Edge case A: a flat prior gives back MLE

WHAT. Suppose the prior is flat — every equally believable, . Then is a constant, and a constant has slope zero everywhere.

WHY. A flat prior means "I believe nothing in advance." Adding a level slab to the log-posterior lifts it uniformly but never tilts it, so the peak stays exactly where the likelihood put it. This is why MLE is MAP with an empty mind.

PICTURE. The amber flat prior is a straight horizontal slab; the posterior peak sits directly under the cyan likelihood peak — MAP and MLE coincide.

Figure — Maximum a posteriori estimation (MAP)

Step 8 — Edge case B: as data floods in, the prior fades

WHAT. We now switch to estimating a Gaussian mean , and derive its MAP formula rather than quoting it, so no symbol appears unearned. We observe numbers drawn from a bell curve (variance known and equal to 1), and our prior is a bell curve — belief centred at with spread .

The likelihood (product over i.i.d. points, constants dropped) has log-form Each term is the log of the bell ; sums replace products because logs turn into .

The prior has log-form The "" is ; the pulls toward , the prior's centre.

Add them, differentiate, set to zero (using ): Write (the data average), so :

WHY this shape. The prior acts like a fixed dab of pretend evidence worth about a quarter of a data point. When real data is scarce, that dab is loud; when data floods in, it is drowned out:

  • : — heavily shrunk toward the prior mean .
  • : , so — the prior vanishes and MAP becomes MLE.

This shrinking pull is exactly what prevents Overfitting on tiny datasets — the same idea as Regularization in ML, Ridge Regression and Lasso Regression (a Gaussian prior is L2, a Laplace prior is L1). See also Fisher Information for how data "sharpens" the likelihood.

PICTURE. Three posteriors stacked for : as grows, the peak marches from near out to , and the curve narrows — belief tightens onto the data.

Figure — Maximum a posteriori estimation (MAP)

The one-picture summary

Everything above, in a single frame: two curves (data in cyan, belief in amber) get multiplied, logs turn that into an addition of two scores, and we walk uphill to the flat top. The peak is a tug-of-war winner — pulled toward the data by evidence, toward the prior by belief, settling wherever the two forces balance.

Figure — Maximum a posteriori estimation (MAP)
Recall Feynman retelling — say it like a story

I want to guess one unknown number . I draw a line of all its possible values. Over that line I sketch two hills: one is how well each value explains my data, the other is how much I believed in each value beforehand. Bayes' theorem tells me to multiply the two hills together (and divide by a constant I can ignore, because it only shrinks, never shifts). Multiplying is annoying, so I take logs and it becomes adding two scores. The best guess is the top of the summed hill, found where the slope goes flat. If I believed nothing (flat prior), the belief-hill is level and I'm back to plain likelihood — that's MLE. If I have loads of data, the data-hill towers over the belief-hill and again I basically get MLE. In between — little data, real belief — MAP quietly pulls my guess toward common sense, which is the whole reason it beats overfitting.

Recall Quick self-test

Why can we drop when finding the MAP estimate? ::: It does not depend on ; dividing every height by the same constant scales the curve but never moves its peak. Where do the exponents and in the log-posterior come from? ::: From adding the likelihood exponents and the Beta prior exponents : and . What does MAP reduce to under a uniform prior, and why? ::: MLE — a constant log-prior adds a level slab that lifts the log-posterior without tilting it, so the peak stays at the likelihood's peak. In the coin example, why is between the prior mode and the MLE ? ::: The posterior multiplies both influences; with 10 flips the data outweighs the prior slightly, so the answer sits closer to . As in the Gaussian case, what happens to the prior's influence? ::: It fades to zero (), so , the plain data mean.