Algorithm Search⌘K

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
Properties
randomizednot exactofflinerandomizediterative
Origin
Bradley Efron, Peter Hall, 1981

The bootstrap-t interval estimates the law of the studentized statistic T=(θ^−θ)/se^T = (\hat\theta - \theta)/\widehat{se} by its bootstrap copy T∗=(θ^∗−θ^)/se^∗T^* = (\hat\theta^* - \hat\theta)/\widehat{se}^*, 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 n−1n^{-1}, against n−1/2n^{-1/2} 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 x1,…,xnx_1, \ldots, x_n, independent and identically distributed from an unknown FF; a procedure est that returns an estimate θ^\hat\theta of θ=t(F)\theta = t(F) and a standard error se^\widehat{se} for it; a level α∈(0,1)\alpha \in (0, 1); and a number of replicates bb.

Output. An interval [L,U][L, U] that should contain θ\theta with probability close to 1−α1 - \alpha.

Definition. Let KK be the bootstrap distribution function of T∗=(θ^∗−θ^)/se^∗T^* = (\hat\theta^* - \hat\theta)/\widehat{se}^* under resampling from F^n\hat F_n, and K−1(p)=inf⁡{t:K(t)≥p}K^{-1}(p) = \inf\{t : K(t) \ge p\}. The ideal bootstrap-t interval is

[ θ^−K−1(1−α/2) se^,  θ^−K−1(α/2) se^ ].[\,\hat\theta - K^{-1}(1 - \alpha/2)\, \widehat{se},\ \ \hat\theta - K^{-1}(\alpha/2)\, \widehat{se}\,].

If TT had the law KK, then P(K−1(α/2)≤T≤K−1(1−α/2))=1−αP(K^{-1}(\alpha/2) \le T \le K^{-1}(1-\alpha/2)) = 1 - \alpha, and solving the two inequalities for θ\theta gives this interval. Note that the upper quantile gives the lower endpoint.

The algorithm

  1. BOOTSTRAP-T-INTERVAL(x_1, ..., x_n, est, α, b)
  2. 1(θ̂, se) ← est(x_1, ..., x_n)
  3. 2for j ← 1 to b do
  4. 3x* ← n independent draws from x_1 .. x_n, each with probability 1/n
  5. 4(θ̂*, se*) ← est(x*)
  6. 5t*_j ← (θ̂* - θ̂) / se*
  7. 6sort t*_1, ..., t*_b into t*_(1) ≤ ... ≤ t*_(b)
  8. 7t_lo ← t*_(⌈b α/2⌉), t_hi ← t*_(⌈b (1 - α/2)⌉)
  9. 8return [θ̂ - t_hi · se, θ̂ - t_lo · se]

Line L7 uses the same quantile rule as the percentile interval: K^b−1(p)\hat K_b^{-1}(p) is the ⌈bp⌉\lceil b p \rceil-th smallest replicate. For the mean, est returns xˉ\bar x and S/nS/\sqrt n, with S2S^2 the sample variance with divisor n−1n - 1.

Zero standard errors. A resample can have se^∗=0\widehat{se}^* = 0, for example when every draw is the same value. The implementation sets t∗=0t^* = 0 if also θ^∗=θ^\hat\theta^* = \hat\theta, and t∗=±∞t^* = \pm\infty otherwise, with the sign of θ^∗−θ^\hat\theta^* - \hat\theta. Such a replicate stays in the tail where it belongs. If about bα/2b\alpha/2 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 se^∗=0\widehat{se}^* = 0 with probability n1−nn^{1-n}, which is 10−910^{-9} for n=10n = 10.

Example

Take the right-skewed sample of n=10n = 10 values

0.4, 0.9, 1.3, 1.6, 2.2, 2.8, 3.5, 4.9, 7.6, 13.1,0.4,\ 0.9,\ 1.3,\ 1.6,\ 2.2,\ 2.8,\ 3.5,\ 4.9,\ 7.6,\ 13.1,

with xˉ=3.83\bar x = 3.83 and se^=S/10=1.233\widehat{se} = S/\sqrt{10} = 1.233. With α=0.05\alpha = 0.05, b=2000b = 2000 and random.Random(1), line L7 gives

tlo=t(50)∗=−5.224,thi=t(1950)∗=1.626.t_{lo} = t^*_{(50)} = -5.224, \qquad t_{hi} = t^*_{(1950)} = 1.626.

The Student table would give ∓2.262\mp 2.262. The bootstrap table is strongly skewed: resamples that miss the large values have a small mean and also a small standard error, so their t∗t^* is very negative. The smallest of the 2000 values was −18.5-18.5, the largest 4.654.65. Line L8 gives

[ 3.83−1.626⋅1.233,  3.83+5.224⋅1.233 ]=[1.82, 10.27].[\,3.83 - 1.626 \cdot 1.233,\ \ 3.83 + 5.224 \cdot 1.233\,] = [1.82,\ 10.27].

With the same seed, the t interval is [1.04,6.62][1.04, 6.62], the percentile interval is [1.80,6.31][1.80, 6.31] and the BCa interval is [2.18,6.97][2.18, 6.97]. The bootstrap-t interval is by far the longest on the right.

Equivariance and its limit. The data 3xi+53x_i + 5 give [10.47,35.82][10.47, 35.82], which is 3⋅[1.82,10.27]+53 \cdot [1.82, 10.27] + 5, because t∗t^* does not change under such a map. The method is not transformation respecting: the bootstrap-t interval for log⁡θ\log\theta, with the delta method standard error se^/xˉ\widehat{se}/\bar x, transformed back, is [1.97,12.30][1.97, 12.30], not [1.82,10.27][1.82, 10.27].

Measured coverage. For samples from the exponential law with mean 1, 1000 data sets of each size with b=1000b = 1000 (seeds 101 and 102) gave these coverages for the true mean at the nominal level 0.95:

method n=10n = 10 n=20n = 20
bootstrap-t 0.943 0.950
t interval 0.911 0.924
percentile 0.869 0.911
BCa 0.884 0.916

At n=10n = 10 the bootstrap-t interval was entirely below the true mean in 4.5%4.5\% of the data sets and entirely above it in 1.2%1.2\%, against 8.4%8.4\% and 0.5%0.5\% for the t interval. The Monte Carlo standard error of each coverage is about 0.0070.007.

Correctness

The proof is in proofs/correctness.md. What is proved:

  • Lines L6 and L7 return K^b−1(α/2)\hat K_b^{-1}(\alpha/2) and K^b−1(1−α/2)\hat K_b^{-1}(1 - \alpha/2), and line L8 inverts them in the right order.
  • If TT and T∗T^* have the same continuous law, the ideal interval has coverage exactly 1−α1 - \alpha. This holds, for example, in a location-scale family where TT is a pivot and the parametric bootstrap is used.
  • The interval is equivariant under maps x↦cx+dx \mapsto c x + d with c>0c > 0 when θ^\hat\theta and se^\widehat{se} are equivariant.
  • With the convention for se^∗=0\widehat{se}^* = 0, line L5 never divides by zero, and an infinite endpoint arises only when at least bα/2b\alpha/2 of the replicates lie in one infinite tail.

What is not proved: the coverage for a nonparametric FF. Under smoothness conditions, Hall (1988) shows by Edgeworth expansions that the coverage error of each one-sided endpoint is O(n−1)O(n^{-1}). 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 (θ^,se^)(\hat\theta, \widehat{se}) with se^>0\widehat{se} > 0, and T∗=(θ^∗−θ^)/se^∗T^* = (\hat\theta^* - \hat\theta)/\widehat{se}^* with the convention of the page when se^∗=0\widehat{se}^* = 0. KK is the bootstrap distribution function of T∗T^*, K^b\hat K_b the empirical distribution function of the bb replicates, and K−1(p)=inf⁡{t:K(t)≥p}K^{-1}(p) = \inf\{t : K(t) \ge p\} for any distribution function KK, with values in [−∞,∞][-\infty, \infty].

This file proves what the algorithm computes and when the ideal interval is exact. It does not prove a coverage level for a nonparametric FF; that coverage is measured on the page.

Claim 1 (lines L6 and L7). tlo=K^b−1(α/2)t_{lo} = \hat K_b^{-1}(\alpha/2) and thi=K^b−1(1−α/2)t_{hi} = \hat K_b^{-1}(1 - \alpha/2).

Proof. The argument of Claim 1 of the percentile interval applies to any bb values in the extended real line: for 0<p<10 < p < 1 and k=⌈bp⌉k = \lceil b p \rceil, at least kk replicates are ≤t(k)∗\le t^*_{(k)} and at most k−1<bpk - 1 < b p are below it. □\square

Claim 2 (inversion). For every θ\theta and every pair tlo≤thit_{lo} \le t_{hi},

tlo≤θ^−θse^≤thi  ⟺  θ^−thi se^≤θ≤θ^−tlo se^.t_{lo} \le \frac{\hat\theta - \theta}{\widehat{se}} \le t_{hi} \iff \hat\theta - t_{hi}\, \widehat{se} \le \theta \le \hat\theta - t_{lo}\, \widehat{se}.

Proof. Multiply by se^>0\widehat{se} > 0, which keeps the order, and subtract θ^\hat\theta; then multiply by −1-1, which reverses both inequalities. □\square

So line L8 returns the set of θ\theta for which the studentized value T=(θ^−θ)/se^T = (\hat\theta - \theta)/\widehat{se} 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 TT has a continuous distribution function KK, the interval [θ^−K−1(1−α/2) se^, θ^−K−1(α/2) se^][\hat\theta - K^{-1}(1 - \alpha/2)\, \widehat{se},\ \hat\theta - K^{-1}(\alpha/2)\, \widehat{se}] contains θ\theta with probability exactly 1−α1 - \alpha.

Proof. By Claim 2 the event is K−1(α/2)≤T≤K−1(1−α/2)K^{-1}(\alpha/2) \le T \le K^{-1}(1 - \alpha/2). For a continuous KK, K(K−1(p))=pK(K^{-1}(p)) = p and P(T<K−1(p))=pP(T < K^{-1}(p)) = p as well. So the probability is (1−α/2)−α/2=1−α(1 - \alpha/2) - \alpha/2 = 1 - \alpha. □\square

Claim 3 is the reason for the method: if TT is a pivot, its law does not depend on FF, and a bootstrap that reproduces it gives an exact interval. For normal data and the mean, TT has the Student law with n−1n - 1 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 KK is discrete and only approximates the law of TT.

Claim 4 (affine equivariance). Let c>0c > 0 and dd be real numbers, and suppose est is equivariant: est(cx+d)=(cθ^+d, c se^)\mathrm{est}(c x + d) = (c \hat\theta + d,\ c\, \widehat{se}) for every sample xx. Run the algorithm on xx and on y=cx+dy = c x + d with the same random numbers. If the first run returns [L,U][L, U], the second returns [cL+d, cU+d][c L + d,\ c U + d].

Proof. Line L3 draws the same indices in both runs, so the jj-th resample of yy is cx∗+dc x^* + d. Line L4 gives (cθ^∗+d, c se^∗)(c\hat\theta^* + d,\ c\, \widehat{se}^*). If se^∗>0\widehat{se}^* > 0, then (cθ^∗+d−cθ^−d)/(c se^∗)=tj∗(c\hat\theta^* + d - c\hat\theta - d)/(c\, \widehat{se}^*) = t^*_j. If se^∗=0\widehat{se}^* = 0, then c se^∗=0c\, \widehat{se}^* = 0, and c(θ^∗−θ^)c(\hat\theta^* - \hat\theta) has the sign of θ^∗−θ^\hat\theta^* - \hat\theta, so the convention gives the same value. So both runs have the same tj∗t^*_j, the same tlot_{lo} and thit_{hi}, and line L8 gives cθ^+d−thi c se^=cL+dc\hat\theta + d - t_{hi}\, c\, \widehat{se} = c L + d and likewise cU+dc U + d. □\square

The mean with se^=S/n\widehat{se} = S/\sqrt n is equivariant in this sense. The tests check the claim with c=2c = 2 and d=5d = 5 up to rounding. For c<0c < 0, t∗t^* 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 k=⌈bα/2⌉k = \lceil b\alpha/2 \rceil and k′=⌈b(1−α/2)⌉k' = \lceil b(1 - \alpha/2) \rceil. The upper endpoint is +∞+\infty exactly when at least kk replicates are −∞-\infty, which is the same as at least bα/2b\alpha/2 of them. The lower endpoint is −∞-\infty exactly when at least b−k′+1=⌊bα/2⌋+1b - k' + 1 = \lfloor b\alpha/2 \rfloor + 1 replicates are +∞+\infty, which is the same as more than bα/2b\alpha/2 of them.

Proof. The convention assigns a value in {−∞,0,+∞}\{-\infty, 0, +\infty\} when se^∗=0\widehat{se}^* = 0, so no division by zero happens. In sorted order, t(k)∗=−∞t^*_{(k)} = -\infty exactly when at least kk values are −∞-\infty, and t(k′)∗=+∞t^*_{(k')} = +\infty exactly when at least b−k′+1b - k' + 1 values are +∞+\infty. The identity b−⌈b(1−α/2)⌉=⌊bα/2⌋b - \lceil b(1 - \alpha/2) \rceil = \lfloor b\alpha/2 \rfloor holds because bb is an integer. For an integer count mm, m≥⌈bα/2⌉m \ge \lceil b\alpha/2 \rceil is the same as m≥bα/2m \ge b\alpha/2. By line L8, tlo=−∞t_{lo} = -\infty makes the upper endpoint +∞+\infty, and thi=+∞t_{hi} = +\infty makes the lower endpoint −∞-\infty. □\square

The tests compute the ideal law of T∗T^* by listing all nnn^n resamples for n≤5n \le 5 and compare it with the multiset enumeration of the implementation. For the sample (0,1)(0, 1) the law puts mass 1/41/4 on −∞-\infty, 1/21/2 on 00 and 1/41/4 on +∞+\infty.

What is not proved. For a nonparametric FF and a smooth statistic, Hall (1988) shows by Edgeworth expansions that each one-sided endpoint has coverage error O(n−1)O(n^{-1}), because the bootstrap reproduces the first skewness term of the expansion of the law of TT. 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 0.0070.007.

Cost. The loop runs bb times; each pass draws nn indices and calls est, which costs Θ(n)\Theta(n) for the mean. Line L6 sorts bb values in Θ(blog⁡b)\Theta(b \log b) time. The total is Θ(bn+blog⁡b)\Theta(b n + b \log b) time and Θ(b+n)\Theta(b + n) extra space.

Complexity

Each replicate draws nn indices and calls est, which for the mean costs Θ(n)\Theta(n). The sort costs Θ(blog⁡b)\Theta(b \log b); two selections instead of the sort give Θ(b)\Theta(b) for this step. So the time is Θ(bn+blog⁡b)\Theta(b n + b \log b), and the extra space is Θ(b+n)\Theta(b + n). If no formula for se^∗\widehat{se}^* exists, a nested bootstrap with b2b_2 inner replicates estimates it, and the time becomes Θ(b b2 n)\Theta(b\, b_2\, n).

Properties

  • Second-order accurate. For smooth statistics, TT is asymptotically pivotal, and the bootstrap captures the skewness term of its Edgeworth expansion, so the one-sided coverage error is O(n−1)O(n^{-1}).
  • 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 se^∗\widehat{se}^*. Small, unstable standard errors in some resamples give extreme t∗t^* values and long intervals, as the example shows.
  • Monte Carlo error. The endpoints depend on extreme order statistics of t∗t^*; use b≥1000b \ge 1000.

Variants and improvements

  • Nested bootstrap standard error. When there is no formula, an inner bootstrap of size b2≈25b_2 \approx 25 to 5050 estimates se^∗\widehat{se}^* 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 1−α1 - \alpha quantile cc of ∣T∗∣\lvert T^* \rvert and returns θ^±c se^\hat\theta \pm c\, \widehat{se}. Its two-sided coverage error is O(n−2)O(n^{-2}) under smoothness, but it cannot follow skewness.
  • Bootstrap tests. Inverting the interval gives a bootstrap t test of θ=θ0\theta = \theta_0, 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 nn 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 θ^∗−θ^\hat\theta^* - \hat\theta by se^\widehat{se} instead of se^∗\widehat{se}^* gives a rescaled percentile interval and loses the second-order accuracy.
  • Swapping the quantiles. The upper quantile thit_{hi} 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 t∗t^* 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 ±∞\pm\infty, as line L5 does in the implementation.
  • Outliers. One large value can control both θ^∗\hat\theta^* and se^∗\widehat{se}^* 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.

bootstrap_t_interval.pyI0RawGitHub
# 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 TT up to terms of order n−3/2n^{-3/2}; 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

  1. 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)
  2. 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)
  3. Peter Hall, The Bootstrap and Edgeworth Expansion, Springer, 1992, Chapter 3 (coverage errors of percentile and percentile-t intervals)
  4. 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)
  5. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example