Algorithmadvanced
MM-estimator
Also called Yohai MM-estimator.
Yohai's regression estimator that starts from a high-breakdown S-estimate of scale and then solves an efficient M-estimation step. It has both high breakdown and high efficiency.
- Time
- O(m (p³ + n p) + t n p²) worst · averageO(m (p³ + n p) + n p²) best
- Space
- O(n + p²) extra
- Where
- m = the number of random subsets; n = the number of observations; p = the number of coefficients; t = the number of reweighting steps
- Model
- Real RAM
- Properties
- randomizednot exact in exact arithmeticnot in placeofflinerandomized
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Víctor J. Yohai, 1987
The MM-estimator of linear regression runs in two stages. First, an S-estimate gives a fit and a robust scale of its residuals, with breakdown point one half. Second, with fixed, a Tukey biweight M-estimate is computed by reweighted least squares, starting from . The second stage raises the efficiency at the normal to a chosen level, usually 95%. Yohai (1987) proved that it keeps the breakdown point of the first stage. The S-estimate is a hard nonconvex problem, so in practice it is approximated by fits through random subsets of points.
In plain words
Fitting a line by least squares fails when a few points are far off, in particular points with unusual values. The MM method first finds a rough line that most points agree with. It tries many lines, each through a few randomly chosen points, and keeps the line whose residuals are the smallest in a robust sense. This also gives a robust size of a typical residual. Then it improves the line: points with small residuals count fully, points with larger residuals count less, and points with very large residuals do not count. The improved line is nearly as precise as least squares when the data are clean.
The problem
The input is a design matrix with rows (with a column of ones for an intercept) and responses , with . Let for and beyond: the biweight loss scaled to a maximum of 1. Write .
- The M-scale of residuals is the solution of . With and , it is consistent for at the normal and has breakdown point one half.
- The S-estimate is , and .
- The MM-estimate is a local minimizer of with , reached from , with fixed.
The algorithm
MM(X, y, m) ▷ c0 = 1.54764, b = 1/2, c1 = 4.685- 1
for j ← 1 to m do - 2
J ← a random set of p rows; if X_J is singular then skip; β ← the solution of X_J β = y_J - 3
s ← M-SCALE(y − Xβ) - 4
if s is the least scale so far then (β0, s0) ← (β, s) - 5
β ← β0; ŝ ← s0; if ŝ = 0 then return (β, ŝ) - 6
repeat - 7
r_i ← y_i − x_iᵀβ for each i - 8
w_i ← (1 − (r_i/(c1 ŝ))²)² if |r_i| < c1 ŝ, else 0 - 9
β' ← the weighted least squares fit with weights w_i - 10
if ‖β' − β‖ ≤ tol then return (β', ŝ) - 11
β ← β' M-SCALE(r)- 12
if at most n b residuals are nonzero then return 0 - 13
lo ← min_{r_i ≠ 0} |r_i| / c0; hi ← a value with h(hi) ≤ b, where h(s) = (1/n) Σ ρ_{c0}(r_i/s) - 14
bisect: if h(mid) > b then lo ← mid else hi ← mid; return hi when the bracket is as small as the precision
is continuous and nonincreasing in . At every nonzero term is 1, so . Since , any has .
Example
Take , an intercept, and . The first eight points lie near . The last two are bad leverage points.
- Least squares gives . The two bad points turn the slope almost flat.
- S-stage, with all 45 pairs as the subsets: the least M-scale, , comes from the line through and , that is . Its residuals are . The terms are , and their mean is , as the scale equation requires.
- M-stage, with :
| step | ||
|---|---|---|
| 0 | (1.2333, 1.9667) | 2.67162 |
| 1 | (1.0758, 1.9881) | 2.52365 |
| 2 | (1.0604, 1.9901) | 2.52205 |
| 3 | (1.0589, 1.9902) | 2.52204 |
The iteration stops at after 10 steps. The two bad points have weight 0, and the eight good points have weights between 0.89 and 1. The objective never rises. Each bad point adds exactly 1 to it.
Correctness
The statistical properties are cited, not proved here. The page proves that the computation does what the pseudocode claims; proofs/correctness.md has the details.
- M-SCALE. is continuous and nonincreasing. If more than residuals are nonzero, is strictly decreasing near any root, so the root is unique, and the bisection keeps it in .
- S-stage. Lines L1 to L4 return the subset fit with the least M-scale among those tried. This is the S-estimate only if the best fit is among the subset fits. In general it is an approximation.
- M-stage. with concave, as on the Tukey biweight page. So the objective lies below a weighted sum of squares that touches it at the current , and line L9 minimizes that sum. Hence the objective does not increase. Since , , so the objective at the start is at most . Each point with zero weight adds 1, so at most points have zero weight at every step. If every rows of are linearly independent and , the weighted least squares problem in line L9 has a unique solution.
Proof: The MM computation finds the M-scale, keeps the least scale, and never increases the M-objectivefull
This proof covers the computation only. The breakdown point and the efficiency of the MM-estimator are theorems of Yohai (1987) and are not proved here.
Notation. for and beyond, with and . For residuals put for , and let be the number of nonzero .
Lemma 1 (the M-scale). If , the equation has exactly one solution , and the bisection of lines L13 and L14 keeps in .
Proof. is continuous, even, and nondecreasing in , and strictly increasing in on . So is continuous and nonincreasing in . At , every nonzero term is with , which is 1, so . Since for , for all , so , and for the of line L13. By continuity a root lies in . Let be a root. Since , some nonzero term has , that is . This term is strictly decreasing in near and the others are nonincreasing, so is strictly decreasing on a neighbourhood of . A nonincreasing function that is strictly decreasing near each of its roots has at most one root: two roots would make constant on . So the root is unique. Each bisection step keeps , so .
If , line L12 returns 0. This is the limit of the scale equation, because the residuals that are 0 already make up at least half of the sample.
Lemma 2 (the S-stage). Lines L1 to L4 return a pair such that is the least M-scale over the nonsingular subsets drawn, and is the fit of one subset that attains it.
Proof. Line L4 keeps a running minimum of the values of line L3.
Lemma 3 (majorization). Put for and beyond. Then , is concave, nondecreasing and differentiable, and with for and 0 beyond.
Proof. for and 0 for , and both one-sided derivatives at are 0. This derivative is nonnegative and nonincreasing.
Lemma 4 (descent). Fix and put . Let be the weights of line L8 at , and suppose the matrix is nonsingular. Then the fit of line L9 satisfies , with equality only if .
Proof. Put . By Lemma 3 and the tangent inequality for concave functions,
with equality at . is a quadratic in whose quadratic part is , which is positive definite because is nonsingular and . So has a unique minimizer, the weighted least squares fit . Then , and the second inequality is strict when .
Lemma 5 (the weighted design stays nonsingular). Assume , every rows of are linearly independent, and . Then at every step of lines L6 to L11, at least points have a positive weight, and is nonsingular.
Proof. For fixed , is nonincreasing in , because decreases. Since , at the start
where the last equality is the scale equation (Lemma 1). A point with weight 0 has and adds exactly 1 to . So the number of points with weight 0 is at most . Assume that was nonsingular at all earlier steps. By Lemma 4, at the current is at most , so at most points have weight 0, and at least points have a positive weight. Any of them have independent rows , so for some of them has , and . So is nonsingular. Induction on the steps completes the proof.
Theorem. Under the assumptions of Lemma 5, the M-stage is well defined, is nonincreasing, and every limit point of the iterates satisfies , where .
Proof. Lemmas 4 and 5 give the first two claims. Let . is closed, because is continuous. For , as in Lemma 5, there is a set of rows with for . Then with , so lies in a bounded set that depends only on . There are finitely many sets , so is bounded, and the iterates, which lie in , have limit points in . The weights are continuous functions of , and is nonsingular at every point of by the count of Lemma 5, so the map is continuous on . As in the proof on the Tukey biweight page, converges to some , and a limit point with its image both have , so Lemma 4 gives . A fixed point of the weighted least squares map satisfies , which is the stated equation.
Complexity
- S-stage. Each subset costs for the solve and for the residuals. M-SCALE costs for each bisection step, and a fixed number of steps reaches the floating-point precision. So the stage costs .
- Number of subsets. If a fraction of the points are outliers, a random -subset is clean with probability . So subsets give a clean one with probability . For , and this is ; for and it is (the bound is ). The count grows exponentially in .
- M-stage. Each step costs to form the weighted normal equations and to solve them. The convergence is linear.
- Space. .
Properties
Proved on this page: the descent and the well-defined steps of the computation. Cited: the statistical properties below. Measured: the numbers in the tests.
- Breakdown point one half (cited). If the S-estimate uses and , the MM-estimate has asymptotic breakdown point (Yohai 1987). It holds for outliers in and for bad leverage points in . The S-estimate alone has this breakdown point (Rousseeuw and Yohai 1984).
- Efficiency (cited). Under normal errors the MM-estimate has the same asymptotic distribution as the M-estimate with the true , so gives 95% efficiency relative to least squares (Yohai 1987). The S-estimate with alone has an efficiency of only about 29% (Maronna et al. 2019).
- Measured. In the tests, on 60 points with 30% bad leverage points the least squares slope is off by more than 1, and the MM slope is within 0.15 of the truth. On 40 clean replications of 40 points the variance ratio of the slopes, least squares over MM, is 0.94.
- Equivariance. The MM-estimate is regression, scale and affine equivariant, when the subsets are chosen independently of the values.
- Exact fit. If more than half of the points lie exactly on a hyperplane, the M-scale of that fit is 0, and the algorithm returns that fit when a subset from those points is drawn.
Variants and improvements
- Fast-S. Improve each subset fit by a few local steps for the S-objective, keep the best few, and refine them fully (Salibián-Barrera and Yohai 2006). This needs far fewer subsets than the plain search.
- Deterministic starts. Use the least trimmed squares or the least median of squares fit as the start instead of random subsets.
- Other losses. Optimal functions, or a Huber loss in the M-stage. A monotone gives a unique M-stage solution, but leverage points then have more influence.
- Inference. Use the sandwich covariance of the M-stage with fixed. Robust tests and robust model selection build on it.
- Robust multivariate methods. For location and scatter, the analogous two-stage idea starts from the minimum covariance determinant.
When to use it
Use MM-regression as the default robust linear regression when outliers in or bad leverage points in are possible and you want an efficiency close to least squares on clean data. Use plain least squares with Cook's distance and leverage diagnostics when the data are clean or the outliers are few and easy to see. Use RANSAC in geometric fitting with a known inlier threshold. Use the Huber M-estimator in regression only when has no outliers.
Pitfalls
- The constant depends on how is scaled. With scaled to a maximum of 1, . With the biweight of maximum , the same estimator needs . Mixing the two conventions gives a wrong scale.
- Too few subsets. With a large and many outliers, all random subsets can contain an outlier, and the S-stage fails. Use Fast-S or a deterministic start.
- Updating the scale. The scale must stay fixed in the M-stage. Recomputing it from the M-stage residuals destroys the breakdown guarantee.
- Efficiency is asymptotic. With few points per coefficient, the efficiency on clean data can be well below 95%.
- A local solution. The M-stage finds a stationary point near the start. A different start can give a different answer.
- Singular weighted designs. With factor variables or many zero weights, the weighted design can be singular. Check the rank.
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/mm-estimator/. The authors are listed in prov/MBJNZK.jsonl.
import itertools
import math
import random
# Biweight constants with rho normalized to a maximum of 1.
C0 = 1.54764 # S-step: E rho_C0(Z) = 1/2 at N(0, 1), breakdown point 1/2
B = 0.5
C1 = 4.685 # M-step: 95% efficiency at the normal
def rho(u, c):
"""Biweight rho normalized to max 1: 1 - (1 - (u/c)^2)^3 inside, 1 outside."""
v = (u / c) ** 2
if v >= 1.0:
return 1.0
return 1.0 - (1.0 - v) ** 3
def weight(u, c):
"""psi(u)/u up to a constant factor: (1 - (u/c)^2)^2 inside, 0 outside."""
v = (u / c) ** 2
return 0.0 if v >= 1.0 else (1.0 - v) ** 2
def psi(u, c):
return u * weight(u, c)
def solve(a, b):
"""Solve the square system a z = b by Gaussian elimination with partial pivoting.
Returns None if the matrix is singular (to rounding).
"""
p = len(a)
m = [list(map(float, row)) + [float(v)] for row, v in zip(a, b)]
scale = max((abs(v) for row in a for v in row), default=0.0) or 1.0
for col in range(p):
piv = max(range(col, p), key=lambda r: abs(m[r][col]))
if abs(m[piv][col]) <= 1e-12 * scale:
return None
m[col], m[piv] = m[piv], m[col]
for r in range(col + 1, p):
f = m[r][col] / m[col][col]
if f:
for k in range(col, p + 1):
m[r][k] -= f * m[col][k]
z = [0.0] * p
for r in range(p - 1, -1, -1):
z[r] = (m[r][p] - sum(m[r][k] * z[k] for k in range(r + 1, p))) / m[r][r]
return z
def residuals(x, y, beta):
return [yi - sum(a * b for a, b in zip(row, beta)) for row, yi in zip(x, y)]
def m_scale(r, c=C0, b=B, iters=200):
"""The M-scale s > 0 with mean(rho_c(r_i / s)) = b, by bisection.
Returns 0 if at most n b residuals are nonzero (an exact fit of that many points).
"""
n = len(r)
nz = [abs(v) for v in r if v != 0]
if len(nz) <= n * b: # @line L12
return 0.0
lo = min(nz) / c # h(lo) = len(nz)/n > b
hi = math.sqrt(3.0 * sum(v * v for v in r) / (n * b)) / c + max(nz) # h(hi) <= b
for _ in range(iters): # @line L13
mid = (lo + hi) / 2
if mid in (lo, hi):
break
if sum(rho(v / mid, c) for v in r) / n > b: # @line L14
lo = mid
else:
hi = mid
return hi
def weighted_ls(x, y, w):
p = len(x[0])
a = [[sum(wi * row[i] * row[j] for wi, row in zip(w, x)) for j in range(p)] for i in range(p)]
rhs = [sum(wi * row[i] * yi for wi, row, yi in zip(w, x, y)) for i in range(p)]
return solve(a, rhs)
def s_estimate(x, y, subsets=500, seed=0):
"""Approximate S-estimate: the elemental fit with the least M-scale over random p-subsets.
If subsets is None, all p-subsets are tried.
"""
n, p = len(x), len(x[0])
rng = random.Random(seed)
if subsets is None:
pool = itertools.combinations(range(n), p)
else:
pool = (rng.sample(range(n), p) for _ in range(subsets))
best = None
for idx in pool: # @line L1
beta = solve([x[i] for i in idx], [y[i] for i in idx]) # @line L2
if beta is None:
continue
s = m_scale(residuals(x, y, beta)) # @line L3
if best is None or s < best[1]: # @line L4
best = (beta, s)
if best is None:
raise ValueError("every subset was singular")
return best
def m_objective(x, y, beta, s, c=C1):
return sum(rho(v / s, c) for v in residuals(x, y, beta))
def mm_estimate(x, y, subsets=500, seed=0, tol=1e-10, max_iter=500, trace=None):
"""MM-estimate of regression: S-start and M-scale, then biweight IRLS with c = 4.685.
x is a list of n rows of length p (put a column of ones in it for an intercept).
Returns (beta, s). If trace is a list, the M-step iterates are appended to it.
"""
beta, s = s_estimate(x, y, subsets, seed) # @line L5
if s == 0:
return beta, s # @line L5
if trace is not None:
trace.append(beta)
for _ in range(max_iter): # @line L6
r = residuals(x, y, beta) # @line L7
w = [weight(v / s, C1) for v in r] # @line L8
new = weighted_ls(x, y, w) # @line L9
if new is None:
raise ValueError("the weighted design is singular")
if trace is not None:
trace.append(new)
done = max(abs(a - b) for a, b in zip(new, beta)) <= tol * (1.0 + max(abs(v) for v in beta))
beta = new # @line L11
if done: # @line L10
break
return beta, s
def ols(x, y):
return weighted_ls(x, y, [1.0] * len(y))
test_mm_estimator.py 113 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/mm-estimator/. The authors are listed in prov/MBJNZK.jsonl.
import math
import random
import pytest
from mm_estimator import (
B, C0, C1, m_objective, m_scale, mm_estimate, ols, psi, residuals, rho, s_estimate,
)
XS = list(range(1, 11))
YS = [3.1, 4.8, 7.2, 9.1, 10.8, 13.2, 15.0, 16.9, 2.0, 1.0]
X = [[1.0, float(v)] for v in XS]
def test_m_scale_by_hand() -> None:
# Four residuals of size 1: rho(1/s) = 1/2, so s = 1 / (c0 sqrt(1 - 2^(-1/3))).
s = m_scale([1, -1, 1, -1])
assert s == pytest.approx(1 / (C0 * math.sqrt(1 - 2 ** (-1 / 3))), rel=1e-12)
# At most n b nonzero residuals: an exact fit of half the points gives scale 0.
assert m_scale([0, 0, 5, -7]) == 0.0
def test_m_scale_is_consistent_at_the_normal() -> None:
rng = random.Random(1)
r = [rng.gauss(0.0, 2.0) for _ in range(20000)]
assert m_scale(r) == pytest.approx(2.0, rel=0.03)
def test_page_example() -> None:
assert ols(X, YS) == pytest.approx([7.306667, 0.182424], abs=1e-6)
beta_s, s = s_estimate(X, YS, subsets=None)
assert beta_s == pytest.approx([37 / 30, 59 / 30], abs=1e-9) # the line through (4, 9.1) and (7, 15.0)
assert sum(rho(v / s, C0) for v in residuals(X, YS, beta_s)) / len(YS) == pytest.approx(B, abs=1e-12)
beta, s2 = mm_estimate(X, YS, subsets=None)
assert s2 == s
assert beta == pytest.approx([1.058707, 1.990247], abs=1e-6)
def test_m_step_descent_and_stationarity() -> None:
rng = random.Random(2)
for _ in range(15):
n, p = rng.randint(20, 40), rng.choice([2, 3])
x = [[1.0] + [rng.gauss(0.0, 1.0) for _ in range(p - 1)] for _ in range(n)]
y = [1.0 + sum(row[1:]) + rng.gauss(0.0, 0.5) for row in x]
for i in rng.sample(range(n), n // 5):
y[i] += rng.uniform(10.0, 30.0)
trace = []
beta, s = mm_estimate(x, y, subsets=60, seed=rng.randint(0, 10**6), trace=trace)
q = [m_objective(x, y, b, s) for b in trace]
assert all(b <= a + 1e-12 for a, b in zip(q, q[1:]))
assert q[-1] <= q[0]
r = residuals(x, y, beta)
for j in range(p):
assert abs(sum(psi(v / s, C1) * row[j] for v, row in zip(r, x))) <= 1e-6 * n
# The scale solves the M-scale equation at the S residuals.
b0 = trace[0]
assert sum(rho(v / s, C0) for v in residuals(x, y, b0)) / n == pytest.approx(B, abs=1e-9)
def test_s_step_takes_the_least_scale() -> None:
rng = random.Random(3)
n = 9
x = [[1.0, float(i)] for i in range(n)]
y = [2.0 * i + rng.gauss(0.0, 1.0) for i in range(n)]
beta, s = s_estimate(x, y, subsets=None)
from mm_estimator import solve
for i in range(n):
for j in range(i + 1, n):
b = solve([x[i], x[j]], [y[i], y[j]])
assert m_scale(residuals(x, y, b)) >= s
def test_exact_fit_of_a_majority() -> None:
x = [[1.0, float(i)] for i in range(10)]
y = [3.0 - 0.5 * i for i in range(10)]
y[1], y[4], y[7] = 50.0, -40.0, 99.0
beta, s = mm_estimate(x, y, subsets=None)
assert s == 0.0
assert beta == pytest.approx([3.0, -0.5], abs=1e-12)
def test_contaminated_regression_error_is_measured() -> None:
# 30% bad leverage points: OLS slope is far off, MM stays near the true slope 2.
rng = random.Random(4)
n = 60
xs = [rng.uniform(0.0, 10.0) for _ in range(n)]
ys = [1.0 + 2.0 * v + rng.gauss(0.0, 1.0) for v in xs]
for i in range(int(0.3 * n)):
xs[i], ys[i] = rng.uniform(25.0, 30.0), rng.uniform(-5.0, 0.0)
x = [[1.0, v] for v in xs]
b_ols = ols(x, ys)
b_mm, _ = mm_estimate(x, ys, subsets=200, seed=5)
assert abs(b_ols[1] - 2.0) > 1.0
assert abs(b_mm[1] - 2.0) < 0.15
def test_efficiency_on_clean_data_is_measured() -> None:
# On clean normal errors the MM and OLS slopes are close; the variance ratio over
# replications is measured, and must be near the 95% of the theory.
rng = random.Random(6)
d_ols, d_mm = [], []
for _ in range(40):
xs = [rng.uniform(0.0, 1.0) for _ in range(40)]
x = [[1.0, v] for v in xs]
y = [rng.gauss(0.0, 1.0) for _ in xs]
d_ols.append(ols(x, y)[1])
d_mm.append(mm_estimate(x, y, subsets=30, seed=rng.randint(0, 10**6))[0][1])
v_ols = sum(v * v for v in d_ols) / len(d_ols)
v_mm = sum(v * v for v in d_mm) / len(d_mm)
assert 0.75 < v_ols / v_mm <= 1.1
History
Rousseeuw (1984) introduced the least median of squares, the first affine equivariant regression estimator with breakdown point one half. Rousseeuw and Yohai (1984) defined S-estimators, which have high breakdown and are asymptotically normal, but have low efficiency. Yohai (1987) combined an S-estimate of scale with an efficient M-step and proved that the result keeps both properties. Salibián-Barrera and Yohai (2006) gave the Fast-S algorithm that most software uses for the first stage.
References
- Víctor J. Yohai, High breakdown-point and high efficiency robust estimates for regression, The Annals of Statistics 15(2), 1987, pp. 642 to 656 (the MM-estimator, its breakdown point and its efficiency at the normal)
- Peter J. Rousseeuw and Víctor J. Yohai, Robust regression by means of S-estimators, in Robust and Nonlinear Time Series Analysis, Lecture Notes in Statistics 26, Springer, 1984, pp. 256 to 272 (S-estimators of regression, the M-scale and their breakdown point)
- Matías Salibián-Barrera and Víctor J. Yohai, A fast algorithm for S-regression estimates, Journal of Computational and Graphical Statistics 15(2), 2006, pp. 414 to 427 (random subsets with local improvement steps for the S-estimate)
- 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 5 (S-estimators, MM-estimators, the choice of the constants and the computation by subsampling)
- Peter J. Rousseeuw and Annick M. Leroy, Robust Regression and Outlier Detection, Wiley, 1987, Chapter 3 (high-breakdown regression and the subsampling algorithm)
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 MBJNZK Edit this page History Report an error Markdown