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

06A prior is a regularizer: ridge, lasso and smoothing

6.1From priors to penalties

The connection interviewers love: regularization is a prior, written as a penalty. Take linear regression with Gaussian noise, y=Xw+\varepsilon with \varepsilon\sim\mathcal N(0,\sigma^2I), so that by (4) maximum likelihood is least squares. Now put a prior on the weights.

Gaussian prior. If the weights are independent \mathcal N(0,\tau^2), then -\log p(w)=\|w\|_2^2/(2\tau^2)+\text{const}, and by (8)

\hat w_{\mathrm{MAP}}=\arg\min_w\Big[\frac{1}{2\sigma^2}\|y-Xw\|_2^2+\frac{1}{2\tau^2}\|w\|_2^2\Big].

Multiplying by 2\sigma^2 does not move the minimum, so

\hat w_{\mathrm{MAP}}=\arg\min_w\ \|y-Xw\|_2^2+\lambda\|w\|_2^2,\qquad\lambda=\frac{\sigma^2}{\tau^2}, \tag{13}

which is ridge regression (Hoerl and Kennard, 1970). A stronger belief that the weights are small (smaller \tau) means a bigger \lambda, and noisier data (bigger \sigma) also mean a bigger \lambda: trust the prior more when the data say less (Bishop, 2006, §1.2.5, §3.3).

Laplace prior. If the weights are independent with density p(w_j)=\frac{1}{2b}e^{-|w_j|/b}, then -\log p(w)=\|w\|_1/b+\text{const} and the same steps give

\hat w_{\mathrm{MAP}}=\arg\min_w\ \|y-Xw\|_2^2+\lambda\|w\|_1,\qquad\lambda=\frac{2\sigma^2}{b}, \tag{14}

the lasso (Tibshirani, 1996). Figure 5 shows the two priors and their negative logarithms. The Gaussian prior is a smooth hill whose penalty is a bowl; the Laplace prior has a sharp peak at zero whose penalty is a V, and that corner is what sets some weights exactly to zero (Hastie et al., 2009, §3.4).

(a) A Gaussian prior \mathcal N(0,1) and a Laplace prior with the same variance, 1. (b) Their negative logarithms, which are the L2 and L1 penalties. Computed for this tutorial.
Figure 5. (a) A Gaussian prior \mathcal N(0,1) and a Laplace prior with the same variance, 1. (b) Their negative logarithms, which are the L2 and L1 penalties. Computed for this tutorial.

6.2On real data

Figure 6 uses the classic diabetes data of Efron et al. (2004): 442 patients, ten baseline measurements (age, sex, body mass index, blood pressure and six blood serum measurements s_1,\dots,s_6) and a measure of disease progression one year later, all standardized. Least squares (maximum likelihood) gives large weights of opposite sign to the strongly correlated cholesterol measurements s_1 and s_2 (-0.489 and 0.294), which largely cancel. Ridge with \lambda=100 (a Gaussian prior) shrinks all the weights; their length \|w\|_2 falls from 0.851 to 0.451. The lasso (a Laplace prior; scikit-learn’s alpha =0.06, which is \lambda=2n\alpha=53.04 in (14)) sets five weights exactly to zero (age, s_1, s_2, s_4, s_6) and keeps sex, body mass index, blood pressure, s_3 and s_5 (Pedregosa et al., 2011).

The ten standardized weights of a linear model for the diabetes data (Efron et al., 2004): least squares, ridge (\lambda=100) and lasso (\lambda=53.04). Open circles mark the weights the lasso sets exactly to zero. The penalty strengths are chosen for illustration. Computed for this tutorial, with the same numbers as the companion video.
Figure 6. The ten standardized weights of a linear model for the diabetes data (Efron et al., 2004): least squares, ridge (\lambda=100) and lasso (\lambda=53.04). Open circles mark the weights the lasso sets exactly to zero. The penalty strengths are chosen for illustration. Computed for this tutorial, with the same numbers as the companion video.

6.3Add-one smoothing is a prior

Back to the spam filter of Section 3. Each word’s probability of appearing in a spam e-mail is a coin: it appears in n_w of the N spam e-mails. Maximum likelihood gives n_w/N, and zero for an unseen word. Put the prior \mathrm{Beta}(2,2) on each word’s probability, one imaginary spam e-mail with the word and one without, and (10) gives

\hat\theta_w=\frac{n_w+1}{N+2}, \tag{15}

add-one, or Laplace, smoothing. For our words: “free” 31/62=0.500, “money” 13/62=0.210, “meeting” 1/62=0.016, and the e-mail now gets 0.500\times0.210\times0.016\approx0.0017 instead of 0. It is the same formula as the coin in Example 4. For word counts in a multinomial model the smoothed estimate is (T_{w}+1)/(\sum_{w'}T_{w'}+V) with V the vocabulary size, the MAP estimate under a Dirichlet prior (Manning et al., 2008, §13.2).

Pause and think. Doubling the noise standard deviation \sigma while keeping the prior fixed: what happens to the ridge parameter \lambda, and why does that make sense?

Show the answer

\lambda=\sigma^2/\tau^2 grows four times. Noisier data are weaker evidence, so the estimate should lean more on the prior.