# Robust regression with Huber and Tukey bisquare: outliers lose their weight step by step

Six years of Belgian phone statistics were logged in minutes instead of calls, and least squares bends to chase them. Run iteratively reweighted least squares and watch Huber and Tukey bisquare weights shrink the outliers until the line follows the real trend.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Robust regression: Huber and Tukey bisquare fitted by reweighting&#34;,&#34;sub&#34;:&#34;Belgian international phone calls, 1950 to 1973. Six years were logged in minutes instead of calls. Run IRLS and watch the outliers lose their weight while least squares stays bent.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;kind&#34;,&#34;label&#34;:&#34;Loss (0 = Huber, 1 = Tukey bisquare)&#34;,&#34;min&#34;:0,&#34;max&#34;:1,&#34;step&#34;:1,&#34;value&#34;:0,&#34;note&#34;:&#34;Huber caps an outlier&#39;s pull; bisquare drops it to zero&#34;},{&#34;id&#34;:&#34;tune&#34;,&#34;label&#34;:&#34;Tuning multiplier&#34;,&#34;min&#34;:0.5,&#34;max&#34;:3,&#34;step&#34;:0.05,&#34;value&#34;:1,&#34;note&#34;:&#34;1 uses the standard constants: 1.345 (Huber) and 4.685 (bisquare)&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;year&#34;,&#34;calls&#34;],&#34;rows&#34;:[[50,4.4],[51,4.7],[52,4.7],[53,5.9],[54,6.6],[55,7.3],[56,8.1],[57,8.8],[58,10.6],[59,12],[60,13.5],[61,14.9],[62,16.1],[63,21.2],[64,119],[65,124],[66,142],[67,159],[68,182],[69,212],[70,43],[71,24],[72,27],[73,29]],&#34;note&#34;:&#34;Belgian international phone calls in tens of millions, years 1950 to 1973 (19xx), from Rousseeuw and Leroy (1987), shipped as MASS::phones in R. 1964 to 1969 were recorded as total minutes, not calls. Edit cells or paste your own x, y columns.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;kc&#34;,&#34;label&#34;:&#34;Tuning constant c&#34;,&#34;expr&#34;:&#34;tune*ifelse(kind, 4.685, 1.345)&#34;,&#34;tex&#34;:&#34;c = m \\times \\begin{cases}1.345 \u0026 \\text{Huber}\\\\ 4.685 \u0026 \\text{bisquare}\\end{cases}&#34;},{&#34;id&#34;:&#34;sx0&#34;,&#34;label&#34;:&#34;Sum of x&#34;,&#34;expr&#34;:&#34;sum(year)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sy0&#34;,&#34;label&#34;:&#34;Sum of y&#34;,&#34;expr&#34;:&#34;sum(calls)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sxy0&#34;,&#34;label&#34;:&#34;Sum of xy&#34;,&#34;expr&#34;:&#34;dot(year, calls)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;sxx0&#34;,&#34;label&#34;:&#34;Sum of x squared&#34;,&#34;expr&#34;:&#34;dot(year, year)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;ols_b&#34;,&#34;label&#34;:&#34;Least-squares slope&#34;,&#34;expr&#34;:&#34;(sxy0 - sx0*sy0/n)/(sxx0 - sx0*sx0/n)&#34;,&#34;tex&#34;:&#34;\\hat\\beta_{OLS} = \\frac{\\sum x_iy_i - \\frac{1}{n}\\sum x_i\\sum y_i}{\\sum x_i^2 - \\frac{1}{n}(\\sum x_i)^2}&#34;},{&#34;id&#34;:&#34;ols_a&#34;,&#34;label&#34;:&#34;Least-squares intercept&#34;,&#34;expr&#34;:&#34;(sy0 - ols_b*sx0)/n&#34;}],&#34;row&#34;:[{&#34;id&#34;:&#34;r&#34;,&#34;label&#34;:&#34;residual&#34;,&#34;expr&#34;:&#34;calls - a - b*year&#34;,&#34;tex&#34;:&#34;r_i = y_i - a - b\\,x_i&#34;},{&#34;id&#34;:&#34;ar&#34;,&#34;label&#34;:&#34;|residual|&#34;,&#34;expr&#34;:&#34;abs(r)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;ols_r&#34;,&#34;label&#34;:&#34;OLS residual&#34;,&#34;expr&#34;:&#34;calls - ols_a - ols_b*year&#34;}],&#34;derived&#34;:[{&#34;id&#34;:&#34;s&#34;,&#34;label&#34;:&#34;Robust scale s (MAD)&#34;,&#34;expr&#34;:&#34;med(ar)/0.6745&#34;,&#34;tex&#34;:&#34;s = \\frac{\\operatorname{median}_i |r_i|}{0.6745}&#34;,&#34;note&#34;:&#34;0.6745 makes s match the standard deviation for normal errors&#34;}],&#34;row2&#34;:[{&#34;id&#34;:&#34;u&#34;,&#34;label&#34;:&#34;scaled residual&#34;,&#34;expr&#34;:&#34;ar/(kc*s)&#34;,&#34;tex&#34;:&#34;u_i = \\frac{|r_i|}{c\\,s}&#34;},{&#34;id&#34;:&#34;wt&#34;,&#34;label&#34;:&#34;weight&#34;,&#34;expr&#34;:&#34;ifelse(kind, ifelse(gt(1, u), (1 - u^2)^2, 0), min(1, 1/max(u, 1e-9)))&#34;,&#34;tex&#34;:&#34;w_i^{H} = \\min\\!\\big(1, \\tfrac{1}{u_i}\\big),\\quad w_i^{B} = \\big(1-u_i^2\\big)^2\\,[u_i\u003c1]&#34;},{&#34;id&#34;:&#34;wx&#34;,&#34;label&#34;:&#34;w x&#34;,&#34;expr&#34;:&#34;wt*year&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;wy&#34;,&#34;label&#34;:&#34;w y&#34;,&#34;expr&#34;:&#34;wt*calls&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;wxy&#34;,&#34;label&#34;:&#34;w x y&#34;,&#34;expr&#34;:&#34;wt*year*calls&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;wxx&#34;,&#34;label&#34;:&#34;w x x&#34;,&#34;expr&#34;:&#34;wt*year*year&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;fit&#34;,&#34;label&#34;:&#34;robust fit&#34;,&#34;expr&#34;:&#34;a + b*year&#34;}],&#34;post&#34;:[{&#34;id&#34;:&#34;sw&#34;,&#34;label&#34;:&#34;Sum of weights&#34;,&#34;expr&#34;:&#34;sum(wt)&#34;},{&#34;id&#34;:&#34;b_new&#34;,&#34;label&#34;:&#34;Next slope&#34;,&#34;expr&#34;:&#34;(sum(wxy) - sum(wx)*sum(wy)/sw)/(sum(wxx) - sum(wx)*sum(wx)/sw)&#34;,&#34;tex&#34;:&#34;b&#39; = \\frac{\\sum w_ix_iy_i - \\frac{\\sum w_ix_i\\,\\sum w_iy_i}{\\sum w_i}}{\\sum w_ix_i^2 - \\frac{(\\sum w_ix_i)^2}{\\sum w_i}}&#34;},{&#34;id&#34;:&#34;a_new&#34;,&#34;label&#34;:&#34;Next intercept&#34;,&#34;expr&#34;:&#34;(sum(wy) - b_new*sum(wx))/sw&#34;,&#34;tex&#34;:&#34;a&#39; = \\frac{\\sum w_iy_i - b&#39;\\sum w_ix_i}{\\sum w_i}&#34;},{&#34;id&#34;:&#34;pred74&#34;,&#34;label&#34;:&#34;Robust forecast for 1974&#34;,&#34;expr&#34;:&#34;a + b*74&#34;},{&#34;id&#34;:&#34;ols74&#34;,&#34;label&#34;:&#34;Least-squares forecast for 1974&#34;,&#34;expr&#34;:&#34;ols_a + ols_b*74&#34;}],&#34;iterate&#34;:{&#34;state&#34;:[{&#34;id&#34;:&#34;a&#34;,&#34;label&#34;:&#34;Intercept a&#34;,&#34;init&#34;:&#34;0&#34;},{&#34;id&#34;:&#34;b&#34;,&#34;label&#34;:&#34;Slope b&#34;,&#34;init&#34;:&#34;0&#34;}],&#34;update&#34;:[{&#34;id&#34;:&#34;a&#34;,&#34;expr&#34;:&#34;a_new&#34;},{&#34;id&#34;:&#34;b&#34;,&#34;expr&#34;:&#34;b_new&#34;}],&#34;max&#34;:40,&#34;per&#34;:1,&#34;track&#34;:&#34;b_new&#34;},&#34;chart&#34;:{&#34;title&#34;:&#34;Calls per year: least squares against the robust fit&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Year (19xx)&#34;,&#34;from&#34;:49,&#34;to&#34;:74},&#34;y&#34;:{&#34;from&#34;:-20,&#34;to&#34;:220},&#34;series&#34;:[{&#34;label&#34;:&#34;Robust fit (current IRLS step)&#34;,&#34;expr&#34;:&#34;a + b*t&#34;},{&#34;label&#34;:&#34;Least squares&#34;,&#34;expr&#34;:&#34;ols_a + ols_b*t&#34;,&#34;dash&#34;:true}],&#34;points&#34;:{&#34;x&#34;:&#34;year&#34;,&#34;y&#34;:&#34;calls&#34;,&#34;label&#34;:&#34;Recorded calls (tens of millions)&#34;}},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Weight each year gets in the robust fit&#34;,&#34;y&#34;:&#34;wt&#34;,&#34;label&#34;:&#34;year&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Start from a line&#34;,&#34;text&#34;:&#34;IRLS starts from a = 0, b = 0 here; least squares on the same data gives slope {ols_b} and intercept {ols_a}, bent upwards by the six minute-logged years.&#34;,&#34;show&#34;:[&#34;ols_b&#34;,&#34;ols_a&#34;]},{&#34;title&#34;:&#34;Measure residuals&#34;,&#34;text&#34;:&#34;Each year gets a residual r = y − a − b·x from the current line, now a = {a} and b = {b}.&#34;},{&#34;title&#34;:&#34;Estimate a robust scale&#34;,&#34;text&#34;:&#34;The scale is the median absolute residual divided by 0.6745: s = {s}. A median ignores how wild the outliers are, unlike a standard deviation.&#34;,&#34;show&#34;:[&#34;s&#34;]},{&#34;title&#34;:&#34;Turn residuals into weights&#34;,&#34;text&#34;:&#34;With c = {kc}, a year whose residual exceeds c·s gets a weight below 1. The weights add up to {sw}; a clean fit would give one per row.&#34;,&#34;show&#34;:[&#34;kc&#34;,&#34;sw&#34;]},{&#34;title&#34;:&#34;Solve weighted least squares&#34;,&#34;text&#34;:&#34;The weighted normal equations give the next line: slope {b_new}, intercept {a_new}. Repeat until the line stops moving.&#34;,&#34;show&#34;:[&#34;b_new&#34;,&#34;a_new&#34;]},{&#34;title&#34;:&#34;Compare the forecasts&#34;,&#34;text&#34;:&#34;For 1974 the robust line predicts {pred74} against {ols74} from least squares.&#34;,&#34;show&#34;:[&#34;pred74&#34;,&#34;ols74&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

The points are a classic dataset from Rousseeuw and Leroy's book on robust regression: the number of international phone calls made from Belgium each year from 1950 to 1973, in tens of millions. The series grows steadily, except that from 1964 to 1969 the statistics office recorded the total number of minutes instead of the number of calls, so those six years jump to 119 to 212. 1970 and the early part of 1963 were affected too.

The dashed line is ordinary least squares. Because it squares every residual, the six huge residuals dominate the fit: the slope comes out at 5.04 per year with an intercept of −260.06, a line that runs too low in the 1950s and too high afterwards. It is the same method as the [linear regression simulation](/ai-simulations/linear-regression), and here you can see what one bad block of data does to it.

The solid line is a robust fit computed by iteratively reweighted least squares (IRLS). The bar chart shows the weight each year receives. Start the iteration and watch the bars for 1964 to 1969 fall while the line swings down to the true trend.

## The algorithm

A robust M-estimator replaces the squared loss with a loss that grows more slowly for large residuals. The [loss functions simulation](/ai-simulations/loss-functions) shows the shapes; here we fit with them. Huber's loss is quadratic for small residuals and linear for large ones, so an outlier still pulls but with a capped force. Tukey's bisquare goes further: beyond a cut-off the loss is flat and the outlier has no pull at all.

Minimising such a loss has no closed form, but it can be written as a sequence of weighted least-squares problems:

1. Start from a line (here a = 0, b = 0) and compute every residual r = y − a − b·x.
2. Estimate the scale of the residuals robustly: s = median|r| / 0.6745. The median ignores how extreme the outliers are, and 0.6745 makes s equal the standard deviation when the errors are normal.
3. Scale each residual: u = |r| / (c·s), with c = 1.345 for Huber and 4.685 for bisquare.
4. Turn residuals into weights. Huber: w = min(1, 1/u). Bisquare: w = (1 − u²)² when u is below 1, and 0 otherwise.
5. Solve weighted least squares for the next slope and intercept using the weighted sums of x, y, xy and x².
6. Repeat until the line stops moving.

The constants 1.345 and 4.685 are chosen so that, if the errors really are normal, the estimator keeps about 95% of the efficiency of least squares. Robustness costs almost nothing on clean data.

**Worked example.** With Huber weights the iteration settles after about 40 steps at slope 2.04 and intercept −102.53, with robust scale s = 9.01. For 1964 the fitted value is −102.53 + 2.0396 × 64 = 28.00, so the residual is 119 − 28.00 = 91.0. Scaled: u = 91.0 / (1.345 × 9.01) = 7.51, so the weight is 1/7.51 = 0.133. The other five minute-logged years get weights from 0.129 down to 0.070, and 1971 to 1973 get about 0.66 to 0.70, while every other year keeps a weight of 1. The weights add up to 17.68 out of 24. The forecast for 1974 is 48.4, against 113.0 from least squares.

Switch to bisquare and the line goes further: slope 1.10, intercept −52.30, robust scale 1.66. 1964 to 1970 all get weight exactly 0, 1963 gets 0.475, and the 1974 forecast is 29.0. The fit is reached in about four steps. These are the same values statsmodels' RLM produces with HuberT and TukeyBiweight on this data.

## Things to try

- Set the loss to bisquare and step through: the slope drops from 5.04 to 1.10 and the seven bad years are switched off completely.
- With Huber, raise the tuning multiplier to 1.5 (c = 2.02). The robust scale balloons to 49.0, nothing looks like an outlier any more and the slope jumps back to 4.84; at 2 it equals least squares. Too large a cut-off and robustness collapses.
- Lower the multiplier to 0.75 (c = 1.01): the Huber slope falls to 1.92, closer to bisquare, but more good points are down-weighted too.
- Edit the data: change 1964 to 1969 back to plausible call counts (around 23 to 33) and watch the two lines nearly meet: with values from 23.5 to 32 the robust slope is 1.37 and the least-squares slope 1.42.
- Add one extreme point at an unusual x, for example year 90 with 5 calls. Huber does not protect against such leverage points: that single row pulls the Huber slope from 2.04 down to 1.25.

## How it works in real systems

Robust regression is the standard defence when a data pipeline can produce unit errors, sensor glitches or duplicated records that you cannot clean by hand. scikit-learn's HuberRegressor fits the Huber loss with the scale as an extra parameter and defaults to epsilon = 1.35; statsmodels' RLM runs exactly the IRLS loop above with a choice of norms, using the median absolute deviation for the scale. R's MASS::rlm does the same and ships this very dataset.

The same idea appears inside deep learning. The Huber loss (smooth L1) is used for bounding-box regression in detectors such as Fast R-CNN and for value targets in deep Q-learning, where rare large errors would otherwise produce exploding gradients. Down-weighting by residual is also how many label-noise methods work: a sample whose loss stays far above the rest gets less say in the update.

Two cautions. Huber only bounds the pull of large residuals; with a quarter of the data contaminated, as here, it still leans towards the outliers, which is why bisquare (a redescending estimator) does better. And neither protects against outliers in x, high-leverage points, for which methods such as least trimmed squares or MM-estimation are used. Robust weights are a diagnostic as much as a fix: the years with weight near zero are exactly the ones to investigate. For the related question of when a model fits noise rather than signal, see [overfitting and regularisation](/ai-simulations/overfitting-and-regularisation).

## Sources

- [MASS phones dataset documentation (Rdatasets)](https://vincentarelbundock.github.io/Rdatasets/doc/MASS/phones.html)
- [MASS phones data as CSV (Rdatasets)](https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/MASS/phones.csv)
- [scikit-learn HuberRegressor documentation](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.HuberRegressor.html)
- [statsmodels Robust Linear Models documentation](https://www.statsmodels.org/stable/rlm.html)
