Skip to main content
Least squares gives every data point the same say in the fit, and it measures disagreement by the square of the residual. A measurement that is wrong by a large amount therefore pulls the fit toward itself with a force that grows with its error. This section measures how far one bad point can move a least squares line, and why its position along the xx axis matters as much as its error. It then fits the same data three other ways: with robust losses solved by iteratively reweighted least squares, with a Student’s t noise model, and with the inlier and outlier mixture of the EM algorithm section. The last part fits a model of the Texas electricity load to a winter that contains Winter Storm Uri, and shows which of these methods keep the storm hours from bending the fit. The notation is that of the linear regression section: pairs (x(i),y(i))(x^{(i)}, y^{(i)}) for i=1,…,mi = 1, \ldots, m, basis functions ϕ(x)\boldsymbol\phi(x), parameters θ\boldsymbol\theta, the design matrix Φ\boldsymbol\Phi with rows ϕ(x(i))⊤\boldsymbol\phi(x^{(i)})^\top, and Gaussian noise of variance σ2\sigma^2. Equation numbers refer to Bishop (Bishop, n.d.), who writes w\mathbf w for θ\boldsymbol\theta, tnt_n for y(i)y^{(i)}, NN for mm, and a precision β=1/σ2\beta = 1/\sigma^2.

The running example

The data are the line y=1+0.5xy = 1 + 0.5x observed at 60 points with Gaussian noise of standard deviation σ=0.3\sigma = 0.3, the same generator as in the EM section. The model is the straight line g(x;θ)=θ⊤ϕ(x)g(x; \boldsymbol\theta) = \boldsymbol\theta^\top \boldsymbol\phi(x) with ϕ(x)=(1,x)⊤\boldsymbol\phi(x) = (1, x)^\top, so θ0\theta_0 is the intercept and θ1\theta_1 the slope. Least squares minimizes the sum-of-squares error E(θ)=12∑i=1mei2,ei=y(i)−θ⊤ϕ(x(i)),E(\boldsymbol\theta) = \frac{1}{2}\sum_{i=1}^{m} e_i^2, \qquad e_i = y^{(i)} - \boldsymbol\theta^\top \boldsymbol\phi(x^{(i)}), which is Bishop’s Eq. 3.12. Here eie_i is the residual of point ii. The minimizer solves the normal equations, θ=(Φ⊤Φ)−1Φ⊤y\boldsymbol\theta = (\boldsymbol\Phi^\top \boldsymbol\Phi)^{-1} \boldsymbol\Phi^\top \mathbf y (Eq. 3.15). It is also the maximum likelihood estimate when the noise is Gaussian, as the conditional maximum likelihood section shows.

One bad point and leverage

Add a 61st point to the clean data and move its target yy away from the line by an amount Δ\Delta. Each target enters the least squares solution linearly, because θ=(Φ⊤Φ)−1Φ⊤y\boldsymbol\theta = (\boldsymbol\Phi^\top \boldsymbol\Phi)^{-1} \boldsymbol\Phi^\top \mathbf y is a fixed matrix times y\mathbf y. The estimate therefore moves in proportion to Δ\Delta, with no limit, so a single bad point among 61 can move the slope by any amount. In the language of robust statistics, the breakdown point of least squares, the smallest fraction of bad points that can carry the estimate arbitrarily far, is 1/m1/m, which goes to zero as mm grows. How far it moves depends on where the point sits. The fitted values are y^=Hy\hat{\mathbf y} = \mathbf H \mathbf y with the hat matrix H=Φ(Φ⊤Φ)−1Φ⊤\mathbf H = \boldsymbol\Phi(\boldsymbol\Phi^\top \boldsymbol\Phi)^{-1}\boldsymbol\Phi^\top. Its diagonal element hii=ϕ(x(i))⊤(Φ⊤Φ)−1ϕ(x(i))h_{ii} = \boldsymbol\phi(x^{(i)})^\top(\boldsymbol\Phi^\top\boldsymbol\Phi)^{-1}\boldsymbol\phi(x^{(i)}) is the leverage of point ii: the amount by which the fitted value at x(i)x^{(i)} moves when y(i)y^{(i)} moves by one unit. Points near the mean of the x(i)x^{(i)} have low leverage. Points at the ends of the range, or beyond it, have high leverage. The code places the bad point at x=5x = 5, near the middle of the data, and at x=15x = 15, beyond its end, and fits for a range of Δ\Delta.
Least squares slope against the error added to one point. The line for a point at x = 15 climbs steeply and without limit; the line for a point at x = 5 stays close to the true slope of 0.5. Least squares slope as one point of 61 is moved away from the line, for a point near the middle of the data and a point beyond its end. A point near the middle has almost no lever arm on the slope: moving it shifts the intercept, and the slope barely changes. The same error at x=15x = 15 tilts the line. Leverage depends only on the inputs x(i)x^{(i)}, not on the targets, so a bad target at a high-leverage input does the most damage, and it is also the hardest to detect from the residuals. The fit moves toward the point, so the point’s own residual stays small. The grid example at the end of this section shows the same effect.

Robust losses and iteratively reweighted least squares

A robust loss replaces the square with a function ρ\rho that grows more slowly for large residuals: Eρ(θ)=∑i=1mρ ⁣(eis),E_\rho(\boldsymbol\theta) = \sum_{i=1}^{m} \rho\!\left(\frac{e_i}{s}\right), where ss is a scale for the residuals, close to σ\sigma for the inliers. Two common choices are the Huber loss, which is quadratic up to a threshold δ\delta and linear beyond it, and the Cauchy loss, which grows only logarithmically: ρHuber(a)={12a2∣a∣≤δδ∣a∣−12δ2∣a∣>δρCauchy(a)=c22ln⁡ ⁣(1+a2c2).\rho_{\text{Huber}}(a) = \begin{cases} \tfrac12 a^2 & |a| \le \delta \\ \delta |a| - \tfrac12 \delta^2 & |a| > \delta \end{cases} \qquad \rho_{\text{Cauchy}}(a) = \frac{c^2}{2}\ln\!\left(1 + \frac{a^2}{c^2}\right). Setting the gradient of EρE_\rho to zero gives ∑iρ′(ei/s) ϕ(x(i))=0\sum_i \rho'(e_i/s)\,\boldsymbol\phi(x^{(i)}) = 0. Write ρ′(a)=w(a) a\rho'(a) = w(a)\,a, with the weight function w(a)=ρ′(a)/aw(a) = \rho'(a)/a. The condition becomes ∑iwi ei ϕ(x(i))=0\sum_i w_i\, e_i\, \boldsymbol\phi(x^{(i)}) = 0 with wi=w(ei/s)w_i = w(e_i/s), which is the normal equation of a weighted least squares problem: θ=(Φ⊤WΦ)−1Φ⊤Wy,W=diag⁡(w1,…,wm).\boldsymbol\theta = (\boldsymbol\Phi^\top \mathbf W \boldsymbol\Phi)^{-1} \boldsymbol\Phi^\top \mathbf W \mathbf y, \qquad \mathbf W = \operatorname{diag}(w_1, \ldots, w_m). This has the form of Bishop’s Eq. 14.42. The weights depend on the residuals, which depend on θ\boldsymbol\theta, so the solution is found by iteration: compute the residuals, compute the weights, solve the weighted problem, repeat. This is iteratively reweighted least squares (IRLS). The weight functions are wHuber(a)=min⁡ ⁣(1,δ∣a∣),wCauchy(a)=11+a2/c2.w_{\text{Huber}}(a) = \min\!\left(1, \frac{\delta}{|a|}\right), \qquad w_{\text{Cauchy}}(a) = \frac{1}{1 + a^2/c^2}. Huber weights fall off as 1/∣a∣1/|a|, so a point’s influence on the fit, w(a) aw(a)\,a, stops growing beyond δ\delta but never shrinks. Cauchy weights fall off as 1/a21/a^2, so the influence of a far point goes back toward zero. The scan registration section uses the same Huber weights inside ICP. The code estimates the scale at every iteration from the median absolute deviation of the residuals, s=1.4826⋅median⁡i∣ei−median⁡jej∣s = 1.4826 \cdot \operatorname{median}_i |e_i - \operatorname{median}_j e_j|, which equals σ\sigma for Gaussian residuals and ignores up to half the points. The constants δ=1.345\delta = 1.345 and c=2.385c = 2.385 are the usual ones: with Gaussian noise and no outliers they lose only 5% of the efficiency of least squares.
Repeat the single-point experiment with the two robust losses, for the point at x=15x = 15.
Slope against the error added to the high-leverage point for three methods. Least squares rises steadily off the top of the plot. Huber rises a little and levels off. Cauchy rises slightly for small errors and then returns to the true slope. Slope estimates as the high-leverage point moves away from the line. Huber caps the influence of the point, so the slope stops moving once the residual passes the threshold, but it settles at a biased value. Cauchy weights the point less and less as it moves away, so the slope returns to the true value. The Cauchy loss is not convex, so the weighted problem can have several solutions, and IRLS finds the one nearest its start. The Huber loss is convex, so IRLS reaches the same answer from any start.

The Student’s t view

Robust losses can also be read as likelihoods. Bishop’s Section 2.3.7 introduces the Student’s t distribution St(x∣μ,λ,ν)\mathrm{St}(x \mid \mu, \lambda, \nu) (Eq. 2.159) and shows in Figure 2.16 that a maximum likelihood fit under it is much less affected by a few outliers than a Gaussian fit. The reason is its form as a scale mixture of Gaussians (Eq. 2.160): St(x∣μ,λ,ν)=∫0∞N(x∣μ,(ηλ)−1) Gam(η∣ν/2,ν/2) dη.\mathrm{St}(x \mid \mu, \lambda, \nu) = \int_0^\infty \mathcal N\big(x \mid \mu, (\eta\lambda)^{-1}\big)\, \mathrm{Gam}(\eta \mid \nu/2, \nu/2)\, d\eta. Each observation has its own precision scale η\eta, drawn from a gamma distribution, so some points are allowed to be far noisier than others. Take the t distribution as the noise model of the regression, p(y(i)∣x(i))=St(y(i)∣θ⊤ϕ(x(i)),λ,ν)p(y^{(i)} \mid x^{(i)}) = \mathrm{St}(y^{(i)} \mid \boldsymbol\theta^\top\boldsymbol\phi(x^{(i)}), \lambda, \nu). Its negative log likelihood, as a function of the residual, is ν+12ln⁡(1+λei2/ν)\frac{\nu + 1}{2}\ln(1 + \lambda e_i^2/\nu) plus a constant. For ν=1\nu = 1 the t distribution is the Cauchy distribution and this is the Cauchy loss above, up to a constant factor, with c2s2=1/λc^2 s^2 = 1/\lambda. The scale mixture also explains the weights. Treat ηi\eta_i as a latent variable. Its posterior given the residual is again a gamma distribution, with mean E[ηi∣ei]=ν+1ν+λei2.\mathbb E[\eta_i \mid e_i] = \frac{\nu + 1}{\nu + \lambda e_i^2}. An EM algorithm for the t regression computes these expectations in the E step and solves a weighted least squares problem with weights E[ηi∣ei]\mathbb E[\eta_i \mid e_i] in the M step. Bishop notes that the t fit can be found by EM (Section 2.3.7). The expectations are exactly the Cauchy weights for ν=1\nu = 1, so IRLS with Cauchy weights is EM for regression with Cauchy noise.

Comparison on contaminated data

The EM section modeled the same contamination differently: each target comes either from the line with Gaussian noise, with probability π\pi, or from a uniform outlier distribution U\mathcal U: p(y(i)∣x(i))=π N(y(i)∣θ⊤ϕ(x(i)),σ2)+(1−π) U(y(i)).p(y^{(i)} \mid x^{(i)}) = \pi\, \mathcal N\big(y^{(i)} \mid \boldsymbol\theta^\top \boldsymbol\phi(x^{(i)}), \sigma^2\big) + (1 - \pi)\, \mathcal U(y^{(i)}). The E step computes the responsibility γ(zi)\gamma(z_i) of the inlier component for each point, and the M step is a weighted least squares problem with weights γ(zi)\gamma(z_i), followed by updates of σ2\sigma^2 and π\pi, which has the same form as IRLS. The weight γ(zi)\gamma(z_i) falls toward zero much faster than a Cauchy weight: once a residual is a few σ\sigma from the line, the Gaussian density is negligible next to the uniform one. The mixture also returns two things a robust loss does not, an estimate of the inlier fraction π\pi and a probability for each point that it is an inlier. The code fits all four methods to the 60-point data set of the EM section, in which 18 targets were replaced by gross errors.
Scatter of 42 inliers on a line and 18 outliers spread over the plot, with four fitted lines. The least squares line is flatter than the others; the Huber, Cauchy and EM lines lie close together along the inliers. Four fits to 60 points of which 18 are gross errors. Least squares is pulled to an intercept of 2.10 and a slope of 0.343. The three robust fits land within 0.01 of the slope of the least squares fit to the 42 true inliers, 0.508, which is the fit you would get if the outliers were known.

How much contamination each method tolerates

One data set says little about where each method fails. The code repeats the comparison on 50 random data sets for each outlier fraction from 0 to 0.7 and records the median absolute error of the slope. Each robust method starts from the least squares fit, which is what you would do without other information.
Median slope error against outlier fraction on a log scale for four methods. Least squares error grows steadily from the first outliers. Huber and Cauchy stay near the noise level up to about 0.45 and then rise sharply. The EM mixture stays near the noise level up to 0.7. Median slope error over 50 data sets per outlier fraction, each robust method started from the least squares fit. Least squares degrades from the first outlier. Huber and Cauchy stay at the noise level, a median slope error between 0.01 and 0.04, up to an outlier fraction of 0.45, and then fail: at 0.6 their errors are 0.18 and 0.16, close to least squares. The scale estimate causes the failure. The median absolute deviation ignores at most half of the residuals. Beyond that, the outliers set ss, every residual looks small relative to it, all weights approach 1, and IRLS returns to least squares. The EM mixture estimates σ\sigma from the points it believes are inliers and lets the uniform component absorb the rest, so its median error stays at 0.031 or below up to 0.7. These outliers are scattered at random, so they pull in no particular direction. Outliers that agree with each other are harder, as the next example shows.

Grid load in Winter Storm Uri

ERCOT operates the electricity grid that serves most of Texas. In mid-February 2021, Winter Storm Uri brought temperatures far below anything in the preceding winter, demand rose, and generation failed. From the early hours of 15 February, ERCOT shed load: it cut power to customers to keep the grid from collapsing. The load it reports for those hours is what it served, well below what customers would have drawn. A model of normal demand fitted to a window that includes those hours faces exactly the contamination of this section. The data are hourly ERCOT system load and a statewide temperature, from 1 December 2020 to 20 February 2021 (Central time). The load is the hourly native load from the ERCOT load archive. The temperature is the mean of the hourly readings at eight airports, one in each of ERCOT’s weather zones, from NOAA’s Integrated Surface Database. The table is published as the ercot-uri-load-temperature dataset, with the scripts that build it.
The model is the one used in the AI course’s grid assignment. Each hour is a row of a design matrix with 82 columns: the scaled temperature T=(temperature−60)/20T = (\text{temperature} - 60)/20 entered as TT, T2T^2 and T3T^3; 24 hour-of-day indicators and 7 day-of-week indicators; and the products of each hour indicator with TT and with T2T^2, which give every hour of the day its own temperature response. The model is linear in θ\boldsymbol\theta, so every method of this section applies unchanged. The indicator columns and their products are not linearly independent (each set of indicators sums to one in every row), and the solver returns the minimum-norm solution, which does not change the fitted values.
Two least squares fits serve as references. The first uses only the ordinary winter, 1 December to 7 February. The second uses the whole window, storm included. The code reports each fit’s root mean square residual on the ordinary winter, its mean residual on 15 and 16 February, and its mean residual on 14 February, the cold day before shedding began, when the load was still the demand.
Fitted to the ordinary winter, the model predicts far more load on 15 and 16 February than ERCOT served: those residuals are the shed load. Fitted with the storm included, least squares bends to meet the shed hours, which shrinks their residuals and moves the error elsewhere. On 14 February, when the load was real demand, the bent model now under-predicts. The robust fits use the whole window. Huber and Cauchy IRLS start from the least squares fit of the whole window. EM is started twice: from the same fit, and from the fit to the ordinary winter.
Hourly load from 11 to 19 February 2021 with four model curves. The served load drops sharply on 15 February. The least squares and Huber fits that include the storm dip toward the served load; the ordinary-winter fit and the EM fit stay well above it for most of 15 and 16 February, with brief dips in the EM curve. Served load and fitted load from 11 to 19 February 2021. The shaded band marks 15 and 16 February.
Inlier responsibility for each hour from 8 to 19 February 2021 for two EM runs. The run started from the ordinary winter drops to zero through 15, 16 and 17 February. The run started from least squares stays high through most of those hours. Inlier responsibility γ(zi)\gamma(z_i) per hour, 8 to 19 February, for the two EM starts. The shaded band marks 15 and 16 February.

Leverage and the starting point

Least squares fitted with the storm moves the mean residual of 15 and 16 February from −39.2-39.2 GW to −4.9-4.9 GW: the fit has bent to meet the shed hours. On 14 February, the cold day before shedding, the bent model now predicts 9.7 GW less than the real demand. Huber and Cauchy IRLS do not undo the bend. Their mean residuals on 15 and 16 February are −3.8-3.8 and −2.5-2.5 GW, because of leverage. The coldest hour of the ordinary winter was 28.4 F, and 126 hours of the storm week were colder. Those hours sit at the far end of the temperature axis, where the cubic and the per-hour quadratic terms are constrained by little else, and their mean leverage is 0.289 against 0.031 for the ordinary winter. The fit can pass close to them, and once it does their residuals are small and no weight function reduces their weight. It is the same effect as the single high-leverage point of the first experiment, produced here by a group of hours that agree with each other. The Huber loss is convex, so a different start would not change its answer. EM started from the least squares fit stops in the same place, a local maximum of the likelihood in which the storm hours are inliers. Started from the fit to the ordinary winter, EM keeps the shape of normal demand: its residual on 14 February is −0.3-0.3 GW, and its residuals on 15 and 16 February stay large and negative. Its inlier responsibilities flag 20 of the 24 hours of 15 February, when shedding began in the early morning, and every hour of 16 and 17 February. Most hours of 18 and 19 February stay flagged as well (21 and 19 of 24). They also flag 7 hours of the cold days of 12 and 13 February. On all of these days the temperature fell below the range of the ordinary winter, where the model is extrapolating. The two EM runs use the same data and the same model and differ only in where they start. A robust method downweights points that disagree with its current fit. It cannot recover once the fit already agrees with the bad points, so it needs a start that is not already contaminated. Here that start came from knowing which window was ordinary. For models with few parameters, the next section, RANSAC, finds such a start with no prior knowledge, by fitting many small random subsets of the data. It also explains why that does not work for an 82-parameter model like this one. The example has three limits. The temperature is an unweighted mean over eight airports, so it does not weight the weather by where the load is. The load during shedding is what ERCOT served, so the size of the negative residuals estimates the unserved demand only as far as the model can be trusted at temperatures it was never fitted to. The results also come from a single storm. Key references: (Bishop, n.d.; Dempster et al., n.d.)

References

  • Dempster, A., Laird, N., Rdin, D. (n.d.). Maximum Likelihood from Incomplete Data via the EM Algorithm.
  • Bishop (n.d.). Pattern Recognition and Machine Learning.

Found an error on this page? Report an issue.