3.3 Bootstrap
In this section we discuss how to calculate standard errors and confidence intervals for a statistic \(T(X_1,X_2,\ldots, X_n)\). But first let us take a step back and consider the basics of what we are doing.
We are given a sample \((x_1,\ldots, x_n)\). We have some function, \(T\), that we apply to the sample and call a statistic. This may be a simple function like the sample mean, or some more complicated function like a parameter in a regression problem. We apply the function to the data and get a number \(t=T(x_1,\ldots,x_n)\). To do inference we also need to know the variability of that number if we were to repeat the same experiment. We therefore consider the random variable \(T_n := T(X_1,\ldots, X_n)\), where \(X_i\overset{iid}{\sim }F\). The distribution of this random variable is called the sampling distribution. In some cases, for a particular \(T\) and \(F\), we may be able to find the sampling distribution either exactly or approximately. For example, we previously found the asymptotic sampling distribution of the maximum likelihood estimate. Once we have that, we can do hypothesis testing, construct confidence intervals or simply state the standard deviation. The procedure can be summarized as: \[ F\overset{sample}{\to} x \overset{T}{\to} t. \]
However, in many cases the calculation of the sampling distribution is not possible and one way to instead approximate the distribution is through simulation. For now, let us assume that we know the distribution \(F\). Then it may be possible to draw \(n\) iid copies of the random variable \(X_1^\star,\ldots X^\star_n \overset{iid}{\sim} F\) using a computer. Then calculate \(t^\star = T(X_1^\star,\ldots X^\star_n)\). Repeat this \(B\) times to obtain \(t_1^\star,\ldots, t_B^\star\).
If we have \(B\) such copies, by the large of large numbers, as \(B\to \infty\), \[\begin{equation} \frac{1}{B}\sum_{i=1}^B f(t_i^\star)\overset{asym.}\sim E\left[ f(T_n) \right]. \tag{3.1} \end{equation}\] So that in practice, we would approximate the right hand side with the left hand side. The most important example is the variance: \[ Var\left( T_n \right) = E\left[T_n^2\right] - E\left[T_n\right]^2 \approx \frac{1}{B}\sum_{i=1}^B (t_i^\star)^2 - \left( \frac{1}{B}\sum_{i=1}^B t_i^\star \right)^2 = \frac{1}{B}\sum_{i=1}^B \left( t_i^\star - \bar{t^\star} \right)^2 \]
As an example, let us consider \(X_1,\ldots, X_{n}\overset{iid}\sim \mathsf{Exp}(1)\). We would like to know the distribution of the sample median and in particular the expected value and variance.
n <- 100
B <- 1000
T <- median
tstar <- array(dim = B)
for (i in seq_len(B)) {
x <- rexp(n, rate = 1)
tstar[i] <- T(x)
}
mean(tstar)## [1] 0.6964009
## [1] 0.01063886
In practice this is not helpful since we rarely know \(F\). What we have is \(\hat F_n\), so let us approximate \(F\approx \hat F_n\) and therefore also \(Var_F(T_n) \approx Var_{\hat F_n}(T_n)\). The right hand side here is then the variance of \(T(X_1,\ldots , X_n)\), where \(X_i\) is iid and distributed as \(\hat F_n\). Then we proceed as above: Draw \(n\) iid copies of the random variable \(X^\star_1,\ldots X^\star_n \sim \hat F_n\) using a computer. Then calculate \(t^\star = T(X_1^\star,\ldots X^\star_n)\). Repeat this \(B\) times to obtain \(t_1^\star,\ldots, t_B^\star\). Here, since \(\hat F_n\) is the empirical distribution, drawing from \(\hat F_n\) means simply to draw an observation at random from the original data set. In contrast to the above, this method can be summarized as: \[ \hat F_n\overset{sample}{\to} x^\star \overset{T}{\to} t^\star. \]
This method is called the bootstrap.
::: {.note}
Bootstrap variance estimation: Choose \(B\) as a large number, then for \(b=1,\ldots, B\)
- Draw \(X_1^\star,\ldots, X_n^\star \overset{iid}{\sim} \hat F_n\).
- Compute \(t_b^\star = T(X_1^\star,\ldots, X_n^\star)\).
- Approximate \(Var_F(T_n)\) by \[ v_{boot} = \frac{1}{B}\sum_{b=1}^B\left( t^\star_{b} - \frac{1}{B}\sum_{r=1}^B t^\star_{r} \right)^2 \]
:::
To be clear, step 1 above, draw \(X_1^\star,\ldots, X_n^\star \overset{iid}{\sim} \hat F_n\), simply means to draw one of the observations in the sample randomly and call it \(X_1^\star\). Put the observation back in the sample and independently draw another observation, call it \(X_2^\star\) and so on.
Let us implement this method to calculate the standard error of the median using the normally distributed data set from the previous section:
T <- median
n <- nrow(data.df)
B <- 1000
for (i in 1:B) {
indices <- sample(seq_len(n), size = n, replace = TRUE)
tstar[i] <- T(data.df$x[indices])
}
sd(tstar)## [1] 0.1564483
In practice we would rather use the boot library.
## Warning: package 'boot' was built under R version 4.6.1
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = data.df, statistic = function(data, index) {
## T(data$x[index])
## }, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 0.08979677 0.03632407 0.1475442
Note that the two bootstrap functions do not give exactly the same result. This is because of the Monte Carlo error, i.e. the error in approximating the sample mean with the expected value as in Equation (3.1).
Since here we know \(F\) we can calculate the standard deviation by simulating from \(F\), an alternative not available in practice.
n <- nrow(data.df)
B <- 1000
Tsim <- array(dim = B)
for (i in 1:B) {
simData.df <- data.frame( x = rnorm(n) )
Tsim[i] <- T(simData.df$x)
}
sd(Tsim) #Standard error of median## [1] 0.1253581
It is also possible to construct confidence intervals using bootstrap. Here we present bootstrap pivotal confidence intervals, sometimes known as basic bootstrap intervals.
Let us call \(T(F) = \theta\) and \(T(\hat F_n )=\hat\theta_n\) and define the pivot \(R_n = \hat\theta_n - \theta\). Write the distribution function of \(R_n\) as: \[ H(r) := P(R_n\leq r). \] Then, \[\begin{align} &P\left(\hat\theta_n - H^{-1}(1-\alpha/2)\leq \theta \leq \hat\theta_n - H^{-1}(\alpha/2)\right) \\ =& P\left( H^{-1}(\alpha/2) \leq \hat\theta_n - \theta \leq H^{-1}(1-\alpha/2) \right)\\ =& P\left( H^{-1}(\alpha/2) \leq R_n \leq H^{-1}(1-\alpha/2) \right)\\ =& H\left( H^{-1}(1-\alpha/2) \right) - H\left( H^{-1}(\alpha/2) \right)\\ =& 1-\frac{\alpha}{2}-\frac{\alpha}{2} = 1-\alpha, \end{align}\] proving that \[ \left[\hat\theta_n - H^{-1}(1-\alpha/2) , \hat\theta_n - H^{-1}(\alpha/2) \right] \] is a \(1-\alpha\) CI for \(\theta\).
Now, since \(H\) is unknown this is not practical. We can however construct a bootstrap estimate of \(H\) by drawing \(R^\star_{n,b} = \hat\theta^\star_{n,b}- \hat\theta_n\) and defining: \[ \hat H(r) = \frac{1}{B}\sum_{b=1}^B I(R^\star_{n,b}\leq r). \] The quantity \(H^{-1}(\alpha)\) is the \(\alpha\) quantile of \(H\). In the CI we replace \(H^{-1}\) by \(\hat H^{-1}\), i.e. the \(\alpha\) quantile of \(R^\star_{n,b}\). Since \(\hat \theta_n\) is fixed in the bootstrap sample, the \(\alpha\) quantile of \(R^\star_{n,b}\) is simply \(\theta_\alpha^\star - \hat\theta_n\), where \(\theta_\alpha^\star\) denotes the \(\alpha\) quantile of \(\hat\theta^\star_{n,b}\).
Finally we get that the \(1-\alpha\) bootstrap pivotal CI is \[ C_n = \left[ \hat\theta_n - (\theta^\star_{1-\alpha/2} - \hat\theta_n) , \hat\theta_n - (\theta^\star_{\alpha/2} - \hat\theta_n) \right] = \left[ 2\hat\theta_n - \theta^\star_{1-\alpha/2} , 2\hat\theta_n -\theta^\star_{\alpha/2} \right] \] Let us implement this on the same data set as above.
alpha <- 0.05
T <- median
n <- nrow(data.df)
B <- 1000
tstar <- array(dim = B)
for (i in seq_len(B)) {
indices <- sample(seq(1:n), size = n, replace = TRUE)
tstar[i] <- T(data.df$x[indices])
}
q <- unname( quantile(tstar, probs = c(1-alpha/2, alpha/2)) )
lowerCI <- 2*T(data.df$x) - q[1]
lowerCI## [1] -0.2250317
## [1] 0.3515109
Even simpler is to use the boot library.
library(boot)
boot.result<- boot(data = data.df,
statistic = function(data, index) T(data$x[index]),
R = 1000)
boot.ci(boot.result, type = "basic")## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = boot.result, type = "basic")
##
## Intervals :
## Level Basic
## 95% (-0.2389, 0.3129 )
## Calculations and Intervals on Original Scale
A better alternative which we do not cover in this course is the bias-corrected CI. It is however just as easy to use.
library(boot)
boot.result<- boot(data = data.df,
statistic = function(data, index) T(data$x[index]),
R = 1000)
boot.ci(boot.result, type = "bca")## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = boot.result, type = "bca")
##
## Intervals :
## Level BCa
## 95% (-0.1333, 0.3701 )
## Calculations and Intervals on Original Scale