MoonSay /Tutorials/Machine Learning Interview #5
Contents
  1. 01Introduction
  2. 02Maximum likelihood
  3. 03Where maximum likelihood fails
  4. 04Bayes’ theorem and the MAP estimate
  5. 05The seesaw
  6. 06A prior is a regularizer
  7. 07Side by side
  8. 08Interview questions and model answers
  9. 09Summary
  10. References

04Bayes’ theorem and the MAP estimate

4.1Bayes’ theorem, term by term

Bayes’ theorem is the foundation of MAP. It follows from writing the joint probability of \theta and D in two ways, p(\theta,D)=p(D\mid\theta)\,p(\theta)=p(\theta\mid D)\,p(D), and dividing by p(D):

\underbrace{p(\theta\mid D)}_{\text{posterior}}=\frac{\overbrace{p(D\mid\theta)}^{\text{likelihood}}\ \overbrace{p(\theta)}^{\text{prior}}}{\underbrace{p(D)}_{\text{evidence}}},\qquad p(D)=\int p(D\mid\theta)\,p(\theta)\,d\theta . \tag{6}

Each term has a plain meaning. The prior p(\theta) is what we believe about \theta before seeing the data. The likelihood p(D\mid\theta) is how well each value of \theta explains the data, the quantity maximum likelihood maximizes. The evidence p(D) is the total probability of the data, averaged over all \theta; it is the same number for every \theta and only makes the posterior integrate to one. The posterior p(\theta\mid D) is what we believe after seeing the data. In words: posterior \propto likelihood \times prior.

Worked example 3 (A rare disease). A disease affects 1 person in 1{,}000. A test always detects it, but it also comes back positive for 5\% of healthy people. You test positive. What is the probability that you have the disease? This is the question Casscells et al. (1978) put to 60 physicians and medical students. With S for sick and + for a positive test, Bayes’ theorem (6) gives

\begin{aligned} P(S\mid+)&=\frac{P(+\mid S)\,P(S)}{P(+\mid S)\,P(S)+P(+\mid\text{healthy})\,P(\text{healthy})}\\[2pt] &=\frac{1\times0.001}{1\times0.001+0.05\times0.999}=\frac{0.001}{0.05095}\approx0.0196 . \end{aligned}

Only about 2\%, not 95\%. Figure 2 shows why: among 1{,}000 people, 1 is sick and tests positive, while about 50 healthy people also test positive, so a positive result means sick only about 1 time in 51. The prior, the rarity of the disease, dominates the evidence of one imperfect test. If the disease affected 10\% of the people tested, the same positive result would mean P(S\mid+)\approx0.69.

Bayes’ theorem with natural frequencies: 1{,}000 people tested, prevalence 1/1000, false-positive rate 5\%. One square is one person. Of the about 51 positive tests, only 1 comes from a sick person. Illustration; the positions of the false positives are arbitrary. Numbers computed for this tutorial; the question follows Casscells et al. (1978).
Figure 2. Bayes’ theorem with natural frequencies: 1{,}000 people tested, prevalence 1/1000, false-positive rate 5\%. One square is one person. Of the about 51 positive tests, only 1 comes from a sick person. Illustration; the positions of the false positives are arbitrary. Numbers computed for this tutorial; the question follows Casscells et al. (1978).

4.2MAP: the peak of the posterior

Definition 2 (Maximum a posteriori estimate). The MAP estimate is the most probable parameter value after seeing the data, the mode of the posterior:

\hat\theta_{\mathrm{MAP}}=\arg\max_\theta\ p(\theta\mid D)=\arg\max_\theta\ p(D\mid\theta)\,p(\theta). \tag{7}

The evidence p(D) was dropped because it does not depend on \theta.

Taking logarithms, as for maximum likelihood,

\hat\theta_{\mathrm{MAP}}=\arg\max_\theta\Big[\underbrace{\sum_{i=1}^n\log p(x_i\mid\theta)}_{\text{the MLE objective}}+\underbrace{\log p(\theta)}_{\text{log-prior}}\Big]. \tag{8}

This is the whole difference between the two estimators: MAP is the maximum likelihood objective plus one extra term. With a flat prior, \log p(\theta) is a constant and MAP equals maximum likelihood.

4.3A prior is imaginary data: the Beta prior

For a probability \theta\in[0,1], the natural prior is the Beta distribution,

p(\theta)=\mathrm{Beta}(\theta\mid\alpha,\beta)=\frac{\theta^{\alpha-1}(1-\theta)^{\beta-1}}{B(\alpha,\beta)},\qquad\alpha,\beta>0,

where B(\alpha,\beta) only normalizes. Multiplying by the coin likelihood \theta^h(1-\theta)^{n-h} gives

p(\theta\mid D)\ \propto\ \theta^{h+\alpha-1}(1-\theta)^{n-h+\beta-1},\qquad\text{that is,}\qquad \theta\mid D\sim\mathrm{Beta}(h+\alpha,\ n-h+\beta). \tag{9}

The posterior is again a Beta distribution: the Beta prior is conjugate to the coin likelihood. Its mode is found exactly like (3), with h+\alpha-1 in place of h and n+\alpha+\beta-2 in place of n:

\hat\theta_{\mathrm{MAP}}=\frac{h+\alpha-1}{n+\alpha+\beta-2}. \tag{10}

Compare with h/n. The prior simply adds \alpha-1 imaginary heads and \beta-1 imaginary tails to the real counts (Mitchell, 2016, §2.2). The ratio of the imaginary counts says what we believe; their total says how strongly. As the real counts grow, the fixed imaginary counts matter less and less, and MAP approaches maximum likelihood.

Worked example 4 (Three heads, revisited). Take the prior \mathrm{Beta}(2,2): one imaginary head and one imaginary tail, a mild belief that the coin is roughly fair. After three real heads, (10) gives

\hat\theta_{\mathrm{MAP}}=\frac{3+1}{3+2}=\frac45=0.8,

four heads out of five, against \hat\theta_{\mathrm{MLE}}=1. Probably biased, but tails can still happen. Figure 3(a) shows the prior, the likelihood and the posterior.

Worked example 5 (More data, less prior). A stronger prior, \mathrm{Beta}(5,5), is worth 4 imaginary heads and 4 imaginary tails. With 7 heads in 10 flips, MAP is (7+4)/(10+8)=11/18=0.611, pulled from 0.7 toward \tfrac12 (Figure 3(b)). With 70 heads in 100 flips it is 74/108=0.685, and with 700 in 1{,}000 it is 704/1008=0.698: the data overwhelm the prior.

Prior \times likelihood \to posterior for a coin. (a) \mathrm{Beta}(2,2) prior and 3 heads in 3 flips: MAP 0.8 instead of 1. (b) \mathrm{Beta}(5,5) prior and 7 heads in 10 flips: MAP 0.61 instead of 0.7. Each curve is scaled to peak 1. Computed for this tutorial, with the same numbers as the companion video.
Figure 3. Prior \times likelihood \to posterior for a coin. (a) \mathrm{Beta}(2,2) prior and 3 heads in 3 flips: MAP 0.8 instead of 1. (b) \mathrm{Beta}(5,5) prior and 7 heads in 10 flips: MAP 0.61 instead of 0.7. Each curve is scaled to peak 1. Computed for this tutorial, with the same numbers as the companion video.
Try itA prior is imaginary data

A \mathrm{Beta}(\alpha,\beta) prior adds \alpha-1 imaginary heads and \beta-1 imaginary tails to the real flips, so \hat\theta_{\mathrm{MAP}}=(h+\alpha-1)/(n+\alpha+\beta-2), equation (10). Choose the imaginary and the real counts and watch prior \times likelihood become the posterior.

4.4The posterior mean and Laplace’s sunrise

The mode is not the only summary of the posterior. The posterior mean of \mathrm{Beta}(h+\alpha,n-h+\beta) is (h+\alpha)/(n+\alpha+\beta). With the flat prior \mathrm{Beta}(1,1) it becomes

\E[\theta\mid D]=\frac{h+1}{n+2}, \tag{11}

Laplace’s rule of succession: the probability that the next trial is a success after h successes in n trials. Laplace used it to ask whether the sun will rise tomorrow. Taking recorded history as 5{,}000 years, or 1{,}826{,}213 days with a sunrise every day, he found odds of 1{,}826{,}214 to 1, adding at once that anyone who understands the mechanism behind the days and seasons would give far higher odds (Laplace, 1902). In our language: the answer depends on the prior, and a physicist’s prior is much stronger than a flat one. Note also that (11) for three heads gives 4/5, the same number as Example 4: the posterior mean under \mathrm{Beta}(1,1) equals the MAP under \mathrm{Beta}(2,2).

Pause and think. Which Beta prior makes the MAP estimate (10) equal to the maximum likelihood estimate h/n for every data set?

Show the answer

\alpha=\beta=1, the flat prior: it adds zero imaginary heads and zero imaginary tails.