Algorithmcore
Tukey's biweight
Also called Tukey bisquare, bisquare estimator.
A redescending M-estimator whose psi function goes to zero beyond a threshold, so gross outliers get no weight. Fitted by iteratively reweighted least squares.
- Time
- Θ(n log n + n t) worst · averageΘ(n log n) best
- Space
- Θ(n) extra
- Where
- n = the sample size; t = the number of reweighting steps
- Model
- Real RAM
- Properties
- deterministicnot exact in exact arithmeticnot in placeofflinenot randomized
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Albert E. Beaton, John W. Tukey, 1974
Tukey's biweight is the M-estimator of location with the loss for and beyond. Its derivative rises, falls back, and is exactly 0 for . So a point farther than from the estimate has no influence at all. The loss is not convex, so the estimate is computed by iteratively reweighted means from a robust start, the median, with the scale fixed at the normalized MAD. Each step does not increase the objective. With the efficiency at the normal is 95%.
In plain words
Each point gets a weight between 0 and 1. A point near the current center gets a weight close to 1. A point farther away gets a smaller weight, and a point beyond a cutoff gets weight 0. The new center is the weighted average of the points. Then the weights are computed again for the new center, and so on until the center stops moving. A point far from the bulk of the data is not only weakened, it is ignored. The cutoff is a number of typical spreads, measured with a robust spread.
The problem
The input is a sample , a constant (the default is ) and a tolerance. The scale is . The target is a local minimizer of
that is a root of , reached from the median. The output is the point where the iteration stops. can have several local minima, and the method does not promise the global one.
The algorithm
The weight function is for and 0 otherwise.
BIWEIGHT(x, c, tol) ▷ x[1 .. n]- 1
θ ← median(x); σ ← MAD(x)/Φ⁻¹(3/4); if σ = 0 then return θ - 2
repeat - 3
r_i ← (x_i − θ)/σ for each i - 4
w_i ← (1 − (r_i/c)²)² if |r_i| < c, else 0 - 5
θ' ← Σ w_i x_i / Σ w_i - 6
if |θ' − θ| ≤ tol · σ then return θ' - 7
θ ← θ'
A fixed point of lines L3 to L5 satisfies , which is . So the method stops near a root of the estimating equation.
Example
Take and . The median is . The absolute deviations are , so the MAD is and . Then , and the point 30 is beyond the cutoff from the start.
| step | weights of | |||
|---|---|---|---|---|
| 0 | 5.5 | 0.787, 0.959, 0.995, 0.995, 0.959, 0 | 5.2582 | |
| 1 | 4.9134 | 0.850, 0.985, 1.000, 0.978, 0.921, 0 | 5.0762 | |
| 2 | 4.8494 | 5.0741 | ||
| 3 | 4.8425 | 5.0740 |
The iteration stops after 13 steps at , with . It is the mean of the first five points weighted almost equally. The value 30 has no effect: moving it to gives the same result. The mean is 9 and the median is .
Correctness
Two facts make the iteration safe, and proofs/correctness.md proves both. First, with concave and nondecreasing on , and . A concave function lies below its tangent, so lies below a weighted sum of squares that touches it at the current . Line L5 minimizes that sum of squares. So , with equality only when . Second, the sum of the weights in line L5 is never 0. At the median, at least half of the points are within one MAD, that is within , so at the start is less than . If all weights were 0 at a later step, would be there, against the descent. The proof also shows that every limit point of the iterates is a root of the estimating equation.
Proof: The biweight reweighting step never increases the objective and never divides by zerofull
Setting. are real, where , and (the case ends at line L1). Write , , and let be the value of line L5.
Lemma 1. Put for and for . Then , is concave and nondecreasing on , it is differentiable, and .
Proof. The identity is the definition. For , , and for , . Both one-sided derivatives at are 0, so is differentiable with . This function is nonnegative and nonincreasing, so is nondecreasing and concave. Finally for , and both sides are 0 for .
Lemma 2 (descent). If , then , with equality only if .
Proof. Fix with , and put and . A concave differentiable function lies below its tangent lines, so for every
with equality at . is a quadratic in with leading coefficient . Its derivative is , which is 0 at . So is the unique minimizer of , and
The second inequality is strict if , because the minimizer of is unique.
Lemma 3 (positive weights). Let be the median. Then , and every iterate has .
Proof. By the definition of the MAD, at least half of the points, and so at least one, satisfy , that is . For such a point , and all other terms are at most . So . For any , means for all , so . By induction on : if are positive, Lemma 2 gives , so .
Theorem. The iterates of lines L2 to L7 are well defined, is nonincreasing, and every limit point of satisfies .
Proof. Lemma 3 shows that line L5 never divides by 0, and Lemma 2 gives the monotonicity. Each is a weighted mean of the , so the iterates lie in , a compact set, and limit points exist. is bounded below by 0, so converges to some . Let . is continuous, so , and , so . is continuous (it is 0 at ), so is continuous at , and . Then . By Lemma 2, , that is . Since , this is .
What is not claimed. The limit is a stationary point of . It need not be the global minimizer, because is not convex. The stopping rule of line L6 bounds the last step, not the distance to the limit.
Complexity
- Time. The median and the MAD take by sorting, or expected with quickselect. Each step takes . So the total is for steps.
- Steps. The convergence is linear in general. The rate depends on the data: when almost all points have weight near 1, it is fast, and in the example the step length drops by about a factor 9 each step. No bound on in terms of alone holds.
- Space. for the weights.
Properties
The page proves the descent, the positive weights, and the bounded estimate for far outliers. The efficiency and the influence function are computed. The rest is cited.
- Influence function. At the normal law with the scale fixed, . It is 0 for : the rejection point is . The largest value of is at , and with the standardized gross-error sensitivity is , against for the Huber estimator at 95% efficiency.
- Efficiency at the normal. The asymptotic variance is with inside. A numerical integration gives 1.0526 for , an efficiency of 0.950.
- Far outliers are ignored (proved). Let and be the least and the greatest clean point, and let points be replaced by values farther than from . The median is in , because more than half the points are clean. If , every replaced point has weight 0, so is a weighted mean of clean points and is in . So all iterates stay in . The MAD of the sample is at most (the argument is on the Huber estimator page), so the condition holds when the replaced points are far enough. The tests move up to points to and and measure that the estimate stays in ; at points it follows them.
- Not convex. is bounded, so it can have several local minima, for example one near each of two clusters. The root found depends on the start. This page does not prove a breakdown point for arbitrary replacements; for the global minimizer and for regression, see the cited books.
- Equivariance. With the median start and the MAD scale, the computed estimate is location and scale equivariant.
Variants and improvements
- Regression. Replace by residuals and the weighted mean by weighted least squares; see iteratively reweighted least squares. A median start is not enough there; the MM-estimator supplies a high-breakdown start and scale.
- The biweight midvariance. A scale estimate from the same weights, used together with the biweight location in exploratory work.
- Other redescending functions. Hampel's three-part , Andrews' sine for , and the Welsch weight behave in the same way.
- One-step version. One Newton step from the median, with the derivative , gives the same first-order asymptotics if the derivative stays positive.
- Choice of . Smaller values reject more points and lose efficiency at the normal. gives 95%. In units of the raw MAD this is MADs.
When to use it
Use the biweight when gross errors are expected and must have no influence, for example readings from a failing sensor or data entry errors. Use it with a good start and a robust scale. Use the Huber estimator when you want a convex problem with a unique answer. Use the median when the data are very dirty and efficiency matters less. In regression, use it only inside the MM-estimator or another high-breakdown method.
Pitfalls
- Bad start. From the mean, or from a least squares fit, the iteration can converge to a root near the outliers. Always start from the median or another high-breakdown estimate.
- Scale updated with the weights. If is recomputed from the weighted residuals at each step, the descent proof no longer holds, and the scale can implode. Fix it, or update it by an M-scale equation with its own proof.
- Zero MAD. If more than half the values are equal, the MAD is 0 and the method returns the median.
- Units of . Some texts give in units of the raw MAD, others in units of . The value 4.685 is in units of .
- Inference. With redescending the sum can be small or negative in small samples, so the sandwich standard error can be unstable.
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/tukey-biweight/. The authors are listed in prov/9NDW0S.jsonl.
from fractions import Fraction
from statistics import NormalDist
# Phi^{-1}(3/4): the MAD of N(0, 1).
Q3 = NormalDist().inv_cdf(0.75)
def weight(u, c):
"""Biweight weight psi(u)/u = (1 - (u/c)^2)^2 inside (-c, c), 0 outside."""
if abs(u) >= c:
return 0 * u
t = 1 - (u / c) ** 2
return t * t
def psi(u, c):
return u * weight(u, c)
def rho(u, c):
"""Biweight loss (c^2/6)(1 - (1 - (u/c)^2)^3) inside, c^2/6 outside."""
if abs(u) >= c:
return c * c / 6
return c * c / 6 * (1 - (1 - (u / c) ** 2) ** 3)
def exact_median(xs):
s = sorted(xs)
n = len(s)
if n % 2 == 1:
return s[n // 2]
lo, hi = s[n // 2 - 1], s[n // 2]
if isinstance(lo, (int, Fraction)) and isinstance(hi, (int, Fraction)):
return Fraction(lo + hi, 2)
return (lo + hi) / 2
def mad_scale(xs):
m = exact_median(xs)
return float(exact_median([abs(v - m) for v in xs])) / Q3
def objective(xs, theta, c, sigma):
return sum(rho((v - theta) / sigma, c) for v in xs)
def biweight_step(xs, theta, c, sigma):
"""One reweighting step: the weighted mean with weights w((x_i - theta)/sigma)."""
w = [weight((v - theta) / sigma, c) for v in xs] # @line L4
sw = sum(w)
if sw == 0:
raise ValueError("all weights are zero")
return sum(wi * v for wi, v in zip(w, xs)) / sw # @line L5
def biweight_location(xs, c=4.685, sigma=None, tol=1e-12, max_iter=1000, trace=None):
"""Tukey biweight M-estimate of location by IRLS from the median, with the scale fixed.
sigma defaults to the normalized MAD. Returns a root of sum psi((x_i - theta)/sigma) = 0
(up to tol); the objective does not increase along the iterates. If trace is a list,
the iterates are appended to it. The iteration runs in floating point; use
biweight_step for exact single steps.
"""
x = [float(v) for v in xs]
if not x:
raise ValueError("empty sample")
theta = exact_median(x) # @line L1
sigma = mad_scale(x) if sigma is None else float(sigma) # @line L1
c = float(c)
if sigma == 0:
return theta # @line L1
if trace is not None:
trace.append(theta)
for _ in range(max_iter): # @line L2
new = biweight_step(x, theta, c, sigma) # @line L3
if trace is not None:
trace.append(new)
if abs(new - theta) <= tol * sigma: # @line L6
return new
theta = new # @line L7
return theta
test_tukey_biweight.py 114 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/tukey-biweight/. The authors are listed in prov/9NDW0S.jsonl.
import random
from fractions import Fraction
from statistics import NormalDist
import pytest
from tukey_biweight import (
biweight_location, biweight_step, exact_median, mad_scale, objective, psi, weight,
)
def test_one_step_by_hand() -> None:
# x = (2, 4, 5, 6, 7, 30), theta = 11/2, sigma = 2, c = 3.
x = [2, 4, 5, 6, 7, 30]
th = exact_median(x)
assert th == Fraction(11, 2)
w = [weight(Fraction(v - th) / 2, Fraction(3)) for v in x]
assert w == [Fraction(9025, 20736), Fraction(225, 256), Fraction(20449, 20736),
Fraction(20449, 20736), Fraction(225, 256), 0]
assert biweight_step(x, th, Fraction(3), Fraction(2)) == Fraction(63352, 12339)
def test_page_example() -> None:
x = [2, 4, 5, 6, 7, 30]
trace = []
t = biweight_location(x, trace=trace)
assert mad_scale(x) == pytest.approx(1.5 / 0.6744897501960817)
assert [round(v, 4) for v in trace[:4]] == [5.5, 4.9134, 4.8494, 4.8425]
assert t == pytest.approx(4.841634, abs=1e-6)
# The point 30 has weight 0 at the end.
assert weight((30 - t) / mad_scale(x), 4.685) == 0
def test_monotone_descent_and_stationarity() -> None:
rng = random.Random(1)
for _ in range(200):
n = rng.randint(3, 40)
x = [rng.gauss(0.0, 1.0) for _ in range(n)]
for _ in range(rng.randint(0, n // 2 - 1 if n >= 4 else 0)):
x[rng.randrange(n)] = rng.uniform(-100.0, 100.0)
c = rng.choice([2.0, 3.0, 4.685, 6.0])
sigma = mad_scale(x)
trace = []
t = biweight_location(x, c=c, trace=trace)
if sigma == 0:
continue
q = [objective(x, v, c, sigma) for v in trace]
assert all(b <= a + 1e-12 for a, b in zip(q, q[1:]))
assert q[0] < n * c * c / 6 # the start has a point inside, so the weights stay positive
assert abs(sum(psi((v - t) / sigma, c) for v in x)) <= 1e-8 * n
def test_exact_steps_do_not_increase_the_objective() -> None:
rng = random.Random(2)
for _ in range(60):
n = rng.randint(2, 12)
x = [rng.randint(-20, 20) for _ in range(n)] + [rng.randint(100, 200)]
c, sigma = Fraction(rng.randint(2, 6)), Fraction(rng.randint(1, 5))
th = exact_median(x)
for _ in range(3):
if sum(weight((v - th) / sigma, c) for v in x) == 0:
break
new = biweight_step(x, th, c, sigma)
assert objective(x, new, c, sigma) <= objective(x, th, c, sigma)
th = new
def test_far_outliers_have_no_influence() -> None:
rng = random.Random(3)
for _ in range(50):
clean = [rng.gauss(10.0, 2.0) for _ in range(rng.randint(5, 30))]
a = biweight_location(clean + [1e6, 2e6])
b = biweight_location(clean + [1e9, 5e12])
assert a == b
def test_efficiency_at_the_normal() -> None:
# Simpson's rule: E psi^2 / (E psi')^2 = 1 / 0.95 for c = 4.685.
phi = NormalDist().pdf
c, m = 4.685, 4000
h = 2 * c / m
def simpson(f):
s = f(-c) + f(c)
for i in range(1, m):
s += (4 if i % 2 else 2) * f(-c + i * h)
return s * h / 3
e2 = simpson(lambda z: psi(z, c) ** 2 * phi(z))
e1 = simpson(lambda z: (1 - (z / c) ** 2) * (1 - 5 * (z / c) ** 2) * phi(z))
assert e1 * e1 / e2 == pytest.approx(0.95, abs=1e-4)
def test_empirical_breakdown() -> None:
rng = random.Random(4)
for n in (5, 6, 9, 10, 15):
clean_all = [rng.uniform(0.0, 10.0) for _ in range(n)]
for s in (1e6, 1e12):
for m in range((n + 1) // 2 + 1):
clean = clean_all[: n - m]
t = biweight_location(clean + [s] * m)
if 2 * m < n:
assert min(clean) <= t <= max(clean)
else:
assert t >= s / 4
def test_errors() -> None:
with pytest.raises(ValueError):
biweight_location([])
assert biweight_location([4, 4, 4, 9]) == 4
History
Beaton and Tukey (1974) introduced the biweight to fit polynomials to band spectra that contained gross errors, and computed it by reweighting. Holland and Welsch (1977) studied iteratively reweighted least squares for the bisquare and other weight functions and the constants for 95% efficiency. Hampel, Ronchetti, Rousseeuw and Stahel (1986) placed redescending estimators in the influence function theory with the rejection point. Yohai (1987) used the biweight in the MM-estimator to obtain high breakdown and high efficiency together.
References
- Albert E. Beaton and John W. Tukey, The fitting of power series, meaning polynomials, illustrated on band-spectroscopic data, Technometrics 16(2), 1974, pp. 147 to 185 (the biweight weight function and its use in reweighted fits)
- Paul W. Holland and Roy E. Welsch, Robust regression using iteratively reweighted least-squares, Communications in Statistics, Theory and Methods 6(9), 1977, pp. 813 to 827 (iteratively reweighted least squares with the bisquare and other weight functions)
- Ricardo A. Maronna, R. Douglas Martin, Victor J. Yohai and Matías Salibián-Barrera, Robust Statistics, Theory and Methods (with R), second edition, Wiley, 2019, Chapter 2 (redescending M-estimators of location, the bisquare, the choice of c and the reweighting algorithm)
- Frank R. Hampel, Elvezio M. Ronchetti, Peter J. Rousseeuw and Werner A. Stahel, Robust Statistics, The Approach Based on Influence Functions, Wiley, 1986, Chapter 2 (redescending estimators and the rejection point)
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 9NDW0S Edit this page History Report an error Markdown