1.3.17Probability & Statistics

Maximum a posteriori estimation (MAP)

2,790 words13 min readdifficulty · medium2 backlinks

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

Definition & Mathematical Foundation

Derivation from Bayes' Theorem

Let's build this from scratch using Bayes' theorem:

Step 1: Start with Bayes' rule for the posterior P(θx)=P(xθ)P(θ)P(x)P(\theta \mid \mathbf{x}) = \frac{P(\mathbf{x} \mid \theta) \cdot P(\theta)}{P(\mathbf{x})}

Why this form? We want to "flip" from P(xθ)P(\mathbf{x} \mid \theta) (the likelihood) to P(θx)P(\theta \mid \mathbf{x}) (the posterior). Bayes' theorem is the mathematical machinery for this inversion.

Step 2: Find the θ\theta that maximizes the posterior θ^MAP=argmaxθP(θx)=argmaxθP(xθ)P(θ)P(x)\hat{\theta}_{\text{MAP}} = \arg\max_{\theta} P(\theta \mid \mathbf{x}) = \arg\max_{\theta} \frac{P(\mathbf{x} \mid \theta) \cdot P(\theta)}{P(\mathbf{x})}

Why this step? We're looking for the peak of the posterior distribution—the single most likely parameter value.

Step 3: Drop the denominator (it's constant w.r.t. θ\theta) θ^MAP=argmaxθP(xθ)P(θ)\hat{\theta}_{\text{MAP}} = \arg\max_{\theta} P(\mathbf{x} \mid \theta) \cdot P(\theta)

Why can we drop it? P(x)P(\mathbf{x}) is the marginal likelihood (evidence), which doesn't depend on θ\theta. Since we're optimizing over θ\theta, constants don't affect the argmax location.

Step 4: Take logarithm for computational convenience θ^MAP=argmaxθ[logP(xθ)+logP(θ)]\hat{\theta}_{\text{MAP}} = \arg\max_{\theta} \left[ \log P(\mathbf{x} \mid \theta) + \log P(\theta) \right]

Why log? Products become sums (easier to optimize), and log is monotonic so it preserves argmax. This is the standard log-posterior.

Relationship to MLE

Maximum Likelihood Estimation (MLE) is a special case of MAP:

θ^MLE=argmaxθP(xθ)=argmaxθlogP(xθ)\hat{\theta}_{\text{MLE}} = \arg\max_{\theta} P(\mathbf{x} \mid \theta) = \arg\max_{\theta} \log P(\mathbf{x} \mid \theta)

Derivation: If we use a uniform prior P(θ)=constP(\theta) = \text{const}, then: θ^MAP=argmaxθ[logP(xθ)+log(const)]=argmaxθlogP(xθ)=θ^MLE\hat{\theta}_{\text{MAP}} = \arg\max_{\theta} \left[ \log P(\mathbf{x} \mid \theta) + \log(\text{const}) \right] = \arg\max_{\theta} \log P(\mathbf{x} \mid \theta) = \hat{\theta}_{\text{MLE}}

Why? A uniform prior means "all parameter values are equally likely before seeing data." The prior contributes nothing to the optimization, so we're back to pure likelihood maximization.

Key insight: MAP generalizes MLE by allowing non-uniform priors. MLE is MAP with no prior knowledge.

Worked Examples

Common Mistakes

MAP vs MLE vs Full Bayes

Method What it gives Formula Use when
MLE Single point estimate argmaxP(xθ)\arg\max P(\mathbf{x} \mid \theta)
MAP Single point estimate argmaxP(θx)\arg\max P(\theta \mid \mathbf{x})
Full Bayes Entire distribution P(θx)P(\theta \mid \mathbf{x})

Why MAP?

  • More robust than MLE with small data (prior regularizes)
  • Computationally simpler than full Bayes (no integration)
  • Natural connection to regularization in optimization

Connection to Regularization (Deep Dive)

The negative log-posterior is a regularized loss:

logP(θx)=logP(xθ)logP(θ)+const-\log P(\theta \mid \mathbf{x}) = -\log P(\mathbf{x} \mid \theta) - \log P(\theta) + \text{const}

Minimizing this (equivalent to maximizing posterior) gives:

θ^MAP=argminθ[logP(xθ)Data loss+(logP(θ))Regularizer]\hat{\theta}_{\text{MAP}} = \arg\min_{\theta} \left[\underbrace{-\log P(\mathbf{x} \mid \theta)}_{\text{Data loss}} + \underbrace{(-\log P(\theta))}_{\text{Regularizer}}\right]

Common correspondences:

  • Gaussian prior N(0,σ2)N(0, \sigma^2) ⟷ L2 regularization (λ=1/(2σ2)\lambda = 1/(2\sigma^2))
  • Laplace prior Laplace(0,b)\text{Laplace}(0, b) ⟷ L1 regularization (λ=1/b\lambda = 1/b)
  • Uniform prior ⟷ No regularization (MLE)

Why this is powerful: Every regularization scheme can be interpreted as a prior belief about parameters. Choosing regularization = choosing a prior.

Recall Explain Like I'm 12

Imagine you're trying to guess how many candies are in a jar. Your friend says "I counted 47 candies through the glass" (this is your data). But you also know that the jar usually holds about 50 candies when it's full (this is your prior knowledge).

MLE would say: "Trust the count completely—guess 47."

MAP would say: "The count is probably close, but the jar is usually around 50, so maybe the count was off by a bit. I'll guess 48 or 49—something between what I saw and what I expected."

MAP combines what you see (the data) with what you already know (the prior). It's like asking two experts and finding a smart compromise between them. The more confident each expert is, the more you listen to them. If you've seen 1000 candies clearly, you trust the data more. If you only glimpsed a few, you trust your prior knowledge more.

Active Recall Challenges

Answers (try first!):

  1. (See derivation section above)
  2. Uniform prior; because logP(θ)=const\log P(\theta) = \text{const} contributes nothing to argmax
  3. Posterior is Beta(2+2, 2+1) = Beta(4,3), mode = (4-1)/(4+3-2) = 3/5 = 0.6
  4. L2 regularization λθ2\lambda\|\theta\|^2 = MAP with Gaussian prior N(0,1/(2λ))N(0, 1/(2\lambda))
  5. Data term nn\to\infty dominates prior term → MAP → MLE (prior washes out)

Connections

  • Maximum Likelihood Estimation (MLE) — MAP generalizes this by adding priors
  • Bayes Theorem — The foundation for deriving MAP
  • Conjugate Priors — Make MAP computationally tractable
  • Regularization in ML — Equivalent to MAP with specific priors
  • Beta Distribution — Common prior for probabilities
  • Posterior Distribution — MAP finds its mode
  • Overfitting — MAP prevents this better than MLE with small data
  • Fisher Information — Used in Jeffreys prior for reparameterization invariance
  • Ridge Regression — MAP with Gaussian prior on coefficients
  • Lasso Regression — MAP with Laplace prior on coefficients

#flashcards/ai-ml

What is the MAP estimation formula in terms of log-posterior? :: θ^MAP=argmaxθ[logP(xθ)+logP(θ)]\hat{\theta}_{\text{MAP}} = \arg\max_{\theta} \left[ \log P(\mathbf{x} \mid \theta) + \log P(\theta) \right] — sum of log-likelihood and log-prior

How does MAP relate to MLE? :: MLE is MAP with a uniform prior: when P(θ)=constP(\theta) = \text{const}, the prior term vanishes and MAP reduces to MLE

In MAP estimation, why can we drop P(x)P(\mathbf{x}) from Bayes' theorem?
Because P(x)P(\mathbf{x}) doesn't depend on θ\theta—it's a constant w.r.t. the optimization variable, so it doesn't affect the argmax
For coin flips with kk heads in nn flips and Beta(α\alpha, β\beta) prior, what is the posterior?
Beta(α+k\alpha + k, β+nk\beta + n - k) — add observed heads to α\alpha, observed tails to β\beta
What is the mode of a Beta(α\alpha, β\beta) distribution?
α1α+β2\frac{\alpha - 1}{\alpha + \beta - 2} for α,β>1\alpha, \beta > 1 — found by setting derivative of log-PDF to zero
What type of regularization corresponds to a Gaussian prior in MAP?
L2 regularization — Gaussian prior N(0,σ2)N(0, \sigma^2) gives regularization term θ22σ2\frac{\|\theta\|^2}{2\sigma^2}
What type of regularization corresponds to a Laplace prior in MAP?
L1 regularization — Laplace prior gives regularization term λθ1\lambda\|\theta\|_1
Does MAP give the mean or mode of the posterior?
Mode — MAP finds the parameter value with highest posterior probability, not the average
What happens to MAP estimate as sample size approaches infinity?
MAP converges to MLE — the data term grows with nn while prior term stays constant, so prior influence vanishes
For Gaussian data with Gaussian prior, is MAP estimate closer to sample mean or prior mean?
A weighted average — specifically μ^MAP=nσ02nσ02+σ2xˉ+σ2nσ02+σ2μ0\hat{\mu}_{\text{MAP}} = \frac{n\sigma_0^2}{n\sigma_0^2 + \sigma^2}\bar{x} + \frac{\sigma^2}{n\sigma_0^2 + \sigma^2}\mu_0
Why is Beta distribution a conjugate prior for binomial likelihood?
Because Beta prior × Binomial likelihood = Beta posterior — the posterior stays in the same family, making computation analytical
What is the relationship between negative log-posterior and regularized loss?
They are equivalent: logP(θx)=data loss+regularization term-\log P(\theta \mid \mathbf{x}) = \text{data loss} + \text{regularization term}
If you use MAP with Beta(1,1) prior for coin bias, what estimate do you get?
Exactly the MLE — Beta(1,1) is uniform on [0,1], equivalent to no prior information
What is Jeffreys prior and why use it?
P(θ)I(θ)P(\theta) \propto \sqrt{|I(\theta)|} where II is Fisher information — it's invariant under reparameterization, making it a truly non-informative prior

For Gaussian mean estimation, how does prior variance σ02\sigma_0^2 affect MAP? :: Larger σ02\sigma_0^2 means less confident prior, so MAP relies more on data and approaches MLE; smaller σ02\sigma_0^2 pulls estimate toward prior mean

Concept Map

defines

proportional to

proportional to

normalized by

maximizes

dropped as constant

log transform gives

log-likelihood + log-prior

uniform reduces MAP to

special case of

injects knowledge

Bayes Theorem

Posterior P theta given x

Likelihood P x given theta

Prior P theta

Evidence P x

MAP Estimate

Log-Posterior

MLE Estimate

Prevents Overfitting

Hinglish (regional understanding)

Intuition Hinglish mein samjho

MAP Estimation ka Intuition

Dekho, jab bhi hum kisi parameter ka estimate karna chahte hain (jaise coin ke bias ya kisi distribution ka mean), toh humein do chezein consider karni padti hain: pehli, jo data humein dikha raha hai (likelihood), aur dosri, jo hum pehle se jante the (prior belief). Maximum Likelihood (MLE) sirf data ko dekhta hai, lekin MAP dono ko balance karta hai — ye Bayesian approach hai.

Samjho ek example se: tumhare pas ek coin hai aur tum 10 baar flip karte ho, 7 baar heads ata hai. MLE kehta hai bias = 7/10 = 0.7. Lekin agar tumhe pehle se pata tha ki ye coin thoda biased hai (prior knowledge), toh MAP us information ko bhi use karega. Agar prior kehta hai "I think bias 0.6 ke as-paas hoga", toh MAP final estimate ko thoda 0.6 ki taraf pull karega — result ayega around 0.69. Jaise jaise zyada data milta hai (100 flips

Go deeper — visual, from zero

Test yourself — Probability & Statistics

Connections