Algorithmadvanced
Least trimmed squares
Also called LTS regression, FAST-LTS.
Regression that minimizes the sum of the h smallest squared residuals. FAST-LTS finds it by random starts and concentration steps that never increase the objective.
- Time
- O(S c (n p^2 + p^3 + n log n)) worst
- Space
- O(n p + p^2) extra
- Where
- S = the number of random starts; c = the number of C-steps for each start; n = the number of rows; p = the number of coefficients
- Model
- Real RAM
- Properties
- randomizednot exactnot in placeofflinerandomizedregression equivariant
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Peter J. Rousseeuw, Katrien Van Driessen, Annick M. Leroy, Jan Ámos Víšek, 1984
Least trimmed squares (LTS) fits a regression by least squares on the best part of the data. For a coverage , usually a little more than , it chooses the coefficients that make the sum of the smallest squared residuals as small as possible. The other points, the worst fitted ones, do not count at all. So LTS has a breakdown point near 1/2, like least median of squares, but it converges at the usual rate . The exact fit needs a search over subsets. FAST-LTS finds a good one by random starts and concentration steps (C-steps): each C-step refits least squares on the best fitted points, and it never makes the objective larger.
In plain words
Pick a line, any line. Look at the points closest to it, just over half of them, and fit ordinary least squares to those points only. The new line fits those points at least as well as the old one, so the total squared error of its closest half can only go down. Repeat until the chosen points stop changing.
This walk downhill can stop at the wrong place: if it starts on a line through bad points, it may settle on a fit of the bad points. So we start many times, from lines through a few random points, and keep the best result. If more than half of the points are good, some starts will use only good points, and those lead to a good fit.
The problem
The input is a design with rows and responses , in the notation , and a coverage with . For coefficients , let , and let be the squared residuals in order. The LTS fit minimizes
The usual choice is , which gives the highest breakdown point. A larger , such as , gives more efficiency and a lower breakdown point. With , LTS is ordinary least squares.
The algorithm
Equivalently, LTS looks for the subset of rows whose least squares fit has the smallest residual sum of squares. FAST-LTS searches this space with C-steps. This is the core of the method; the full FAST-LTS adds the refinements listed under the variants.
FAST-LTS(X, y, h, S)- 1
for each of S random starts do - 2
b ← the exact fit through p random rows ▷ draw again if they are singular - 3
repeat - 4
b' ← C-STEP(b) - 5
if Q(b') ≥ Q(b) then leave the repeat loop; b ← b' - 6
if Q(b) is the smallest so far then b* ← b - 7
return b* and Q(b*) C-STEP(b) ▷ concentration step- 8
H ← the h rows with the smallest r_i(b)² - 9
return the least squares fit on the rows of H
The C-step works because of one chain of inequalities. Let be the rows best fitted by , and the least squares fit on . The smallest squared residuals of sum to at most its sum over ; that is at most the sum of over , because is the least squares fit there; and that is :
Example
Ten points; the first seven lie near the line , and the last three are bad.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
| 3 | 5 | 8 | 9 | 11 | 14 | 15 | 2 | 1 | 0 |
Here (intercept and slope) and . Least squares on all ten points gives , with the wrong sign of the slope. The exact LTS fit, found by trying all subsets, uses the points :
C-steps from three starts give:
| start (line L2) | at start | subset of the C-step | fit after it | after it | next C-step |
|---|---|---|---|---|---|
| points 1, 2: | 1 | {1, 2, 3, 4, 5, 7} | 57/70 ≈ 0.814 | no change | |
| points 8, 9: | 19 | {2, 3, 4, 8, 9, 10} | 1625/87 ≈ 18.68 | no change | |
| points 2, 9: | 19.73 | {1, 2, 3, 8, 9, 10} | 1816/93 ≈ 19.53 | no change |
The objective drops at every step, as the C-step theorem says, but each run stops at a fixed point. The first start reaches a good fit with , close to the best value 0.708 but not equal to it: the line fits points 3 and 6 equally badly, and the tie is broken toward point 3. The other two starts stop at fits of the bad points. On these data none of the 45 starts from a pair of points reaches the global minimum; the test file checks this. FAST-LTS with 50 starts returns , a good fit but an upper bound on the minimum. In the test file, on 30 random data sets with from 6 to 12 and a third of the points bad, 20 starts reached the exact minimum every time. These are measurements on small data, not a guarantee.
Correctness
For any , the sum of the smallest squared residuals is the least residual sum over sets of rows. So the minimum of is the minimum over -subsets of the least squares residual sum on the subset, and the exact LTS fit is least squares on the best subset. The C-step never increases , by the chain above, and while it decreases strictly it cannot use the same subset twice, so each run stops after at most steps, and in practice after a few. The global minimizer is a fixed point of the C-step, but other fixed points exist, so FAST-LTS returns an upper bound on the minimum, which is exact when a start falls in the basin of the global minimum. The full proofs are below.
Proof: LTS is least squares on the best h-subset, and the C-step never increases the trimmed objectivefull
Setting. The data are rows of the design and responses , in the notation . For a coefficient vector , , and the squared residuals sorted are . For the LTS objective is
and for a set of rows, .
Part 1. LTS as a search over subsets
Lemma 1. For every , , and the minimum is attained by the rows with the smallest squared residuals.
Proof. Any of the numbers sum to at least the sum of the smallest, and the smallest attain it.
Theorem 2. . If attains the outer minimum, the least squares fit on is an LTS fit.
Proof. By Lemma 1, , and two minima can be swapped. For a fixed , the inner minimum over is the least squares fit on the rows of . If is that fit for , then .
So the enumeration over all subsets in the test file returns the exact LTS fit, when every subset has a full-rank design. When a subset is rank deficient, its least squares fit is not unique, but its minimum value is still well defined; the code skips such subsets, which is safe when the data have distinct design rows in the needed sense, as in the tests.
Part 2. The concentration step
The C-step of lines L8 and L9 takes , lets be the rows with the smallest , and returns the least squares fit on .
Theorem 3 (Rousseeuw and Van Driessen). , with equality only if is itself a least squares fit on (when the fit on is unique, only if ).
Proof. By Lemma 1, . Since minimizes , . And by the choice of . So
If , the middle inequality is an equality, so also minimizes .
Corollary 4 (termination). The loop of lines L3 to L5 stops after at most C-steps.
Proof. Let be the fits made by the C-steps, and the subsets they were fitted on. Line L9 computes the fit from the subset alone, so gives . While the loop runs, strictly, so the fits are all different, and so are the subsets. There are subsets.
In practice the loop stops after a few steps. When it stops, is a fixed point in value: the rows it fits best are a subset on which it is a least squares fit. Theorem 2 shows that the global minimizer is such a fixed point, but there can be other fixed points. The example on the page has one, which is why FAST-LTS uses many random starts and returns the best of them. FAST-LTS is therefore a heuristic: the value it returns is an upper bound on , and it is exact when one of its starts lies in the basin of the global minimum.
Part 3. Cost
A C-step computes residuals in time, selects the smallest in expected time (or by sorting, as in the code), and solves the normal equations in time. With starts and at most steps each, FAST-LTS takes time.
What is cited, not proved
The breakdown point for data in general position, which is for (Rousseeuw and Leroy 1987), and the rate with asymptotic normality (Víšek 2006) are cited on the page. They are not proved here.
Complexity
- One C-step costs for the residuals, expected for selecting the smallest (or by sorting), and for least squares. Usually , the number of steps until a fixed point, is small.
- FAST-LTS with starts takes time and extra space. Rousseeuw and Van Driessen use starts, two C-steps for each, and full iteration only for the 10 best.
- Exact LTS by enumeration of -subsets takes least squares fits, practical only for up to about 20. Exact methods with branch and bound reach a little further. No exact algorithm is known whose time is polynomial in both and .
- How many starts. A start uses only good rows with probability when a fraction is bad, so about starts give a clean one with probability ; see the RANSAC iteration bound. A clean start usually, but not always, leads to the global minimum, as the example shows.
Properties
- Breakdown point. For data in general position, , which is for , the highest possible for a regression equivariant estimator (Rousseeuw and Leroy 1987). This is cited, not proved here.
- Root-n rate. LTS is -consistent and asymptotically normal under regularity conditions (Víšek 2006). Its efficiency at the normal model is low for , about 7 percent (Rousseeuw and Leroy 1987), and grows with . This is cited, not proved here.
- Exact fit. If at least points lie exactly on one hyperplane, there, and that hyperplane is an LTS fit. The test file checks this with 6 of 10 points on a line.
- Equivariance. Regression, scale and affine equivariant, like least squares.
- Monotone search. Every C-step gives a fit at least as good as before. This makes FAST-LTS simple to check and to stop.
Variants and improvements
- Full FAST-LTS. Rousseeuw and Van Driessen (2006) add three refinements: two C-steps only from each start, then full iteration from the 10 best (selective iteration); for large , a run on disjoint subsamples of about 300 rows and then on their union (nested extensions); and an intercept adjustment for each candidate.
- Reweighted LTS. After LTS, estimate a robust scale from the residuals, give weight 0 to points with a standardized residual above about 2.5, and fit least squares to the rest. This raises the efficiency and keeps the breakdown point.
- The same idea for covariance. FAST-MCD uses the same C-step for the minimum covariance determinant: refit the mean and covariance on the points with the smallest Mahalanobis distances. The determinant never increases.
- Trimmed problems elsewhere. The trimmed mean is LTS for location, where the exact solution is a window of consecutive order statistics. Trimmed ICP uses the same trimming for point cloud alignment.
- MM-estimators. LTS is a common starting fit for an MM-estimator, which then reaches high efficiency.
When to use it
Use LTS when you need a regression that resists up to almost half bad rows, including bad leverage points, in more than one predictor. It is the standard high breakdown fit in robust statistics software, and it is the basis of robust outlier diagnostics: plot the standardized LTS residuals against robust distances of the rows. Prefer it to LMS, which converges more slowly. If the fraction of bad rows is small and leverage is not a concern, an M-estimator or iteratively reweighted least squares is cheaper.
Pitfalls
- Treating the FAST-LTS result as exact. It is the best fixed point found, an upper bound on the minimum. The example shows a clean start that misses the global minimum. Use more starts when the answer matters, and check whether different seeds agree.
- Coverage h too small or too large. With near the efficiency is low; with near the breakdown point is low. is a common compromise.
- Singular subsets. With categorical or discrete predictors, many -subsets are singular. Draw again, or add rows until the subset has full rank.
- Ties in residuals. When two rows have equal residuals at the -th place, the choice changes the next fit. Results can depend on the tie rule.
- Inference. Standard errors from least squares on the chosen subset ignore the selection; use the reweighted fit or the bootstrap.
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/least-trimmed-squares/. The authors are listed in prov/DQP8F6.jsonl.
import random
from fractions import Fraction
from itertools import combinations
def default_h(n, p):
"""h = floor((n + p + 1) / 2), the coverage with the best breakdown point."""
return (n + p + 1) // 2
def solve(A, b):
"""Solve A z = b exactly by Gaussian elimination; None if A is singular."""
m = len(A)
M = [list(map(Fraction, row)) + [Fraction(v)] for row, v in zip(A, b)]
for c in range(m):
piv = next((r for r in range(c, m) if M[r][c] != 0), None)
if piv is None:
return None
M[c], M[piv] = M[piv], M[c]
for r in range(m):
if r != c and M[r][c] != 0:
f = M[r][c] / M[c][c]
M[r] = [u - f * v for u, v in zip(M[r], M[c])]
return [M[i][m] / M[i][i] for i in range(m)]
def ols(X, y, idx):
"""Least squares coefficients on the rows idx, by the normal equations."""
p = len(X[0])
A = [[sum(X[i][a] * X[i][c] for i in idx) for c in range(p)] for a in range(p)]
rhs = [sum(X[i][a] * y[i] for i in idx) for a in range(p)]
return solve(A, rhs)
def residuals2(X, y, b):
return [(y[i] - sum(u * v for u, v in zip(X[i], b))) ** 2 for i in range(len(y))]
def objective(X, y, b, h):
"""Sum of the h smallest squared residuals."""
return sum(sorted(residuals2(X, y, b))[:h])
def c_step(X, y, b, h):
"""One concentration step: refit least squares on the h points with the
smallest squared residuals. The objective never increases."""
r = residuals2(X, y, b)
H = sorted(range(len(y)), key=lambda i: r[i])[:h] # @line L8
b_new = ols(X, y, H) # @line L9
return b if b_new is None else b_new
def fast_lts(X, y, h=None, starts=50, rng=None, max_steps=100):
"""FAST-LTS core: random p-subsets, then C-steps until the objective stops
falling; keep the best. Returns (coefficients, objective)."""
rng = rng or random.Random(0)
X = [[Fraction(v) for v in row] for row in X]
y = [Fraction(v) for v in y]
n, p = len(X), len(X[0])
h = h or default_h(n, p)
best = None
for _ in range(starts): # @line L1
b = ols(X, y, rng.sample(range(n), p)) # @line L2
if b is None:
continue
q = objective(X, y, b, h)
for _ in range(max_steps): # @line L3
b2 = c_step(X, y, b, h) # @line L4
q2 = objective(X, y, b2, h)
if q2 >= q: # @line L5
break
b, q = b2, q2
if best is None or q < best[1]: # @line L6
best = (b, q)
if best is None:
raise ValueError("every random subset was singular")
return best # @line L7
def lts_exact(X, y, h=None):
"""Exact LTS by trying every h-subset (small n only). Returns
(coefficients, objective)."""
X = [[Fraction(v) for v in row] for row in X]
y = [Fraction(v) for v in y]
n, p = len(X), len(X[0])
h = h or default_h(n, p)
best = None
for H in combinations(range(n), h):
b = ols(X, y, H)
if b is None:
continue
q = sum(residuals2([X[i] for i in H], [y[i] for i in H], b))
if best is None or q < best[1]:
best = (b, q)
return best
def design(x):
"""Rows [1, x_i] for simple regression."""
return [[1, v] for v in x]
test_least_trimmed_squares.py 120 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/least-trimmed-squares/. The authors are listed in prov/DQP8F6.jsonl.
import random
from fractions import Fraction
from itertools import combinations
from least_trimmed_squares import (
c_step, default_h, design, fast_lts, lts_exact, objective, ols,
)
PAGE_X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
PAGE_Y = [3, 5, 8, 9, 11, 14, 15, 2, 1, 0]
def F(rows):
return [[Fraction(v) for v in r] for r in rows]
def test_page_example() -> None:
X, y = F(design(PAGE_X)), [Fraction(v) for v in PAGE_Y]
h = default_h(10, 2)
assert h == 6
b, q = lts_exact(X, y)
assert b == [Fraction(142, 161), Fraction(333, 161)]
assert q == Fraction(114, 161)
# C-steps from the pair {1, 2} stop at a local minimum.
b = ols(X, y, [0, 1])
assert b == [1, 2] and objective(X, y, b, h) == 1
b = c_step(X, y, b, h)
assert b == [Fraction(89, 70), Fraction(69, 35)]
assert objective(X, y, b, h) == Fraction(57, 70)
assert c_step(X, y, b, h) == b
# From the bad pair {8, 9} they stop at a fit of the bad points.
b = ols(X, y, [7, 8])
assert b == [10, -1] and objective(X, y, b, h) == 19
b = c_step(X, y, b, h)
assert objective(X, y, b, h) == Fraction(1625, 87)
assert c_step(X, y, b, h) == b
# No start from a pair reaches the global minimum on these data.
for P in combinations(range(10), 2):
b = ols(X, y, list(P))
q = objective(X, y, b, h)
while True:
b2 = c_step(X, y, b, h)
q2 = objective(X, y, b2, h)
if q2 >= q:
break
b, q = b2, q2
assert q > Fraction(114, 161)
# So FAST-LTS returns the good local minimum, an upper bound.
assert fast_lts(design(PAGE_X), PAGE_Y, starts=50)[1] == Fraction(57, 70)
def test_c_step_never_increases() -> None:
rng = random.Random(41)
for _ in range(300):
n = rng.randint(3, 12)
X = F(design(rng.sample(range(-20, 20), n)))
y = [Fraction(rng.randint(-20, 20)) for _ in range(n)]
h = rng.randint(2, n)
b = [Fraction(rng.randint(-9, 9), rng.randint(1, 3)) for _ in range(2)]
assert objective(X, y, c_step(X, y, b, h), h) <= objective(X, y, b, h)
def test_exact_is_the_minimum() -> None:
rng = random.Random(42)
for _ in range(40):
n = rng.randint(4, 10)
X = F(design(rng.sample(range(-10, 10), n)))
y = [Fraction(rng.randint(-10, 10)) for _ in range(n)]
h = default_h(n, 2)
b, q = lts_exact(X, y)
assert objective(X, y, b, h) == q
# No line on a grid, and no fixed point of the C-steps, does better.
for _ in range(100):
g = [Fraction(rng.randint(-40, 40), 4), Fraction(rng.randint(-20, 20), 4)]
assert objective(X, y, g, h) >= q
# The optimum is a fixed point in value.
assert objective(X, y, c_step(X, y, b, h), h) == q
def test_fast_lts_against_enumeration() -> None:
rng = random.Random(43)
hits = 0
trials = 30
for t in range(trials):
n = rng.randint(6, 12)
x = rng.sample(range(0, 30), n)
y = [2 * v + 1 + rng.randint(-2, 2) for v in x]
for i in rng.sample(range(n), (n - 1) // 3):
y[i] = rng.randint(-60, 60)
exact = lts_exact(design(x), y)[1]
got = fast_lts(design(x), y, starts=20, rng=random.Random(t))[1]
assert got >= exact
hits += got == exact
assert hits >= trials // 2
def test_quadratic_model() -> None:
rng = random.Random(44)
for _ in range(10):
n = rng.randint(6, 9)
x = rng.sample(range(-6, 7), n)
X = [[1, v, v * v] for v in x]
y = [v * v - 2 * v + 3 for v in x]
y[0] += 50
b, q = lts_exact(X, y)
assert q == 0 and b == [3, -2, 1]
b2, q2 = fast_lts(X, y, starts=40, rng=random.Random(n))
assert q2 == 0 and b2 == [3, -2, 1]
def test_exact_fit_with_majority_on_a_line() -> None:
x = list(range(10))
y = [3 - 2 * v for v in x]
for i, v in zip((1, 4, 6, 9), (100, -50, 77, 0)):
y[i] = v
b, q = fast_lts(design(x), y, starts=30)
assert q == 0 and b == [3, -2]
History
Rousseeuw proposed least trimmed squares in his 1984 paper on least median of squares, and the book of Rousseeuw and Leroy (1987) studied both. LMS was used more at first, because it was easier to approximate. That changed when Rousseeuw and Van Driessen found the C-step theorem: first for the minimum covariance determinant (FAST-MCD, 1999), then for regression in FAST-LTS, published in 2006. Víšek proved the asymptotic normality of LTS in a series of papers that ended in 2006.
References
- Peter J. Rousseeuw, Least median of squares regression, Journal of the American Statistical Association 79(388), 1984, pp. 871 to 880 (least trimmed squares is proposed there next to LMS)
- Peter J. Rousseeuw and Annick M. Leroy, Robust Regression and Outlier Detection, Wiley, 1987, Chapter 3 (the breakdown point of LTS for the coverage h, and its asymptotic efficiency) and Chapter 4 (properties of LTS)
- Peter J. Rousseeuw and Katrien Van Driessen, Computing LTS regression for large data sets, Data Mining and Knowledge Discovery 12(1), 2006, pp. 29 to 45 (the C-step theorem, the FAST-LTS algorithm with random p-subsets, selective iteration and nested extensions)
- Jan Ámos Víšek, The least trimmed squares. Part III: Asymptotic normality, Kybernetika 42(2), 2006, pp. 203 to 224, The whole paper (root-n consistency and asymptotic normality of LTS)
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 DQP8F6 Edit this page History Report an error Markdown