The sample and the population

Bootstrap distribution

◆ The Blueprint
Why resampling works

The sampling distribution of a statistic describes its values over every possible sample of size \(n\) from the population. We have only one sample, so the bootstrap uses that sample in place of the population and draws resamples of size \(n\) from it with replacement. The spread of the statistic over the resamples estimates its spread over samples from the population.

The bootstrap standard error
$$\text{SE}_{\text{boot}} = \sqrt{\frac{1}{B-1}\sum_{b=1}^{B}\big(\hat\theta^*_b - \bar\theta^*\big)^2}, \qquad \bar\theta^* = \frac{1}{B}\sum_{b=1}^{B}\hat\theta^*_b$$

\(\hat\theta^*_b\) is the statistic on resample \(b\). The bootstrap distribution is centered near \(\hat\theta\), the statistic on the sample, and the gap \(\bar\theta^* - \hat\theta\) estimates the statistic's bias.

How many resamples

\(B\) sets how many resample statistics the histogram counts, so a larger \(B\) gives a smoother histogram and a steadier standard error. For the mean of 30 normal values, the bootstrap standard error moves by 0.004 on average (about 2%) between \(B = 500\) and \(B = 2{,}000\), and by at most 0.015, over 200 simulated samples; at this tab's seed it is 0.1851 at \(B = 500\) and 0.1787 at \(B = 2{,}000\). A larger \(n\) is what narrows the distribution.

The true standard errors

Mean: \(\sigma/\sqrt{n}\). Median: \(1/(2f(m)\sqrt{n})\), where \(m\) is the population median and \(f(m)\) the density there; it is accurate for large \(n\). It does not apply to the bimodal population, whose density at its median is near 0. SD: \(\sigma/\sqrt{2(n-1)}\), the formula for a normal population, which does not apply to the other two. IQR: no simple formula.

When the bootstrap is unreliable

With very small samples, and for statistics that depend on one or two extreme values (the maximum, the minimum), the resamples repeat the same few values and the bootstrap standard error is too small.

In R

replicate(B, mean(sample(x, replace = TRUE))) returns the \(B\) resample means, and sd() of them is the bootstrap standard error. Part 1 of The Script reproduces this tab's sample, its 500 resample means, and both standard errors.

Bootstrap distribution

Coverage

◆ The Blueprint
Percentile interval
$$\big(\hat\theta^*_{(\alpha/2)},\ \hat\theta^*_{(1-\alpha/2)}\big)$$

The \(\alpha/2\) and \(1-\alpha/2\) quantiles of the \(B\) resample statistics, with \(\alpha = 1 - \text{level}\). The quantiles use R's default rule, quantile(type = 7), which interpolates between neighboring sorted values.

Normal interval
$$\hat\theta \pm \texttt{qnorm}(1-\alpha/2)\,\text{SE}_{\text{boot}}$$

It assumes the statistic's sampling distribution is close to normal. When the bootstrap distribution is skewed, the percentile interval is lopsided around \(\hat\theta\) while the normal interval is symmetric, so the two disagree.

Coverage

A method's coverage is the probability that its interval contains the true value, over repeated samples. The coverage check estimates it with a count: each repetition draws a new sample, builds one interval, and records whether it contains the true value. The first repetition uses the sample on the plot. With \(r\) repetitions the estimate has standard error about \(\sqrt{0.95 \times 0.05 / r}\), 3 percentage points at \(r = 50\).

Why coverage is low for small skewed samples

For samples of 10 from the right-skewed population, the bootstrap standard error of the mean averages 0.278, about 12% below the true standard error \(1/\sqrt{10} = 0.316\), over 3,000 simulated samples. A bootstrap distribution built from 10 values understates the spread and the skew of the mean's sampling distribution, so 95% intervals capture the true mean less often than 95% of the time.

In R

quantile(boot_means, c(0.025, 0.975)) gives the percentile interval and mean(x) + c(-1, 1) * qnorm(0.975) * sd(boot_means) the normal interval. Part 2 of The Script runs the coverage check.

The data and the fitted curve

Error by degree

◆ The Blueprint
The procedure
  1. Assign each point to one of \(K\) folds.
  2. For fold \(k\), fit the model to every point outside fold \(k\).
  3. Predict the points in fold \(k\) and compute their mean squared error, \(\text{MSE}_k\).
  4. Average the \(K\) held-out MSEs.
$$\text{CV} = \frac{1}{K}\sum_{k=1}^{K}\text{MSE}_k, \qquad \text{SE}(\text{CV}) = \frac{\text{sd}(\text{MSE}_1, \ldots, \text{MSE}_K)}{\sqrt{K}}$$

The shaded band on the error plot is CV ± SE(CV).

How the folds are assigned

The \(K\) fold labels are put in random order and repeated down the points sorted by \(x\): rep_len(sample(1:K), n). Every fold therefore spreads across the whole range of \(x\). With \(K = n\), leave-one-out cross-validation (LOOCV), each fold is one point and no random draw is needed.

Choosing K

A small \(K\) fits each model to fewer points, so the CV error tends to overstate the error of a model fit to all \(n\). A large \(K\) fits to nearly all the points, but its fits are nearly identical to one another, so the CV error varies more from sample to sample. \(K = 5\) or \(K = 10\) is the usual choice.

Why training error falls

A degree-\(d\) polynomial contains every lower-degree polynomial, so least squares at degree \(d+1\) can always match the degree-\(d\) fit and usually improves on it. Training MSE never rises with the degree. At degrees 11 and 12 the powers of \(x\) are so alike that small rounding errors change the fit.

In R

lm(y ~ poly(x, d, raw = TRUE), subset = !test) fits without the held-out fold, and predict() scores it. Part 3 of The Script loops over folds and degrees.

Coefficient paths

Coefficients at your λ

CV error by λ

◆ The Blueprint
The penalized objective
$$\min_{\beta_0,\,\beta}\ \frac{1}{2n}\sum_{i=1}^{n}\big(y_i - \beta_0 - x_i^\top\beta\big)^2 + \lambda\Big[\tfrac{1-\alpha}{2}\sum_j \beta_j^2 + \alpha\sum_j |\beta_j|\Big]$$

This is glmnet's form. \(\alpha = 1\) is the LASSO, which sets some coefficients exactly to 0. \(\alpha = 0\) is ridge, which shrinks every coefficient toward 0 and sets none to 0. Values between give the elastic net. The intercept is not penalized, and glmnet standardizes each predictor before fitting and reports the coefficients on the original scale.

The λ path

glmnet starts at the smallest λ that sets every coefficient to 0 and lowers it in up to 100 equal steps on the log scale, down to \(10^{-4}\) times the start when \(n > p\). It stops early once the fit stops improving, so a path often has fewer than 100 values.

Cross-validation in cv.glmnet

Each fold gets its own path; its predictions at the full data's λ values give \(\text{MSE}_k(\lambda)\). The CV error averages the folds weighted by their sizes, and the bars are its standard error.

$$\lambda_{\min} = \arg\min_\lambda \text{CV}(\lambda), \qquad \lambda_{\text{1se}} = \max\{\lambda : \text{CV}(\lambda) \le \text{CV}(\lambda_{\min}) + \text{SE}(\lambda_{\min})\}$$

\(\lambda_{\text{1se}}\) is the simplest model within one standard error of the lowest CV error.

Tuning α as well

Here α stays fixed while λ is tuned. To tune both, run cv.glmnet() with the same fold assignments (foldid) for each α on a grid and keep the pair with the lowest CV error.

In R

cv.glmnet(X, y, alpha = 1, foldid = foldid), then coef(cv_lasso, s = "lambda.1se"). Part 4 of The Script fits this tab's paths and runs its ten-fold CV.

The R functions, one by one
CallWhat it returns
set.seed(123)Starts R's random numbers at a tab's seed, so R draws the same data as the tab.
sample(x, replace = TRUE)One resample: n values drawn from x with replacement.
replicate(B, mean(sample(x, replace = TRUE)))The B resample means, the bootstrap distribution.
quantile(boot_means, c(0.025, 0.975))The 95% percentile interval.
lm(y ~ poly(x, d, raw = TRUE), subset = !test)A degree-d polynomial fit without the held-out fold.
glmnet(X, y, alpha = 1)The LASSO coefficients at every λ on the path; alpha = 0 is ridge.
cv.glmnet(X, y, alpha = 1, foldid = foldid)The CV error and its standard error at every λ, plus lambda.min and lambda.1se.
coef(cv_lasso, s = "lambda.1se")The coefficients at λ1se; a dot marks a coefficient of exactly 0.
bcv_in_R.R