Unit 4, lesson 26

Robust Regression and Quantile Prediction

How the loss decides what a regression predicts, how robust regression with Huber loss limits the pull of large residuals, and how pinball loss, quantile regression and asymmetric costs target a conditional quantile.

  • Video 26
  • Regression Metrics and Loss Choice

A shop orders stock before it knows the next day’s demand. Each unit short costs 4 currency units in lost sales and each unit left over costs 1. Should the order aim at the average demand, and how far should one extreme day in the training data move the forecast? This lesson shows that the loss decides which summary of the conditional distribution a model predicts, how Huber loss limits the pull of large residuals, and how pinball loss turns unequal costs into a quantile.

Key terms

Robust regression

Definition In this lesson the residual of a prediction qq for an outcome yy is u=y−qu = y - q, actual minus prediction, so u>0u > 0 is an underprediction, a shortage, and u<0u < 0 is an overprediction, an excess. The lesson on regression metrics used the opposite sign. Squared, absolute and Huber losses do not depend on the sign, but asymmetric losses do.

A loss L(u)L(u) acts on the fitted parameters through its derivative. Squared loss u2u^2 has derivative 2u2u, which grows without bound, so one observation with a huge residual can move a least-squares fit by an arbitrarily large amount. Robust regression fits a regression model with a loss, or a weighting of the observations, chosen so that a few unusual observations have limited influence on the fit. A common choice is a loss with a bounded derivative, such as the absolute loss ∣u∣\lvert u \rvert, whose derivative is ±1\pm 1 for u≠0u \ne 0, or the Huber loss below. For a constant prediction without features, squared loss is minimized by the sample mean and absolute loss by a sample median. A bounded derivative limits the influence of large residuals but not of high-leverage points, observations whose features lie far from the rest, because the gradient with respect to a weight multiplies L′(u)L'(u) by the feature value.

In plain words Least squares listens hardest to the points it fits worst, so a single extreme value can drag the whole fit toward itself. A loss that grows only linearly for large residuals gives every far-off point the same fixed pull. Each method has robustness to some departures and not to others, and an extreme value can be a real rare event that matters for the decision. Investigate it and choose the loss for the task, rather than using a robust loss to make inconvenient data disappear.

Example The constructed training outcomes 10, 11, 12, 13 and 14 have mean and median 12. Replace 14 by 40, kept as a valid extreme observation for this sensitivity check. The mean becomes 86/5=17.286/5 = 17.2, while the median stays 12. At a residual of size 1, squared and absolute loss are both 1, and at size 6 they are 36 and 6. For the constant prediction 12, the outcome 40 has residual 28, so its squared loss has a derivative of size 56, while its absolute loss has a derivative of size 1.

Huber loss

Definition For a threshold δ>0\delta > 0, measured in target units, the Huber loss of a residual uu is Hδ(u)=12u2H_\delta(u) = \tfrac12 u^2 when ∣u∣≤δ\lvert u \rvert \le \delta and

Hδ(u)=δ(∣u∣−12δ)H_\delta(u) = \delta\bigl(\lvert u \rvert - \tfrac12 \delta\bigr)

when ∣u∣>δ\lvert u \rvert > \delta. The two pieces agree in value and in slope at ∣u∣=δ\lvert u \rvert = \delta, so the loss is convex and continuously differentiable. Its derivative is Hδ′(u)=uH_\delta'(u) = u for ∣u∣≤δ\lvert u \rvert \le \delta and δ\delta times the sign of uu beyond that, so it never exceeds δ\delta in absolute value. Huber regression with a linear model chooses bb and w\mathbf{w} to minimize the convex objective

1n∑i=1nHδ(yi−b−w⊤xi).\frac{1}{n} \sum_{i=1}^{n} H_\delta\bigl(y_i - b - \mathbf{w}^{\top}\mathbf{x}_i\bigr) .

The gradient of term ii with respect to w\mathbf{w} is −Hδ′(ui) xi-H_\delta'(u_i)\,\mathbf{x}_i, which is capped in the residual but not in the features. The minimizer of the expected Huber loss is in general neither the conditional mean nor the conditional median, although for a conditional distribution with finite mean that is symmetric about a point, that point is a minimizer.

In plain words Huber loss behaves like squared loss for small residuals, smooth near zero, and like absolute loss for large ones, with a fixed pull. The threshold decides what counts as large, in target units, so it has to match the scale of the residuals, and any scale estimated for it should come from the training data only. With a very large threshold the loss acts like half the squared loss, and with a very small one the scaled loss Hδ(u)/δH_\delta(u)/\delta is close to ∣u∣\lvert u \rvert.

Example With δ=2\delta = 2, the Huber loss is 12\tfrac12 at u=1u = 1, equals 2 at the boundary u=2u = 2 under both formulas, and is 2(6−1)=102(6 - 1) = 10 at u=6u = 6. Because Huber loss halves the square near zero, compare its shape with squared loss rather than its raw values. For the outcomes 10, 11, 12, 13 and 40 and a constant prediction qq, the derivative of ∑iH2(yi−q)\sum_i H_2(y_i - q) with respect to qq is −∑iH2′(yi−q)-\sum_i H_2'(y_i - q). At q=12q = 12 the residuals are −2,−1,0,1,28-2, -1, 0, 1, 28, which clip to −2,−1,0,1,2-2, -1, 0, 1, 2 and sum to 0. The objective is convex, so 12 is a minimizer, and in fact the only one. The value 40 pulls with strength 2, not 28, and the Huber fit stays at 12 while the mean moves to 17.2.

Asymmetric cost

Definition An asymmetric cost charges different amounts for an underprediction and an overprediction of the same size. With u=y−qu = y - q, a per-unit shortage cost cu>0c_u > 0 and a per-unit excess cost co>0c_o > 0, the linear asymmetric cost is

C(u)=cumax⁡(u,0)+comax⁡(−u,0).\begin{aligned} C(u) &= c_u \max(u, 0) \\[0.4em] &\quad + c_o \max(-u, 0) . \end{aligned}

Dividing by cu+coc_u + c_o does not change which prediction minimizes the cost or its expected value, and it turns the cost into the pinball loss below with level

τ=cucu+co.\tau = \frac{c_u}{c_u + c_o} .

This model assumes that the cost is proportional to the number of units short or over. Fixed fees, capacity limits or stock carried into the next period need a different decision analysis.

In plain words When running short hurts more than holding extra, the best order sits above the middle of the demand distribution, and the ratio of the two costs, not their size, decides how far above. Equal costs give the level one half, the median.

Example With a shortage cost of 4 and an excess cost of 1 per unit, the level is τ=4/(4+1)=0.8\tau = 4/(4 + 1) = 0.8. Five units short costs 20 and five units over costs 5. After dividing by 5 these become 4 and 1.

Pinball loss

Definition For a level τ\tau with 0<τ<10 < \tau < 1, the pinball loss, also called quantile loss, of a residual u=y−qu = y - q is

ρτ(u)={τuu≥0(τ−1)uu<0\rho_\tau(u) = \begin{cases} \tau u & u \ge 0 \\ (\tau - 1)u & u < 0 \end{cases}

Equivalently ρτ(u)=max⁡(τu,(τ−1)u)\rho_\tau(u) = \max\bigl(\tau u, (\tau - 1)u\bigr). It is zero at u=0u = 0, positive elsewhere, convex and piecewise linear, with slope τ\tau for underpredictions and τ−1\tau - 1 for overpredictions, and ρ1/2(u)=12∣u∣\rho_{1/2}(u) = \tfrac12 \lvert u \rvert. For outcomes y1,…,yny_1, \dots, y_n, a constant qq minimizes ∑iρτ(yi−q)\sum_i \rho_\tau(y_i - q) if and only if

#{i:yi<q}≤nτ,#{i:yi≤q}≥nτ,\begin{gathered} \#\{i : y_i < q\} \le n\tau , \\[0.4em] \#\{i : y_i \le q\} \ge n\tau , \end{gathered}

that is, when qq is a sample τ\tau-quantile. The smallest such qq is the lower sample quantile.

In plain words Pinball loss tilts the absolute loss. With τ=0.8\tau = 0.8, a unit of shortfall costs four times as much as a unit of surplus, so the best constant moves up until at most a fifth of the outcomes lie above it and at most four fifths lie below it. Several constants can tie, and software then reports one of them by a convention, often the lower quantile.

Example With τ=0.8\tau = 0.8, a shortage of 5 gives ρ0.8(5)=4\rho_{0.8}(5) = 4 and an excess of 5 gives ρ0.8(−5)=1\rho_{0.8}(-5) = 1. For the outcomes 10, 11, 12, 13 and 40 and q=13q = 13, the residuals are −3,−2,−1,0,27-3, -2, -1, 0, 27 and the losses are 0.6, 0.4, 0.2, 0 and 21.6, a total of 22.8. Between 13 and 40, four outcomes lie at or below qq and one lies above, so raising qq by one unit adds 4⋅0.2=0.84 \cdot 0.2 = 0.8 and removes 0.80.8. Every qq from 13 to 40 gives the total 22.8, and every qq outside that interval gives more, for example 23.8 at q=12q = 12. The lower quantile convention reports 13.

Conditional quantile

Definition Let YY be a real-valued target and XX the features, with conditional cumulative distribution function, or conditional CDF, F(y∣x)=P(Y≤y∣X=x)F(y \mid x) = P(Y \le y \mid X = x). For 0<τ<10 < \tau < 1, a number qq is a conditional τ\tau-quantile of YY given X=xX = x if

P(Y<q∣X=x)≤τ,P(Y≤q∣X=x)≥τ.\begin{gathered} P(Y < q \mid X = x) \le \tau , \\[0.4em] P(Y \le q \mid X = x) \ge \tau . \end{gathered}

The set of such numbers is a closed bounded interval, often a single point, and its left end inf⁡{y:F(y∣x)≥τ}\inf\{y : F(y \mid x) \ge \tau\} is the lower conditional quantile. The level τ=1/2\tau = 1/2 gives the conditional median. If E[∣Y∣∣X=x]<∞E[\lvert Y \rvert \mid X = x] < \infty, then qq minimizes the expected pinball loss E[ρτ(Y−q)∣X=x]E[\rho_\tau(Y - q) \mid X = x] if and only if qq is a conditional τ\tau-quantile, and at any qq where F(⋅∣x)F(\cdot \mid x) is continuous, the derivative of this expected loss is F(q∣x)−τF(q \mid x) - \tau. Because ∣u∣=2ρ1/2(u)\lvert u \rvert = 2\rho_{1/2}(u), the same holds for absolute loss and the conditional median. If E[Y2∣X=x]<∞E[Y^2 \mid X = x] < \infty, then E[(Y−q)2∣X=x]=Var⁡(Y∣X=x)+(q−E[Y∣X=x])2E[(Y - q)^2 \mid X = x] = \operatorname{Var}(Y \mid X = x) + (q - E[Y \mid X = x])^2, so the conditional mean is the unique minimizer of the expected squared loss.

In plain words Cases with identical features still have varying outcomes. The conditional mean, the conditional median and the conditional quantiles are different summaries of that one conditional distribution, and the loss decides which summary a prediction aims at. A conditional 0.8-quantile is a cutoff with at least 80 percent of the conditional probability at or below it and at most 80 percent strictly below it.

Example Suppose that for stores with one particular feature value, a constructed demand distribution puts probability 0.3 on each of 10, 20 and 30 units and 0.1 on 40 units. The conditional CDF is 0.3, 0.6, 0.9 and 1 at these values. The conditional mean is 22, the median is 20 because 0.3≤0.5≤0.60.3 \le 0.5 \le 0.6, and the 0.8-quantile is 30 because 0.6≤0.8≤0.90.6 \le 0.8 \le 0.9. With a shortage cost of 4 and an excess cost of 1 per unit, the expected cost of ordering 30 is

0.1⋅4⋅10+0.3⋅20+0.3⋅10=13,\begin{aligned} &0.1 \cdot 4 \cdot 10 + 0.3 \cdot 20 \\[0.4em] &\quad + 0.3 \cdot 10 = 13 , \end{aligned}

against 21 for ordering the mean and 23 for ordering the median. Between support points the expected cost has slope 5(F(q∣x)−0.8)5\bigl(F(q \mid x) - 0.8\bigr), which is −1-1 from 20 to 30 and 0.50.5 from 30 to 40, so 30 is the only best order.

Quantile regression

Definition Quantile regression at a fixed level τ\tau fits a model of the conditional τ\tau-quantile, for example the linear model qτ(x)=b+w⊤xq_\tau(\mathbf{x}) = b + \mathbf{w}^{\top}\mathbf{x}, by choosing the parameters that minimize the average training pinball loss

1n∑i=1nρτ(yi−b−w⊤xi).\frac{1}{n} \sum_{i=1}^{n} \rho_\tau\bigl(y_i - b - \mathbf{w}^{\top}\mathbf{x}_i\bigr) .

The objective is convex but not differentiable where a residual is zero. A minimizer always exists, can be computed by linear programming and need not be unique. When the model has an intercept, every minimizer leaves at most nτn\tau training points strictly below the fitted function and at least nτn\tau at or below it. If E[∣Y∣]<∞E[\lvert Y \rvert] < \infty and the model class contains the true conditional quantile function, that function minimizes the expected pinball loss over the class, so the fit estimates it. Fits at different levels are separate problems and can cross. Two fitted quantiles give a candidate prediction interval, but its nominal level does not guarantee coverage on new data.

In plain words Quantile regression lets the cutoff move with the features, so a weekend day can get a higher order than a weekday instead of one percentile copied onto every day. The fitted function is still an estimate. Model error, a small sample, choices made while tuning and changes in the population between training and use can all move the true fraction of new outcomes below it away from τ\tau, so check the coverage and the order of the endpoints on held-out data.

Example A constructed history has six weekdays with demands 8, 10, 11, 12, 14 and 20 and six weekend days with demands 20, 24, 26, 30, 34 and 50. Let x=0x = 0 on weekdays and x=1x = 1 on weekends, and fit q0.8(x)=b+wxq_{0.8}(x) = b + wx. The model has one free value per group, so the fit is the sample 0.8-quantile within each group, 14 for weekdays and 34 for weekends, each unique, which gives b=14b = 14 and w=20w = 20. The pooled 0.8-quantile of all twelve demands is 30. Copied onto every day, it would lie above all six weekday demands and below two of the six weekend demands. The fitted function has 8 of the 12 training points strictly below it and 10 at or below it, and 8≤9.6≤108 \le 9.6 \le 10 as the intercept property requires.

Check yourself

  1. Take τ=0.75\tau = 0.75 and an actual demand of 12 units. Compute the pinball loss of the predictions 9 and 15, say which direction of error is penalized more, and give the cost ratio this level represents.
Show the answer

For the prediction 9 the residual is u=12−9=3u = 12 - 9 = 3, an underprediction, so the loss is 0.75⋅3=2.250.75 \cdot 3 = 2.25. For the prediction 15 the residual is u=−3u = -3, an overprediction, so the loss is (0.75−1)(−3)=0.75(0.75 - 1)(-3) = 0.75.

The two misses have the same size, but the underprediction costs three times as much. Since τ=cu/(cu+co)=3/4\tau = c_u/(c_u + c_o) = 3/4, this level matches a shortage cost three times the excess cost per unit, under the linear cost model.

  1. Find the lower 0.75-quantile of the four equally weighted outcomes 2, 4, 9 and 10. Then show that the constants 9 and 10 have the same total pinball loss at this level, and explain why two programs could report different answers without either being wrong.
Show the answer

The empirical CDF is 0.25, 0.5, 0.75 and 1 at 2, 4, 9 and 10. It first reaches 0.75 at 9, so the lower 0.75-quantile is 9.

At q=9q = 9 the residuals are −7-7, −5-5, 00 and 11, so the total loss is 0.25⋅7+0.25⋅5+0+0.75⋅1=3.750.25 \cdot 7 + 0.25 \cdot 5 + 0 + 0.75 \cdot 1 = 3.75. At q=10q = 10 they are −8-8, −6-6, −1-1 and 00, so the total is 0.25⋅(8+6+1)=3.750.25 \cdot (8 + 6 + 1) = 3.75. For any qq strictly between 9 and 10, three outcomes lie below and one above, so the total changes at the rate 3⋅0.25−0.75=03 \cdot 0.25 - 0.75 = 0 and stays at 3.75. At 8 or 11 it is 4.75.

With nτ=3n\tau = 3, every qq from 9 to 10 satisfies #{i:yi<q}≤3≤#{i:yi≤q}\#\{i : y_i < q\} \le 3 \le \#\{i : y_i \le q\}, so the whole interval minimizes the loss. A program using the lower convention reports 9, and another may report 10 or the midpoint 9.5. All are minimizers, which is why a report should name its convention.

  1. A linear model with one feature is fitted with Huber loss and δ=2\delta = 2. At the current parameters, two training points both have residual u=10u = 10, one with feature value x=1x = 1 and one with x=40x = 40. Find each point’s contribution to the gradient with respect to the weight ww, compare with half squared loss, and judge the claim that Huber loss protects the fit from any single unusual observation.
Show the answer

The gradient of H2(y−b−wx)H_2(y - b - wx) with respect to ww is −H2′(u) x-H_2'(u)\,x, and the residual 10 is clipped to 2. The contributions are −2⋅1=−2-2 \cdot 1 = -2 and −2⋅40=−80-2 \cdot 40 = -80. Under half squared loss, 12u2\tfrac12 u^2, they would be −10⋅1=−10-10 \cdot 1 = -10 and −10⋅40=−400-10 \cdot 40 = -400.

Huber loss shrinks both contributions by the same factor 5, but the point at x=40x = 40 still pulls on the slope 40 times as hard as the point at x=1x = 1. The claim is false. Huber loss caps the influence of a large residual, not the influence of a high-leverage point. Such a point can even tilt the line until its own residual is small, so that its term is never clipped. In a constructed check with five points on the line y=xy = x at x=1,…,5x = 1, \dots, 5 and a sixth point at (40,0)(40, 0), the Huber fit with δ=2\delta = 2 has slope about −0.07-0.07, almost the least-squares slope, and the residual of the far point is only about −0.27-0.27.

How this lesson connects