Algorithmadvanced
Bootstrap-t interval
Also called studentized bootstrap interval, percentile-t interval.
Bootstraps the studentized statistic, estimate minus original over its standard error, and inverts its quantiles. It is second-order accurate when the standard error is stable.
- Time
- Θ(b n + b log b) worstΘ(b n) best
- Space
- Θ(b + n) extra
- Where
- b = the number of bootstrap replicates; n = the sample size
- Model
- Real RAM
- Properties
- randomizednot exactofflinerandomizediterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Bradley Efron, Peter Hall, 1981
The bootstrap-t interval estimates the law of the studentized statistic by its bootstrap copy , and inverts it as the t interval inverts the Student table. Each resample needs its own standard error, so the method fits statistics with a standard error formula, such as a mean, a difference of means or a regression coefficient. Its one-sided coverage error is of order , against for the percentile interval, but it is not transformation respecting and it can give very long intervals. The coverage of the interval is measured on this page, not proved.
In plain words
The usual interval for a mean is the estimate plus or minus a table number times the standard error. The table number comes from the bell curve and is correct only for normal data. The bootstrap-t method makes its own table from the data.
Take many bootstrap samples. For each one, measure how far its estimate is from the original estimate, in units of its own standard error. Sort these distances. The lower and upper cut points take the place of the table number. Because the data make the table, the two cut points can differ, and the interval can be longer on one side.
The problem
Input. A sample , independent and identically distributed from an unknown ; a procedure est that returns an estimate of and a standard error for it; a level ; and a number of replicates .
Output. An interval that should contain with probability close to .
Definition. Let be the bootstrap distribution function of under resampling from , and . The ideal bootstrap-t interval is
If had the law , then , and solving the two inequalities for gives this interval. Note that the upper quantile gives the lower endpoint.
The algorithm
BOOTSTRAP-T-INTERVAL(x_1, ..., x_n, est, α, b)- 1
(θ̂, se) ← est(x_1, ..., x_n) - 2
for j ← 1 to b do - 3
x* ← n independent draws from x_1 .. x_n, each with probability 1/n - 4
(θ̂*, se*) ← est(x*) - 5
t*_j ← (θ̂* - θ̂) / se* - 6
sort t*_1, ..., t*_b into t*_(1) ≤ ... ≤ t*_(b) - 7
t_lo ← t*_(⌈b α/2⌉), t_hi ← t*_(⌈b (1 - α/2)⌉) - 8
return [θ̂ - t_hi · se, θ̂ - t_lo · se]
Line L7 uses the same quantile rule as the percentile interval: is the -th smallest replicate. For the mean, est returns and , with the sample variance with divisor .
Zero standard errors. A resample can have , for example when every draw is the same value. The implementation sets if also , and otherwise, with the sign of . Such a replicate stays in the tail where it belongs. If about or more replicates are infinite on one side, the corresponding endpoint is infinite, which is the honest answer; the proof gives the exact counts. For the mean, a resample has with probability , which is for .
Example
Take the right-skewed sample of values
with and . With , and random.Random(1), line L7 gives
The Student table would give . The bootstrap table is strongly skewed: resamples that miss the large values have a small mean and also a small standard error, so their is very negative. The smallest of the 2000 values was , the largest . Line L8 gives
With the same seed, the t interval is , the percentile interval is and the BCa interval is . The bootstrap-t interval is by far the longest on the right.
Equivariance and its limit. The data give , which is , because does not change under such a map. The method is not transformation respecting: the bootstrap-t interval for , with the delta method standard error , transformed back, is , not .
Measured coverage. For samples from the exponential law with mean 1, 1000 data sets of each size with (seeds 101 and 102) gave these coverages for the true mean at the nominal level 0.95:
| method | ||
|---|---|---|
| bootstrap-t | 0.943 | 0.950 |
| t interval | 0.911 | 0.924 |
| percentile | 0.869 | 0.911 |
| BCa | 0.884 | 0.916 |
At the bootstrap-t interval was entirely below the true mean in of the data sets and entirely above it in , against and for the t interval. The Monte Carlo standard error of each coverage is about .
Correctness
The proof is in proofs/correctness.md. What is proved:
- Lines L6 and L7 return and , and line L8 inverts them in the right order.
- If and have the same continuous law, the ideal interval has coverage exactly . This holds, for example, in a location-scale family where is a pivot and the parametric bootstrap is used.
- The interval is equivariant under maps with when and are equivariant.
- With the convention for , line L5 never divides by zero, and an infinite endpoint arises only when at least of the replicates lie in one infinite tail.
What is not proved: the coverage for a nonparametric . Under smoothness conditions, Hall (1988) shows by Edgeworth expansions that the coverage error of each one-sided endpoint is . The table above is a measurement.
Proof: The bootstrap-t interval inverts the bootstrap law of the studentized statisticfull
We use the pseudocode of the page. The data are fixed, est returns with , and with the convention of the page when . is the bootstrap distribution function of , the empirical distribution function of the replicates, and for any distribution function , with values in .
This file proves what the algorithm computes and when the ideal interval is exact. It does not prove a coverage level for a nonparametric ; that coverage is measured on the page.
Claim 1 (lines L6 and L7). and .
Proof. The argument of Claim 1 of the percentile interval applies to any values in the extended real line: for and , at least replicates are and at most are below it.
Claim 2 (inversion). For every and every pair ,
Proof. Multiply by , which keeps the order, and subtract ; then multiply by , which reverses both inequalities.
So line L8 returns the set of for which the studentized value lies between the two bootstrap quantiles. Note that the upper quantile gives the lower endpoint.
Claim 3 (exact coverage when the bootstrap law is the true law). If has a continuous distribution function , the interval contains with probability exactly .
Proof. By Claim 2 the event is . For a continuous , and as well. So the probability is .
Claim 3 is the reason for the method: if is a pivot, its law does not depend on , and a bootstrap that reproduces it gives an exact interval. For normal data and the mean, has the Student law with degrees of freedom, and the parametric bootstrap with the normal model reproduces it, so its ideal bootstrap-t interval is the t interval. The nonparametric bootstrap law is discrete and only approximates the law of .
Claim 4 (affine equivariance). Let and be real numbers, and suppose est is equivariant: for every sample . Run the algorithm on and on with the same random numbers. If the first run returns , the second returns .
Proof. Line L3 draws the same indices in both runs, so the -th resample of is . Line L4 gives . If , then . If , then , and has the sign of , so the convention gives the same value. So both runs have the same , the same and , and line L8 gives and likewise .
The mean with is equivariant in this sense. The tests check the claim with and up to rounding. For , changes sign and the order is reversed, so the two endpoints are mirror images only up to one rank, as in the percentile interval. The method is not equivariant under nonlinear monotone maps; the page shows a counterexample with the logarithm.
Claim 5 (zero standard errors). Line L5 is defined for every resample. Let and . The upper endpoint is exactly when at least replicates are , which is the same as at least of them. The lower endpoint is exactly when at least replicates are , which is the same as more than of them.
Proof. The convention assigns a value in when , so no division by zero happens. In sorted order, exactly when at least values are , and exactly when at least values are . The identity holds because is an integer. For an integer count , is the same as . By line L8, makes the upper endpoint , and makes the lower endpoint .
The tests compute the ideal law of by listing all resamples for and compare it with the multiset enumeration of the implementation. For the sample the law puts mass on , on and on .
What is not proved. For a nonparametric and a smooth statistic, Hall (1988) shows by Edgeworth expansions that each one-sided endpoint has coverage error , because the bootstrap reproduces the first skewness term of the expansion of the law of . That result needs moment and smoothness conditions that this file does not check. The coverages on the page are measurements from a simulation with stated seeds, with a Monte Carlo standard error of about .
Cost. The loop runs times; each pass draws indices and calls est, which costs for the mean. Line L6 sorts values in time. The total is time and extra space.
Complexity
Each replicate draws indices and calls est, which for the mean costs . The sort costs ; two selections instead of the sort give for this step. So the time is , and the extra space is . If no formula for exists, a nested bootstrap with inner replicates estimates it, and the time becomes .
Properties
- Second-order accurate. For smooth statistics, is asymptotically pivotal, and the bootstrap captures the skewness term of its Edgeworth expansion, so the one-sided coverage error is .
- Equivariant, not transformation respecting. The interval moves with affine changes of the data, but the result depends on the scale on which the method is run. Running it on a variance-stabilized scale and transforming back usually helps.
- Not range preserving. An endpoint can fall outside the parameter space, for example a correlation above 1.
- Sensitive to the standard error. The method is only as good as . Small, unstable standard errors in some resamples give extreme values and long intervals, as the example shows.
- Monte Carlo error. The endpoints depend on extreme order statistics of ; use .
Variants and improvements
- Nested bootstrap standard error. When there is no formula, an inner bootstrap of size to estimates in each outer resample. The jackknife standard error is a cheaper choice.
- Variance-stabilized bootstrap-t. Tibshirani (1988) estimates a variance-stabilizing transformation from a first bootstrap, runs the bootstrap-t on the new scale and transforms back. The result is shorter and more stable, and it is close to transformation respecting.
- Symmetric bootstrap-t. Uses the quantile of and returns . Its two-sided coverage error is under smoothness, but it cannot follow skewness.
- Bootstrap tests. Inverting the interval gives a bootstrap t test of , by the duality of tests and intervals. For two samples with unequal variances, studentizing with the Welch standard error gives a bootstrap version of the Welch test.
- Regression. Resampling residuals or pairs, with the usual standard error of a coefficient, gives bootstrap-t intervals for the coefficients.
When to use it
Use it for a mean, a difference of means or a regression coefficient when the data are skewed and is small or moderate, which is where the t interval is least accurate. Avoid it for statistics without a stable standard error, such as the median, and for bounded parameters such as a correlation, unless you first move to a scale where the parameter is unbounded and the variance is stable. For a general smooth statistic without a standard error formula, the BCa interval is often simpler.
Pitfalls
- Using the original standard error. Dividing by instead of gives a rescaled percentile interval and loses the second-order accuracy.
- Swapping the quantiles. The upper quantile gives the lower endpoint. Swapping them reflects the interval and badly damages the coverage of skewed statistics.
- Very long intervals. In small skewed samples the lower tail of is long, and so is the upper part of the interval. The interval is valid but may be too wide to be useful.
- Dropping replicates with zero standard error. Removing them biases the tails. Keep them as , as line L5 does in the implementation.
- Outliers. One large value can control both and in many resamples.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/bootstrap-t-interval/. The authors are listed in prov/48PN4N.jsonl.
import itertools
import math
import random
from fractions import Fraction
from typing import Callable, Sequence
Estimate = Callable[[Sequence[float]], tuple]
def mean_and_se(xs: Sequence[float]) -> tuple:
"""The sample mean and its standard error s / sqrt(n), s with divisor n - 1."""
n = len(xs)
if n < 2:
raise ValueError("need at least two values")
m = math.fsum(xs) / n
ss = math.fsum((v - m) ** 2 for v in xs) # fsum is exactly rounded, so the order of xs does not matter
return m, math.sqrt(ss / (n - 1)) / math.sqrt(n)
def empirical_quantile(sorted_values: Sequence[float], p: float) -> float:
"""inf{t : G(t) >= p}: the ceil(b p)-th smallest value, or the smallest for p = 0."""
k = max(1, math.ceil(Fraction(p) * len(sorted_values)))
return sorted_values[k - 1]
def studentized(theta_star: float, se_star: float, theta_hat: float) -> float:
"""t* = (theta* - theta_hat) / se*. A resample with se* = 0 gives 0 if theta* = theta_hat,
and plus or minus infinity otherwise, so that it stays in the right tail."""
diff = theta_star - theta_hat
if se_star == 0:
if diff == 0:
return 0.0
return math.copysign(math.inf, diff)
return diff / se_star
def bootstrap_t_interval(
x: Sequence[float],
alpha: float,
b: int,
rng: random.Random,
estimate: Estimate = mean_and_se,
) -> tuple:
"""The 1 - alpha studentized bootstrap interval
[theta_hat - t*_(1 - alpha/2) se, theta_hat - t*_(alpha/2) se]."""
if not 0 < alpha < 1:
raise ValueError("alpha must be in (0, 1)")
if b < 1:
raise ValueError("b must be at least 1")
n = len(x)
theta_hat, se = estimate(x) # @line L1
if se == 0:
raise ValueError("the standard error of the sample is 0")
ts = []
for _ in range(b): # @line L2
xs = [x[rng.randrange(n)] for _ in range(n)] # @line L3
theta_star, se_star = estimate(xs) # @line L4
ts.append(studentized(theta_star, se_star, theta_hat)) # @line L5
ts.sort() # @line L6
t_lo = empirical_quantile(ts, alpha / 2) # @line L7
t_hi = empirical_quantile(ts, 1 - alpha / 2)
return theta_hat - t_hi * se, theta_hat - t_lo * se # @line L8
def ideal_t_law(x: Sequence[float], estimate: Estimate = mean_and_se) -> list:
"""The ideal bootstrap law of t* (b = infinity) as a sorted list of (t, probability).
It lists the C(2n - 1, n) multisets of indices with multinomial weights; small n only.
"""
n = len(x)
theta_hat, _ = estimate(x)
fact_n = math.factorial(n)
law: dict = {}
for idx in itertools.combinations_with_replacement(range(n), n):
w = fact_n
for _, g in itertools.groupby(idx):
w //= math.factorial(len(list(g)))
theta_star, se_star = estimate([x[i] for i in idx])
t = studentized(theta_star, se_star, theta_hat)
law[t] = law.get(t, 0) + w
return sorted((t, Fraction(w, n**n)) for t, w in law.items())
test_bootstrap_t_interval.py 87 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/bootstrap-t-interval/. The authors are listed in prov/48PN4N.jsonl.
import itertools
import math
import random
import statistics
from fractions import Fraction
import pytest
from bootstrap_t_interval import (
bootstrap_t_interval,
empirical_quantile,
ideal_t_law,
mean_and_se,
studentized,
)
SKEWED = [0.4, 0.9, 1.3, 1.6, 2.2, 2.8, 3.5, 4.9, 7.6, 13.1]
def test_ideal_law_matches_all_resamples():
rng = random.Random(1)
for n in range(2, 6):
x = [float(rng.randint(0, 9)) for _ in range(n)]
if len(set(x)) == 1:
x[0] += 1.0
theta_hat, _ = mean_and_se(x)
law = {}
for idx in itertools.product(range(n), repeat=n):
xs = sorted(x[i] for i in idx)
t = studentized(*mean_and_se(xs), theta_hat)
law[t] = law.get(t, 0) + Fraction(1, n**n)
assert ideal_t_law(x) == sorted(law.items())
def test_zero_standard_error_resamples():
law = dict(ideal_t_law([0.0, 1.0]))
assert law == {-math.inf: Fraction(1, 4), 0.0: Fraction(1, 2), math.inf: Fraction(1, 4)}
def test_affine_equivariance():
lo, hi = bootstrap_t_interval(SKEWED, 0.1, 999, random.Random(3))
y = [5.0 + 2.0 * v for v in SKEWED]
ylo, yhi = bootstrap_t_interval(y, 0.1, 999, random.Random(3))
assert math.isclose(ylo, 5.0 + 2.0 * lo, rel_tol=1e-9)
assert math.isclose(yhi, 5.0 + 2.0 * hi, rel_tol=1e-9)
z = [-v for v in SKEWED]
zlo, zhi = bootstrap_t_interval(z, 0.1, 999, random.Random(3))
# Negation flips t*, so the interval of -x is close to minus the interval of x;
# with b = 999 the order statistics used are mirror images only up to one rank.
assert zlo < -statistics.fmean(SKEWED) < zhi
def test_page_example_is_skewed_right():
lo, hi = bootstrap_t_interval(SKEWED, 0.05, 2000, random.Random(1))
m = statistics.fmean(SKEWED)
assert lo < m < hi and hi - m > m - lo
def coverage(sampler, true_mean, r, b, n, seed):
rng = random.Random(seed)
hits = 0
for _ in range(r):
x = [sampler(rng) for _ in range(n)]
lo, hi = bootstrap_t_interval(x, 0.05, b, rng)
hits += lo <= true_mean <= hi
return hits / r
def test_coverage_is_measured_not_proved():
r, alpha = 300, 0.05
tol = 4 * math.sqrt(alpha * (1 - alpha) / r)
cov = coverage(lambda g: g.gauss(0.0, 1.0), 0.0, r, 300, 20, 8)
assert abs(cov - 0.95) <= tol + 0.03
cov = coverage(lambda g: g.expovariate(1.0), 1.0, r, 300, 20, 9)
assert abs(cov - 0.95) <= tol + 0.03
def test_quantile_and_errors():
assert empirical_quantile([1.0, 2.0, 3.0, 4.0], 0.5) == 2.0
assert empirical_quantile([1.0, 2.0, 3.0, 4.0], 0.0) == 1.0
with pytest.raises(ValueError):
bootstrap_t_interval([2.0, 2.0, 2.0], 0.1, 10, random.Random(1))
with pytest.raises(ValueError):
bootstrap_t_interval(SKEWED, 1.5, 10, random.Random(1))
History
The idea of studentizing before resampling is in Bradley Efron's early work on the bootstrap, around 1979 to 1982, as the natural bootstrap version of Student's t. Efron noted that the method can be erratic, and he later preferred the BCa interval for general statistics. Peter Hall (1988) used Edgeworth expansions to show that the studentized interval is second-order accurate and that its endpoints agree with those of the interval based on the exact law of up to terms of order ; Hall called it the "percentile-t" method, which is the second alias of this page. Robert Tibshirani (1988) proposed the variance-stabilized version to make it more stable.
References
- Bradley Efron and Robert J. Tibshirani, An Introduction to the Bootstrap, Chapman and Hall, 1993, Chapter 12 (confidence intervals based on bootstrap tables, Section 12.5 on the bootstrap-t interval and its transformations)
- Peter Hall, Theoretical comparison of bootstrap confidence intervals, The Annals of Statistics 16(3), 1988, pp. 927 to 953 (the studentized interval is second-order accurate and second-order correct)
- Peter Hall, The Bootstrap and Edgeworth Expansion, Springer, 1992, Chapter 3 (coverage errors of percentile and percentile-t intervals)
- A. C. Davison and D. V. Hinkley, Bootstrap Methods and their Application, Cambridge University Press, 1997, Chapter 5 (the studentized bootstrap confidence limits and variance-stabilizing transformations)
- Bradley Efron, Bootstrap methods, another look at the jackknife, The Annals of Statistics 7(1), 1979, pp. 1 to 26 (the bootstrap distribution of a statistic)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 48PN4N Edit this page History Report an error Markdown