Algorithmcore
Jackknife
Also called Quenouille-Tukey jackknife, leave-one-out jackknife.
Recompute a statistic with each observation left out in turn, and combine the n values to estimate its bias and variance. It fails for nonsmooth statistics like the median.
- Time
- Θ(n^2) worstΘ(n) best
- Space
- Θ(n) extra
- Where
- n = the sample size
- Model
- Real RAM
- Properties
- deterministicnot exactofflinenot randomizediterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Maurice Quenouille, John Tukey, 1949
The jackknife computes a statistic more times, each time with one observation left out, and uses the spread of these values to estimate the bias and the standard error of the statistic. It needs no model and no random numbers, and it costs evaluations of the statistic. It is exact for the mean and for the plug-in variance, it removes the term of the bias of smooth statistics, and it turns a V-statistic into the corresponding U-statistic. It fails for statistics that are not smooth, such as the median, where its variance estimate does not converge.
In plain words
You have a statistic, such as an average or a correlation, from data points, and you want to know how much it would change with other data. Leave out the first point and compute the statistic again. Put it back, leave out the second point, and compute again. Do this for every point. If the results are close together, the statistic is stable. If they spread out, it is not. A fixed formula turns this spread into a standard error and turns the average shift into an estimate of the bias.
The method works well for statistics that change smoothly when one point moves. It works badly for the median, which jumps between only two or three values when one point is left out.
The problem
Input. A sample , independent and identically distributed, with , and a statistic that is defined for samples of size and and does not depend on the order of the data. Write .
Output. An estimate of the bias , the bias-corrected estimate, and an estimate of the variance of .
Definitions. Let be on the sample without , and . Then
The factor in both formulas makes them exact for the mean. Pseudo-values give the same results: is their mean, and is their sample variance divided by , as if they were independent observations.
The algorithm
JACKKNIFE(x_1, ..., x_n, s)- 1
θ̂ ← s(x_1, ..., x_n) - 2
for i ← 1 to n do - 3
θ̂_(i) ← s(x_1, ..., x_{i-1}, x_{i+1}, ..., x_n) - 4
θ̂_(·) ← (1/n) Σ_i θ̂_(i) - 5
bias ← (n - 1) (θ̂_(·) - θ̂) - 6
var ← ((n - 1)/n) Σ_i (θ̂_(i) - θ̂_(·))² - 7
return θ̂ - bias, bias, var
The implementation uses only addition, subtraction, multiplication and division, so with Fraction data it computes every quantity exactly. This is how the tests check the identities below.
Example
The treatment group of the mouse data of Efron and Tibshirani (Table 2.1) has survival times
Mean. . Leaving out each value in turn gives , whose mean is again , so the bias estimate is 0. Line L6 gives , and the jackknife standard error is , which is exactly . The exact bootstrap standard error is ; the bootstrap uses the plug-in variance with divisor .
Plug-in variance. . The bias estimate is , and the corrected value is , the unbiased sample variance.
Median. . The seven leave-one-out medians take only three values: three times, three times and once. The jackknife standard error is , close to the bootstrap value by chance. The bias estimate is , and the "corrected" median is , larger than every value but one. Do not use the jackknife for the median.
Measured failure for the median. For 2000 uniform samples of each size, the ratio of the jackknife variance of the median to its true variance had these summaries (seed 11):
| mean ratio | median ratio | ratio below 0.5 | |
|---|---|---|---|
| 10 | 1.62 | 0.48 | 51% |
| 40 | 1.79 | 0.47 | 51% |
| 160 | 1.96 | 0.49 | 51% |
The ratio does not concentrate at 1 as grows. It converges in law to , which has mean 2 and median (Efron and Tibshirani, Chapter 11).
Correctness
The proof is in proofs/correctness.md. It shows these facts, all exact:
- For the mean, the bias estimate is 0 and , with the sample variance with divisor .
- If for every , then . The term of the bias is removed completely.
- For every symmetric kernel of degree 2, the jackknife-corrected V-statistic equals the U-statistic . With this turns the plug-in variance into .
- The pseudo-value form gives the same estimate and variance.
- For the median with , , a function of a single spacing.
The tests check each identity with Fraction data, and check the U-statistic against its definition as a sum over all pairs.
Proof: Exact identities of the jackknifefull
We use the pseudocode and the notation of the page: is the statistic without , their mean, and . is the mean and . Lines L1 to L7 compute these formulas directly, so it remains to prove what the formulas give.
Claim 1 (the mean). If is the mean, the bias estimate is 0, and .
Proof. , so and the bias estimate is 0. Further , so
The same computation with in place of covers every statistic .
Claim 2 (bias reduction). Suppose that for every sample size , with constants . Then . In particular, if , is unbiased.
Proof. Each is on independent observations, so . Then
Claim 3 (V-statistics become U-statistics). Let be symmetric, , and let be the V-statistic . Then
Proof. Write and , so . Leaving out removes from and the terms , , from :
Summing over , each diagonal term is removed once and each pair is removed twice, once for and once for . So
Then
Corollary (the plug-in variance). With , , the plug-in variance, and . So the jackknife turns the plug-in variance into the unbiased sample variance, and its bias estimate is .
The identity follows by expanding : the double sum is . The identity follows in the same way, because is half of the double sum.
Claim 4 (pseudo-values). With , the mean of the is , and .
Proof. Averaging gives . Then , so
For the mean, : the pseudo-values are the data.
Claim 5 (the median with an even sample size). Let , let be the median, and let be the order statistics. Then .
Proof. Without one observation, values remain and the median is the -th smallest of them. If the left-out observation is one of , the -th smallest of the rest is . If it is one of , it is . With ties the same holds for the values, whichever copy is left out. So of the equal and equal , their mean is , and each deviation is with . Hence .
So the estimate depends on a single spacing of the sample. For a density that is positive at the median, is asymptotically exponential with mean , so divided by the asymptotic variance does not converge to 1. This explains the table on the page, which is a measurement.
Claim 6 (the delete-d jackknife for the mean). For every , the delete- variance , where runs over the subsets of size and is the mean of the , equals .
Proof. By symmetry each is in the same number of subsets, so . The mean of a random subset of size drawn without replacement from has variance , where is the population variance of the values. This is the variance of the uniform law over the subsets, so
and multiplying by gives .
With the delete- formula is the formula of line L6, because the subsets are the leave-one-out samples. The tests check Claims 1 to 6 with exact Fraction arithmetic, and Claim 3 against the definition of the U-statistic as a sum over all pairs, for four kernels.
Cost. Line L3 runs times on values, which costs for a statistic of linear cost. Lines L4 to L6 cost . The values need space.
Complexity
Lines L2 and L3 evaluate on samples of size . For a statistic that costs , such as a mean or a variance, this is time, and lines L4 to L6 add . For the mean and other statistics built from sums, each follows from the full sums by one subtraction, and the whole jackknife costs . The extra space is for the values. The delete- jackknife over all subsets is much more expensive and is usually done on a random selection of subsets.
Properties
- Deterministic. The same data always give the same result, unlike the bootstrap.
- Exact for linear statistics. For a statistic of the form , the variance estimate is exactly the unbiased estimate .
- Second-order bias reduction. The corrected estimate has bias when the bias has an expansion in powers of . The variance of the corrected estimate can be larger than that of .
- Conservative variance. Efron and Stein (1981) showed that, for any symmetric statistic of observations, . So the jackknife variance tends to be too large rather than too small.
- A linear approximation of the bootstrap. The jackknife variance is, up to the factor , the bootstrap variance of the best linear approximation of the statistic in the resampling weights. It agrees with the bootstrap for smooth statistics and differs for nonsmooth ones.
- Inconsistent for the median and other quantiles. The delete-1 estimate depends on one or two spacings of the order statistics, as the example shows.
Variants and improvements
- Delete- jackknife. Leave out observations at a time and use over the subsets . For the mean it gives for every . Shao and Wu (1989) proved that it is consistent for the sample quantiles when and . With it is closely related to subsampling with subsamples of size .
- Grouped jackknife. Split the data into groups and leave out one group at a time. This is Quenouille's original form and reduces the cost to evaluations.
- Infinitesimal jackknife. Replace the leave-one-out differences by derivatives of the statistic in the direction of each point, which are the empirical influence function values. This gives the delta-method variance .
- Acceleration of BCa. The skewness of the leave-one-out values gives the acceleration of the BCa interval.
- Jackknife+ prediction intervals. In predictive inference, leave-one-out residuals give prediction intervals with guaranteed coverage; see conformal prediction.
- Leave-one-out cross-validation. The same loop that leaves out each point, applied to prediction error rather than to bias and variance, is cross-validation.
When to use it
Use the jackknife for a quick, deterministic standard error or bias correction of a smooth statistic: a mean, a ratio of means, a correlation, a regression coefficient or a smooth function of moments. It is useful when the statistic is expensive and is small, so that evaluations are cheaper than the of the bootstrap. Use the bootstrap or the delete- jackknife for quantiles and other nonsmooth statistics, and the bootstrap when you need a confidence interval rather than a standard error.
Pitfalls
- The median and other quantiles. The variance estimate is inconsistent, and the bias correction can be absurd, as the example shows.
- Using the usual variance formula. The leave-one-out values are much closer together than independent replicates, because any two of them share observations. Their ordinary sample variance, with the factor in place of , is too small by the factor .
- Variance of the corrected estimate. The jackknife variance estimates the variance of ; it is often used for too, which is only approximately right.
- Dependent data. Leaving out single observations of a time series ignores the dependence. Leave out blocks, as the block bootstrap resamples blocks.
- Bias correction can increase the error. Removing a small bias can add more variance than it removes in mean squared error; report both and .
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/jackknife-resampling/. The authors are listed in prov/DBF7V5.jsonl.
import itertools
import math
from typing import Callable, NamedTuple, Sequence
Statistic = Callable[[Sequence], object]
class JackknifeResult(NamedTuple):
estimate: object # theta_hat, the statistic on the full sample
leave_one_out: list # theta_(i), i = 1..n
bias: object # (n - 1) (theta_(.) - theta_hat)
corrected: object # theta_hat - bias
variance: object # ((n - 1)/n) sum (theta_(i) - theta_(.))^2
def jackknife(x: Sequence, statistic: Statistic) -> JackknifeResult:
"""The leave-one-out jackknife of Quenouille and Tukey.
The arithmetic uses + - * / only, so Fraction data give exact results.
"""
n = len(x)
if n < 2:
raise ValueError("the jackknife needs at least two observations")
theta_hat = statistic(list(x)) # @line L1
loo = []
for i in range(n): # @line L2
loo.append(statistic(list(x[:i]) + list(x[i + 1 :]))) # @line L3
theta_dot = sum(loo[1:], loo[0]) / n # @line L4
bias = (n - 1) * (theta_dot - theta_hat) # @line L5
variance = (n - 1) * sum(((t - theta_dot) ** 2 for t in loo[1:]), (loo[0] - theta_dot) ** 2) / n # @line L6
return JackknifeResult(theta_hat, loo, bias, theta_hat - bias, variance) # @line L7
def pseudo_values(x: Sequence, statistic: Statistic) -> list:
"""Tukey's pseudo-values n theta_hat - (n - 1) theta_(i)."""
r = jackknife(x, statistic)
n = len(x)
return [n * r.estimate - (n - 1) * t for t in r.leave_one_out]
def delete_d_variance(x: Sequence, statistic: Statistic, d: int):
"""The delete-d jackknife variance (n - d)/(d C(n, d)) sum_S (theta_S - mean)^2 over all
subsets S that leave out d observations. It lists all C(n, d) subsets: small n only."""
n = len(x)
if not 1 <= d < n:
raise ValueError("need 1 <= d < n")
values = [statistic([x[i] for i in keep]) for keep in itertools.combinations(range(n), n - d)]
count = len(values)
mean = sum(values[1:], values[0]) / count
ss = sum(((v - mean) ** 2 for v in values[1:]), (values[0] - mean) ** 2)
return (n - d) * ss / (d * count)
def mean(xs: Sequence):
return sum(xs[1:], xs[0]) / len(xs)
def plugin_variance(xs: Sequence):
"""The plug-in variance with divisor n, a V-statistic of degree 2."""
m = mean(xs)
return sum(((v - m) ** 2 for v in xs[1:]), (xs[0] - m) ** 2) / len(xs)
def median(xs: Sequence):
s = sorted(xs)
k = len(s)
if k % 2:
return s[k // 2]
return (s[k // 2 - 1] + s[k // 2]) / 2
def jackknife_se(x: Sequence[float], statistic: Statistic) -> float:
return math.sqrt(jackknife(x, statistic).variance)
test_jackknife_resampling.py 131 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/jackknife-resampling/. The authors are listed in prov/DBF7V5.jsonl.
import itertools
import math
import random
from fractions import Fraction
import pytest
from jackknife_resampling import delete_d_variance, jackknife, mean, median, plugin_variance, pseudo_values
MOUSE_TREATMENT = [94, 197, 16, 38, 99, 141, 23]
def sample_variance(xs):
"""S^2 with divisor n - 1, by its definition."""
m = Fraction(sum(xs), len(xs))
return sum((Fraction(v) - m) ** 2 for v in xs) / (len(xs) - 1)
def random_fractions(rng, n):
return [Fraction(rng.randint(-50, 50), rng.randint(1, 6)) for _ in range(n)]
def test_variance_of_the_mean_is_s2_over_n():
rng = random.Random(1)
for n in range(2, 12):
x = random_fractions(rng, n)
r = jackknife(x, mean)
assert r.variance == sample_variance(x) / n
assert r.bias == 0 and r.corrected == mean(x)
def test_jackknife_of_plugin_variance_is_s2():
rng = random.Random(2)
for n in range(2, 12):
x = random_fractions(rng, n)
r = jackknife(x, plugin_variance)
assert r.corrected == sample_variance(x)
assert r.bias == -sample_variance(x) / n # E(plug-in) - sigma^2 = -sigma^2/n
def u_statistic_by_definition(x, kernel):
"""The average of the kernel over all 2-subsets of the sample."""
pairs = list(itertools.combinations(range(len(x)), 2))
return sum((kernel(x[i], x[j]) for i, j in pairs), Fraction(0)) / len(pairs)
def v_statistic(kernel):
def v(xs):
n = len(xs)
return sum((kernel(a, b) for a in xs for b in xs), Fraction(0)) / (n * n)
return v
KERNELS = [
lambda a, b: (a - b) ** 2 / 2, # U = S^2
lambda a, b: abs(a - b), # Gini mean difference
lambda a, b: Fraction(int(a + b > 0)), # Wilcoxon one-sample kernel
lambda a, b: a * b, # U estimates mu^2
]
def test_jackknifed_v_statistic_is_the_u_statistic():
rng = random.Random(3)
for n in range(2, 9):
x = random_fractions(rng, n)
for h in KERNELS:
assert jackknife(x, v_statistic(h)).corrected == u_statistic_by_definition(x, h)
x = random_fractions(rng, 6)
assert u_statistic_by_definition(x, KERNELS[0]) == sample_variance(x)
def test_pseudo_values():
rng = random.Random(4)
for n in range(2, 10):
x = random_fractions(rng, n)
r = jackknife(x, plugin_variance)
pv = pseudo_values(x, plugin_variance)
assert sum(pv) / n == r.corrected
assert sample_variance(pv) / n == r.variance
assert pseudo_values(x, mean) == x # the pseudo-values of the mean are the data
def test_median_formula_and_inconsistency():
rng = random.Random(5)
for m in range(1, 7):
n = 2 * m
x = sorted(random_fractions(rng, n))
gap = x[m] - x[m - 1]
assert jackknife(x, median).variance == Fraction(n - 1, 4) * gap**2
# For odd n the leave-one-out medians take only three values.
loo = jackknife(MOUSE_TREATMENT, median).leave_one_out
assert sorted(set(loo)) == [66, Fraction(137, 2), Fraction(193, 2)]
def test_delete_d_reduces_to_delete_one_and_is_exact_for_the_mean():
rng = random.Random(6)
for n in range(3, 9):
x = random_fractions(rng, n)
assert delete_d_variance(x, plugin_variance, 1) == jackknife(x, plugin_variance).variance
for d in range(1, n):
assert delete_d_variance(x, mean, d) == sample_variance(x) / n
def test_median_variance_estimates_by_simulation():
"""Measured, not proved: the delete-1 jackknife variance of the median does not settle
as n grows, while the delete-d version with d near sqrt(n) stays closer to the truth."""
rng = random.Random(7)
n, reps = 16, 60
ratios_one, ratios_d = [], []
for _ in range(reps):
x = [rng.random() for _ in range(n)]
ratios_one.append(jackknife(x, median).variance)
ratios_d.append(delete_d_variance(x, median, 4))
true_var = 1 / (4 * (n + 2)) # variance of the uniform median, about 1/(4n)
spread_one = math.sqrt(sum((v / true_var - 1) ** 2 for v in ratios_one) / reps)
spread_d = math.sqrt(sum((v / true_var - 1) ** 2 for v in ratios_d) / reps)
assert spread_d < spread_one
def test_float_example_and_errors():
r = jackknife([float(v) for v in MOUSE_TREATMENT], mean)
s2 = sample_variance(MOUSE_TREATMENT)
assert math.isclose(math.sqrt(r.variance), math.sqrt(s2 / 7), rel_tol=1e-12)
with pytest.raises(ValueError):
jackknife([1.0], mean)
with pytest.raises(ValueError):
delete_d_variance([1, 2, 3], mean, 3)
History
Maurice Quenouille introduced the idea in 1949, to reduce the bias of estimates of serial correlation by combining estimates on parts of the sample, and in 1956 gave the leave-one-out form . John Tukey, in a 1958 abstract, saw that the pseudo-values behave approximately like independent observations, which gives a variance estimate and approximate t intervals, and named the method after the pocket knife that serves many purposes. Rupert Miller studied its theory in the 1960s and 1970s. Bradley Efron's 1979 bootstrap paper presented the bootstrap as "another look at the jackknife", and Efron and Stein (1981) proved that the jackknife variance is biased upward. Shao and Wu (1989) developed the delete- jackknife.
References
- M. H. Quenouille, Approximate tests of correlation in time-series, Journal of the Royal Statistical Society, Series B 11(1), 1949, pp. 68 to 84 (the bias reduction by estimates on parts of the sample)
- M. H. Quenouille, Notes on bias in estimation, Biometrika 43(3-4), 1956, pp. 353 to 360 (the leave-one-out bias correction n θ̂ - (n - 1) θ̂_(·))
- J. W. Tukey, Bias and confidence in not-quite large samples, 1958, p. 614 (the pseudo-values and the jackknife variance estimate)
- Bradley Efron and Robert J. Tibshirani, An Introduction to the Bootstrap, Chapman and Hall, 1993, Chapter 11 (the jackknife, its bias and variance estimates, the failure for the median and the delete-d jackknife)
- B. Efron and C. Stein, The jackknife estimate of variance, The Annals of Statistics 9(3), 1981, pp. 586 to 596 (the jackknife variance estimate is biased upward)
- Jun Shao and C. F. J. Wu, A general theory for jackknife variance estimation, The Annals of Statistics 17(3), 1989, pp. 1176 to 1197 (the delete-d jackknife and its consistency for the sample quantiles)
Where this leads
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 DBF7V5 Edit this page History Report an error Markdown