Huber and Robust Loss Functions
Squared-error loss lets a single bad data point dominate a model's training — the Huber loss switches to a linear penalty past a threshold so one outlier can't hijack the fit.
Prerequisites: Overfitting, Outlier Detection and Winsorization
Train a model to predict next-quarter earnings growth with ordinary squared-error loss, and one restated, one-off accounting write-down in the training set can quietly rewrite the whole fit. Squared error punishes a residual of 50 with four times the weight of a residual of 25 — so the model bends itself out of shape chasing that single point at the expense of everything else.
Absolute-error loss fixes the domination problem: a residual twice as large only counts twice as much, not four times. But it has a kink at zero, so gradient-based optimizers stall and jitter right where most residuals actually live. The Huber loss is the compromise: quadratic like squared error for small residuals, where you want a smooth gradient, and linear like absolute error for large ones, where you want a damped penalty.
Huber loss behaves like squared error near the correct answer and like absolute error far from it. A single threshold, , decides where "small" ends and "large" begins — everything past it is punished in proportion to its size, not its square.
The formula, one piece at a time
Let be a residual (prediction minus actual). The Huber loss is:
In words: if the error is within of zero, square it and halve it, exactly like ordinary least squares. If it's bigger than , switch to a straight line whose slope matches the quadratic piece at the seam, so the function is smooth with no kink at and no cliff at . The constant term just keeps the two pieces meeting continuously.
Worked example
Suppose a return-forecasting model is scored with (in percentage points), and it has two residuals in the training set: a routine miss of 1.0 and a wild miss of 8.0 caused by a merger announcement no feature could have seen coming.
- Squared error. Routine miss: . Wild miss: . The wild miss contributes 64 times more to the total loss than the routine one.
- Huber loss, . Routine miss (within ): , unchanged. Wild miss (beyond ): . The wild miss still costs more, but only 28 times as much, not 64 — its influence on the gradient is capped rather than squared.
What this means in practice
Return data is full of one-off events — earnings surprises, halts, thin-liquidity prints — that are real but not representative. A model trained with squared error effectively lets its gradient step be dictated by whichever training example currently has the worst residual, which drifts as fitting proceeds and can make optimization unstable. Gradient-boosted tree libraries (XGBoost, LightGBM) expose Huber-style objectives directly, and it is a common substitute for squared error whenever a target — returns, P&L, order-fill slippage — has heavy tails.
Picking is itself a modeling choice: a small treats almost every residual as an outlier and downweights it, converging toward pure absolute-error behavior; a large recovers ordinary squared error. A common starting point is a robust scale estimate of the residuals, such as their median absolute deviation.
Related concepts
Practice in interviews
Further reading
- Huber, 'Robust Estimation of a Location Parameter' (Annals of Mathematical Statistics, 1964)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (ch. 10)