Huber Loss
The Huber loss (Huber, 1964) is squared error for small residuals and absolute error for large ones. For a residual and a threshold , it is
The threshold has the units of . Both pieces equal at , and both have slope there. The derivative is the residual clipped to :
Here is the pull that one point exerts on the fit. Huber loss has seven headline facts:
- Limits: as the loss becomes , half the squared error. As , tends to , the absolute error.
- Smoothness: the loss is convex with a continuous first derivative. Its second derivative jumps from to at .
- Minimizer: the best constant solves . It tends to a median as and equals the mean once exceeds every .
- Choosing : gives asymptotic efficiency under Gaussian noise with standard deviation . In practice , where MAD is the median absolute deviation of the residuals.
- Fitting: no closed form exists. Iteratively reweighted least squares (IRLS) with weights or gradient descent solves the convex problem.
- Smooth L1: deep learning libraries define . It equals for and otherwise. Fast R-CNN (Girshick, 2015) uses it with for bounding-box regression.
- Noise model: the density proportional to has a Gaussian center and Laplace tails.
Huber loss is the usual training loss when the noise is mostly Gaussian with occasional outliers. It combines the efficiency of MSE near zero with the robustness of MAE in the tails.
Intuition
Peter Huber designed this loss in 1964 to estimate a mean from data with a few gross errors. It treats a residual smaller than as ordinary noise. It treats a larger residual as a possible outlier. Ordinary residuals get the squared penalty, which uses the data efficiently. Suspicious residuals get the absolute penalty, which caps the pull of each one at . The threshold separates the two groups, so it must match the scale of the typical noise.
Each point pulls on the fit with a strength equal to its clipped residual. Under squared error the pull grows without limit as the residual grows. Under absolute error the pull is always , even for a point that lies almost on the fit. Huber loss keeps the proportional pull for nearby points and caps it at for distant ones. Near the optimum it behaves like least squares, so gradient methods settle smoothly. Far from the optimum it behaves like absolute error, so one bad point cannot dominate a step.
The animation sweeps between and and writes for the residual. The loss is quadratic between the dashed lines and linear outside them. Its derivative is the residual clipped to .
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{grn}{HTML}{2F9E44}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw[->] (-0.20,0.0) -- (6.30,0.0) node[right] {$r$};
\draw[->] (3.0,0.0) -- (3.0,4.75) node[above] {$L_\delta(r)$};
\draw[mid, thick] (0.00,4.05) -- (0.25,3.40) -- (0.50,2.81) -- (0.75,2.28) -- (1.00,1.80) -- (1.25,1.38) -- (1.50,1.01) -- (1.75,0.70) -- (2.00,0.45) -- (2.25,0.25) -- (2.50,0.11) -- (2.75,0.03) -- (3.00,0.00) -- (3.25,0.03) -- (3.50,0.11) -- (3.75,0.25) -- (4.00,0.45) -- (4.25,0.70) -- (4.50,1.01) -- (4.75,1.38) -- (5.00,1.80) -- (5.25,2.28) -- (5.50,2.81) -- (5.75,3.40) -- (6.00,4.05);
\draw[mid, thick] (0.00,2.70) -- (3.00,0.00) -- (6.00,2.70);
\node[anchor=south] at (6.00,4.05) {\footnotesize $r^2/2$};
\node[anchor=west] at (6.05,2.70) {\footnotesize $|r|$};
\draw[->] (8.60,2.1) -- (15.10,2.1);
\node[anchor=north] at (15.00,2.05) {$r$};
\draw[->] (11.8,-0.20) -- (11.8,4.75) node[above] {$\psi(r) = L_\delta'(r)$};
\draw[mid, thick] (8.80,0.00) -- (14.80,4.20);
\draw[mid, thick] (8.80,1.40) -- (11.80,1.40) -- (11.80,2.80) -- (14.80,2.80);
\node[anchor=south west, inner sep=1pt] at (14.80,4.20) {\footnotesize $r$};
\node[anchor=west] at (14.85,2.80) {\footnotesize $\mathrm{sign}(r)$};
\draw (2.92,1.80) -- (3.08,1.80);
\draw (2.92,3.60) -- (3.08,3.60);
\draw (11.72,0.70) -- (11.88,0.70);
\draw (11.72,3.50) -- (11.88,3.50);
\draw (11.72,1.40) -- (11.88,1.40);
\draw (11.72,2.80) -- (11.88,2.80);
\draw (0.00,-0.08) -- (0.00,0.08);
\draw (8.80,2.02) -- (8.80,2.18);
\draw (1.00,-0.08) -- (1.00,0.08);
\draw (9.80,2.02) -- (9.80,2.18);
\draw (2.00,-0.08) -- (2.00,0.08);
\draw (10.80,2.02) -- (10.80,2.18);
\draw (4.00,-0.08) -- (4.00,0.08);
\draw (12.80,2.02) -- (12.80,2.18);
\draw (5.00,-0.08) -- (5.00,0.08);
\draw (13.80,2.02) -- (13.80,2.18);
\draw (6.00,-0.08) -- (6.00,0.08);
\draw (14.80,2.02) -- (14.80,2.18);
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 3s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.25) -- (2.00,0.45) -- (2.17,0.31) -- (2.33,0.20) -- (2.50,0.11) -- (2.67,0.05) -- (2.83,0.01) -- (3.00,0.00) -- (3.17,0.01) -- (3.33,0.05) -- (3.50,0.11) -- (3.67,0.20) -- (3.83,0.31) -- (4.00,0.45) -- (6.00,2.25);
\draw[grn, line width=1.6pt] (8.80,1.40) -- (10.80,1.40) -- (12.80,2.80) -- (14.80,2.80);
\draw[dashed] (2.00,0.00) -- (2.00,4.50);
\node[anchor=north] at (2.00,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.80,0.00) -- (10.80,4.50);
\node[anchor=north] at (10.80,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.00,0.45) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.80,1.40) circle (2.2pt);
\draw[dashed] (4.00,0.00) -- (4.00,4.50);
\node[anchor=north] at (4.00,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.80,0.00) -- (12.80,4.50);
\node[anchor=north] at (12.80,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.00,0.45) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.80,2.80) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.00$. \ Between the dashed lines the loss is $r^2/2$. Outside, it grows linearly.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3s = "true", 6s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.25) -- (2.00,0.45) -- (2.17,0.31) -- (2.33,0.20) -- (2.50,0.11) -- (2.67,0.05) -- (2.83,0.01) -- (3.00,0.00) -- (3.17,0.01) -- (3.33,0.05) -- (3.50,0.11) -- (3.67,0.20) -- (3.83,0.31) -- (4.00,0.45) -- (6.00,2.25);
\draw[grn, line width=1.6pt] (8.80,1.40) -- (10.80,1.40) -- (12.80,2.80) -- (14.80,2.80);
\draw[dashed] (2.00,0.00) -- (2.00,4.50);
\node[anchor=north] at (2.00,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.80,0.00) -- (10.80,4.50);
\node[anchor=north] at (10.80,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.00,0.45) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.80,1.40) circle (2.2pt);
\draw[dashed] (4.00,0.00) -- (4.00,4.50);
\node[anchor=north] at (4.00,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.80,0.00) -- (12.80,4.50);
\node[anchor=north] at (12.80,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.00,0.45) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.80,2.80) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.00$. \ The derivative $\psi(r)$ is the residual $r$ clipped to the range $[-\delta, \delta]$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6s = "true", 6.26s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.02) -- (2.12,0.34) -- (2.27,0.24) -- (2.42,0.15) -- (2.56,0.09) -- (2.71,0.04) -- (2.85,0.01) -- (3.00,0.00) -- (3.15,0.01) -- (3.29,0.04) -- (3.44,0.09) -- (3.58,0.15) -- (3.73,0.24) -- (3.88,0.34) -- (6.00,2.02);
\draw[grn, line width=1.6pt] (8.80,1.49) -- (10.93,1.49) -- (12.68,2.71) -- (14.80,2.71);
\draw[dashed] (2.12,0.00) -- (2.12,4.50);
\node[anchor=north] at (2.12,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.93,0.00) -- (10.93,4.50);
\node[anchor=north] at (10.93,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.12,0.34) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.93,1.49) circle (2.2pt);
\draw[dashed] (3.88,0.00) -- (3.88,4.50);
\node[anchor=north] at (3.88,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.68,0.00) -- (12.68,4.50);
\node[anchor=north] at (12.68,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (3.88,0.34) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.68,2.71) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 0.88$. \ $\delta$ gets smaller.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.26s = "true", 6.52s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,1.77) -- (2.25,0.25) -- (2.38,0.18) -- (2.50,0.11) -- (2.62,0.06) -- (2.75,0.03) -- (2.88,0.01) -- (3.00,0.00) -- (3.12,0.01) -- (3.25,0.03) -- (3.38,0.06) -- (3.50,0.11) -- (3.62,0.18) -- (3.75,0.25) -- (6.00,1.77);
\draw[grn, line width=1.6pt] (8.80,1.58) -- (11.05,1.58) -- (12.55,2.62) -- (14.80,2.62);
\draw[dashed] (2.25,0.00) -- (2.25,4.50);
\node[anchor=north] at (2.25,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (11.05,0.00) -- (11.05,4.50);
\node[anchor=north] at (11.05,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.25,0.25) circle (2.2pt);
\filldraw[draw=black, fill=grn] (11.05,1.58) circle (2.2pt);
\draw[dashed] (3.75,0.00) -- (3.75,4.50);
\node[anchor=north] at (3.75,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.55,0.00) -- (12.55,4.50);
\node[anchor=north] at (12.55,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (3.75,0.25) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.55,2.62) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 0.75$. \ $\delta$ gets smaller.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.52s = "true", 6.78s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,1.51) -- (2.38,0.18) -- (2.48,0.12) -- (2.58,0.08) -- (2.69,0.04) -- (2.79,0.02) -- (2.90,0.00) -- (3.00,0.00) -- (3.10,0.00) -- (3.21,0.02) -- (3.31,0.04) -- (3.42,0.08) -- (3.52,0.12) -- (3.62,0.18) -- (6.00,1.51);
\draw[grn, line width=1.6pt] (8.80,1.66) -- (11.18,1.66) -- (12.43,2.54) -- (14.80,2.54);
\draw[dashed] (2.38,0.00) -- (2.38,4.50);
\node[anchor=north] at (2.38,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (11.18,0.00) -- (11.18,4.50);
\node[anchor=north] at (11.18,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.38,0.18) circle (2.2pt);
\filldraw[draw=black, fill=grn] (11.18,1.66) circle (2.2pt);
\draw[dashed] (3.62,0.00) -- (3.62,4.50);
\node[anchor=north] at (3.62,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.43,0.00) -- (12.43,4.50);
\node[anchor=north] at (12.43,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (3.62,0.18) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.43,2.54) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 0.62$. \ $\delta$ gets smaller.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.78s = "true", 9.58s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,1.24) -- (2.50,0.11) -- (2.58,0.08) -- (2.67,0.05) -- (2.75,0.03) -- (2.83,0.01) -- (2.92,0.00) -- (3.00,0.00) -- (3.08,0.00) -- (3.17,0.01) -- (3.25,0.03) -- (3.33,0.05) -- (3.42,0.08) -- (3.50,0.11) -- (6.00,1.24);
\draw[grn, line width=1.6pt] (8.80,1.75) -- (11.30,1.75) -- (12.30,2.45) -- (14.80,2.45);
\draw[dashed] (2.50,0.00) -- (2.50,4.50);
\node[anchor=north] at (2.50,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (11.30,0.00) -- (11.30,4.50);
\node[anchor=north] at (11.30,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.50,0.11) circle (2.2pt);
\filldraw[draw=black, fill=grn] (11.30,1.75) circle (2.2pt);
\draw[dashed] (3.50,0.00) -- (3.50,4.50);
\node[anchor=north] at (3.50,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.30,0.00) -- (12.30,4.50);
\node[anchor=north] at (12.30,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (3.50,0.11) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.30,2.45) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 0.50$. \ Small $\delta$: most residuals fall in the linear part, so Huber acts like the absolute loss.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.58s = "true", 9.84s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,1.64) -- (2.31,0.21) -- (2.43,0.15) -- (2.54,0.09) -- (2.66,0.05) -- (2.77,0.02) -- (2.89,0.01) -- (3.00,0.00) -- (3.11,0.01) -- (3.23,0.02) -- (3.34,0.05) -- (3.46,0.09) -- (3.57,0.15) -- (3.69,0.21) -- (6.00,1.64);
\draw[grn, line width=1.6pt] (8.80,1.62) -- (11.11,1.62) -- (12.49,2.58) -- (14.80,2.58);
\draw[dashed] (2.31,0.00) -- (2.31,4.50);
\node[anchor=north] at (2.31,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (11.11,0.00) -- (11.11,4.50);
\node[anchor=north] at (11.11,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.31,0.21) circle (2.2pt);
\filldraw[draw=black, fill=grn] (11.11,1.62) circle (2.2pt);
\draw[dashed] (3.69,0.00) -- (3.69,4.50);
\node[anchor=north] at (3.69,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.49,0.00) -- (12.49,4.50);
\node[anchor=north] at (12.49,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (3.69,0.21) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.49,2.58) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 0.69$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.84s = "true", 10.1s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.02) -- (2.12,0.34) -- (2.27,0.24) -- (2.42,0.15) -- (2.56,0.09) -- (2.71,0.04) -- (2.85,0.01) -- (3.00,0.00) -- (3.15,0.01) -- (3.29,0.04) -- (3.44,0.09) -- (3.58,0.15) -- (3.73,0.24) -- (3.88,0.34) -- (6.00,2.02);
\draw[grn, line width=1.6pt] (8.80,1.49) -- (10.93,1.49) -- (12.68,2.71) -- (14.80,2.71);
\draw[dashed] (2.12,0.00) -- (2.12,4.50);
\node[anchor=north] at (2.12,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.93,0.00) -- (10.93,4.50);
\node[anchor=north] at (10.93,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.12,0.34) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.93,1.49) circle (2.2pt);
\draw[dashed] (3.88,0.00) -- (3.88,4.50);
\node[anchor=north] at (3.88,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.68,0.00) -- (12.68,4.50);
\node[anchor=north] at (12.68,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (3.88,0.34) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.68,2.71) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 0.88$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.1s = "true", 10.36s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.36) -- (1.94,0.51) -- (2.11,0.35) -- (2.29,0.23) -- (2.47,0.13) -- (2.65,0.06) -- (2.82,0.01) -- (3.00,0.00) -- (3.18,0.01) -- (3.35,0.06) -- (3.53,0.13) -- (3.71,0.23) -- (3.89,0.35) -- (4.06,0.51) -- (6.00,2.36);
\draw[grn, line width=1.6pt] (8.80,1.36) -- (10.74,1.36) -- (12.86,2.84) -- (14.80,2.84);
\draw[dashed] (1.94,0.00) -- (1.94,4.50);
\node[anchor=north] at (1.94,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.74,0.00) -- (10.74,4.50);
\node[anchor=north] at (10.74,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.94,0.51) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.74,1.36) circle (2.2pt);
\draw[dashed] (4.06,0.00) -- (4.06,4.50);
\node[anchor=north] at (4.06,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.86,0.00) -- (12.86,4.50);
\node[anchor=north] at (12.86,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.06,0.51) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.86,2.84) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.06$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.36s = "true", 10.62s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.67) -- (1.75,0.70) -- (1.96,0.49) -- (2.17,0.31) -- (2.38,0.18) -- (2.58,0.08) -- (2.79,0.02) -- (3.00,0.00) -- (3.21,0.02) -- (3.42,0.08) -- (3.62,0.18) -- (3.83,0.31) -- (4.04,0.49) -- (4.25,0.70) -- (6.00,2.67);
\draw[grn, line width=1.6pt] (8.80,1.23) -- (10.55,1.23) -- (13.05,2.98) -- (14.80,2.98);
\draw[dashed] (1.75,0.00) -- (1.75,4.50);
\node[anchor=north] at (1.75,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.55,0.00) -- (10.55,4.50);
\node[anchor=north] at (10.55,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.75,0.70) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.55,1.23) circle (2.2pt);
\draw[dashed] (4.25,0.00) -- (4.25,4.50);
\node[anchor=north] at (4.25,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.05,0.00) -- (13.05,4.50);
\node[anchor=north] at (13.05,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.25,0.70) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.05,2.98) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.25$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.62s = "true", 10.88s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.95) -- (1.56,0.93) -- (1.80,0.65) -- (2.04,0.41) -- (2.28,0.23) -- (2.52,0.10) -- (2.76,0.03) -- (3.00,0.00) -- (3.24,0.03) -- (3.48,0.10) -- (3.72,0.23) -- (3.96,0.41) -- (4.20,0.65) -- (4.44,0.93) -- (6.00,2.95);
\draw[grn, line width=1.6pt] (8.80,1.09) -- (10.36,1.09) -- (13.24,3.11) -- (14.80,3.11);
\draw[dashed] (1.56,0.00) -- (1.56,4.50);
\node[anchor=north] at (1.56,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.36,0.00) -- (10.36,4.50);
\node[anchor=north] at (10.36,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.56,0.93) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.36,1.09) circle (2.2pt);
\draw[dashed] (4.44,0.00) -- (4.44,4.50);
\node[anchor=north] at (4.44,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.24,0.00) -- (13.24,4.50);
\node[anchor=north] at (13.24,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.44,0.93) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.24,3.11) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.44$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.88s = "true", 11.14s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,3.20) -- (1.38,1.19) -- (1.65,0.83) -- (1.92,0.53) -- (2.19,0.30) -- (2.46,0.13) -- (2.73,0.03) -- (3.00,0.00) -- (3.27,0.03) -- (3.54,0.13) -- (3.81,0.30) -- (4.08,0.53) -- (4.35,0.83) -- (4.62,1.19) -- (6.00,3.20);
\draw[grn, line width=1.6pt] (8.80,0.96) -- (10.18,0.96) -- (13.43,3.24) -- (14.80,3.24);
\draw[dashed] (1.38,0.00) -- (1.38,4.50);
\node[anchor=north] at (1.38,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.18,0.00) -- (10.18,4.50);
\node[anchor=north] at (10.18,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.38,1.19) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.18,0.96) circle (2.2pt);
\draw[dashed] (4.62,0.00) -- (4.62,4.50);
\node[anchor=north] at (4.62,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.43,0.00) -- (13.43,4.50);
\node[anchor=north] at (13.43,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.62,1.19) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.43,3.24) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.62$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.14s = "true", 11.4s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,3.42) -- (1.19,1.48) -- (1.49,1.03) -- (1.79,0.66) -- (2.09,0.37) -- (2.40,0.16) -- (2.70,0.04) -- (3.00,0.00) -- (3.30,0.04) -- (3.60,0.16) -- (3.91,0.37) -- (4.21,0.66) -- (4.51,1.03) -- (4.81,1.48) -- (6.00,3.42);
\draw[grn, line width=1.6pt] (8.80,0.83) -- (9.99,0.83) -- (13.61,3.37) -- (14.80,3.37);
\draw[dashed] (1.19,0.00) -- (1.19,4.50);
\node[anchor=north] at (1.19,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (9.99,0.00) -- (9.99,4.50);
\node[anchor=north] at (9.99,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.19,1.48) circle (2.2pt);
\filldraw[draw=black, fill=grn] (9.99,0.83) circle (2.2pt);
\draw[dashed] (4.81,0.00) -- (4.81,4.50);
\node[anchor=north] at (4.81,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.61,0.00) -- (13.61,4.50);
\node[anchor=north] at (13.61,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.81,1.48) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.61,3.37) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.81$. \ $\delta$ gets larger.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.4s = "true", 14.2s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,3.60) -- (1.00,1.80) -- (1.33,1.25) -- (1.67,0.80) -- (2.00,0.45) -- (2.33,0.20) -- (2.67,0.05) -- (3.00,0.00) -- (3.33,0.05) -- (3.67,0.20) -- (4.00,0.45) -- (4.33,0.80) -- (4.67,1.25) -- (5.00,1.80) -- (6.00,3.60);
\draw[grn, line width=1.6pt] (8.80,0.70) -- (9.80,0.70) -- (13.80,3.50) -- (14.80,3.50);
\draw[dashed] (1.00,0.00) -- (1.00,4.50);
\node[anchor=north] at (1.00,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (9.80,0.00) -- (9.80,4.50);
\node[anchor=north] at (9.80,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.00,1.80) circle (2.2pt);
\filldraw[draw=black, fill=grn] (9.80,0.70) circle (2.2pt);
\draw[dashed] (5.00,0.00) -- (5.00,4.50);
\node[anchor=north] at (5.00,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.80,0.00) -- (13.80,4.50);
\node[anchor=north] at (13.80,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (5.00,1.80) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.80,3.50) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 2.00$. \ Large $\delta$: more residuals fall in the quadratic part, so Huber acts like the squared loss.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.2s = "true", 14.46s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,3.40) -- (1.20,1.46) -- (1.50,1.01) -- (1.80,0.65) -- (2.10,0.36) -- (2.40,0.16) -- (2.70,0.04) -- (3.00,0.00) -- (3.30,0.04) -- (3.60,0.16) -- (3.90,0.36) -- (4.20,0.65) -- (4.50,1.01) -- (4.80,1.46) -- (6.00,3.40);
\draw[grn, line width=1.6pt] (8.80,0.84) -- (10.00,0.84) -- (13.60,3.36) -- (14.80,3.36);
\draw[dashed] (1.20,0.00) -- (1.20,4.50);
\node[anchor=north] at (1.20,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.00,0.00) -- (10.00,4.50);
\node[anchor=north] at (10.00,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.20,1.46) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.00,0.84) circle (2.2pt);
\draw[dashed] (4.80,0.00) -- (4.80,4.50);
\node[anchor=north] at (4.80,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.60,0.00) -- (13.60,4.50);
\node[anchor=north] at (13.60,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.80,1.46) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.60,3.36) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.80$. \ $\delta$ returns to $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.46s = "true", 14.72s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,3.17) -- (1.40,1.15) -- (1.67,0.80) -- (1.93,0.51) -- (2.20,0.29) -- (2.47,0.13) -- (2.73,0.03) -- (3.00,0.00) -- (3.27,0.03) -- (3.53,0.13) -- (3.80,0.29) -- (4.07,0.51) -- (4.33,0.80) -- (4.60,1.15) -- (6.00,3.17);
\draw[grn, line width=1.6pt] (8.80,0.98) -- (10.20,0.98) -- (13.40,3.22) -- (14.80,3.22);
\draw[dashed] (1.40,0.00) -- (1.40,4.50);
\node[anchor=north] at (1.40,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.20,0.00) -- (10.20,4.50);
\node[anchor=north] at (10.20,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.40,1.15) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.20,0.98) circle (2.2pt);
\draw[dashed] (4.60,0.00) -- (4.60,4.50);
\node[anchor=north] at (4.60,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.40,0.00) -- (13.40,4.50);
\node[anchor=north] at (13.40,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.60,1.15) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.40,3.22) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.60$. \ $\delta$ returns to $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.72s = "true", 14.98s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.90) -- (1.60,0.88) -- (1.83,0.61) -- (2.07,0.39) -- (2.30,0.22) -- (2.53,0.10) -- (2.77,0.02) -- (3.00,0.00) -- (3.23,0.02) -- (3.47,0.10) -- (3.70,0.22) -- (3.93,0.39) -- (4.17,0.61) -- (4.40,0.88) -- (6.00,2.90);
\draw[grn, line width=1.6pt] (8.80,1.12) -- (10.40,1.12) -- (13.20,3.08) -- (14.80,3.08);
\draw[dashed] (1.60,0.00) -- (1.60,4.50);
\node[anchor=north] at (1.60,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.40,0.00) -- (10.40,4.50);
\node[anchor=north] at (10.40,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.60,0.88) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.40,1.12) circle (2.2pt);
\draw[dashed] (4.40,0.00) -- (4.40,4.50);
\node[anchor=north] at (4.40,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.20,0.00) -- (13.20,4.50);
\node[anchor=north] at (13.20,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.40,0.88) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.20,3.08) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.40$. \ $\delta$ returns to $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.98s = "true", 15.24s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.59) -- (1.80,0.65) -- (2.00,0.45) -- (2.20,0.29) -- (2.40,0.16) -- (2.60,0.07) -- (2.80,0.02) -- (3.00,0.00) -- (3.20,0.02) -- (3.40,0.07) -- (3.60,0.16) -- (3.80,0.29) -- (4.00,0.45) -- (4.20,0.65) -- (6.00,2.59);
\draw[grn, line width=1.6pt] (8.80,1.26) -- (10.60,1.26) -- (13.00,2.94) -- (14.80,2.94);
\draw[dashed] (1.80,0.00) -- (1.80,4.50);
\node[anchor=north] at (1.80,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.60,0.00) -- (10.60,4.50);
\node[anchor=north] at (10.60,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (1.80,0.65) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.60,1.26) circle (2.2pt);
\draw[dashed] (4.20,0.00) -- (4.20,4.50);
\node[anchor=north] at (4.20,-0.12) {\footnotesize $\delta$};
\draw[dashed] (13.00,0.00) -- (13.00,4.50);
\node[anchor=north] at (13.00,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.20,0.65) circle (2.2pt);
\filldraw[draw=black, fill=grn] (13.00,2.94) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.20$. \ $\delta$ returns to $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.24s = "true", 18.64s = "false", 18.64s = "false", repeats}}}]
\draw[grn, line width=1.6pt] (0.00,2.25) -- (2.00,0.45) -- (2.17,0.31) -- (2.33,0.20) -- (2.50,0.11) -- (2.67,0.05) -- (2.83,0.01) -- (3.00,0.00) -- (3.17,0.01) -- (3.33,0.05) -- (3.50,0.11) -- (3.67,0.20) -- (3.83,0.31) -- (4.00,0.45) -- (6.00,2.25);
\draw[grn, line width=1.6pt] (8.80,1.40) -- (10.80,1.40) -- (12.80,2.80) -- (14.80,2.80);
\draw[dashed] (2.00,0.00) -- (2.00,4.50);
\node[anchor=north] at (2.00,-0.12) {\footnotesize $-\delta$};
\draw[dashed] (10.80,0.00) -- (10.80,4.50);
\node[anchor=north] at (10.80,-0.12) {\footnotesize $-\delta$};
\filldraw[draw=black, fill=grn] (2.00,0.45) circle (2.2pt);
\filldraw[draw=black, fill=grn] (10.80,1.40) circle (2.2pt);
\draw[dashed] (4.00,0.00) -- (4.00,4.50);
\node[anchor=north] at (4.00,-0.12) {\footnotesize $\delta$};
\draw[dashed] (12.80,0.00) -- (12.80,4.50);
\node[anchor=north] at (12.80,-0.12) {\footnotesize $\delta$};
\filldraw[draw=black, fill=grn] (4.00,0.45) circle (2.2pt);
\filldraw[draw=black, fill=grn] (12.80,2.80) circle (2.2pt);
\node[anchor=west] at (-0.6,-0.95) {$\delta = 1.00$. \ Beyond $\pm\delta$ the derivative stays at $\pm\delta$. A huge residual moves the fit no more than one just past $\delta$.};
\end{scope}
\node[anchor=east, fill=white, inner sep=1pt] at (2.90,1.80) {\footnotesize $2$};
\node[anchor=east, fill=white, inner sep=1pt] at (2.90,3.60) {\footnotesize $4$};
\node[anchor=east, fill=white, inner sep=1pt] at (11.70,0.70) {\footnotesize $-2$};
\node[anchor=east, fill=white, inner sep=1pt] at (11.70,3.50) {\footnotesize $2$};
\node[anchor=west] at (-0.6,-1.6) {\footnotesize green: Huber, \ gray: squared loss $r^2/2$ (derivative $r$) and absolute loss $|r|$ (derivative $\mathrm{sign}(r)$)};
\end{tikzpicture}
\end{document}Properties
(Bounded Influence) No single residual can move the Huber fit by more than a bounded amount.
An M-estimator is defined by an equation for some function . In robust statistics, the influence function measures how much one added point with residual moves an estimate. For an M-estimator it is proportional to . Under squared error it grows linearly in without limit. Under Huber loss it grows linearly up to and then stays flat. Huber regression therefore behaves like least squares that clips each residual at before using it. The IRLS weights show the same rule. Points inside the threshold keep weight , and a point at gets weight .
(Where 1.345 Comes From) The constant gives up of Gaussian efficiency in exchange for bounded influence.
An M-estimator has asymptotic variance , where is the noise. For standard Gaussian noise and , , the probability that a residual falls inside the threshold. The efficiency is the variance of the mean divided by this variance. It is at , at , at and at . A smaller gives more robustness and less efficiency. Common robust regression software, such as R’s rlm and statsmodels, uses as the default.
(Minimax Robustness) Huber’s estimator minimizes the worst-case variance when a fraction of the data comes from an unknown distribution.
Huber (1964) modeled the noise as , where is any symmetric distribution and is the contaminated fraction. Among M-estimators of location, the Huber estimator minimizes the largest asymptotic variance over all such . Its optimal threshold solves
where and are the standard normal density and CDF. The solution is for , for and for . The worst-case yields the density with a Gaussian center and Laplace tails. Huber loss is the maximum-likelihood loss for that density.
(Scale Must Be Estimated) The threshold has units, so the fit depends on an estimate of the noise scale.
Doubling every with held fixed changes which points count as outliers. Robust regression software therefore sets with a scale estimate . The sample standard deviation is a poor choice, since the outliers inflate it and raise . The median absolute deviation (MAD) resists them. For Gaussian data , because is the th percentile of the standard normal. Hence . Fitting procedures alternate between updating the coefficients and updating from the current residuals. Deep learning code usually standardizes the targets and fixes by hand.
(Smooth L1 in Deep Learning) Smooth L1 loss is a rescaled Huber loss whose gradient never exceeds in size.
Fast R-CNN predicts bounding-box offsets whose errors can be large early in training. Girshick (2015) reports that L2 loss then needs careful tuning of the learning rate to avoid exploding gradients. Smooth L1 has gradient inside the threshold and outside. Its gradient therefore never exceeds in size, which acts like gradient clipping inside the loss. PyTorch offers HuberLoss(delta) and SmoothL1Loss(beta), and the two differ by the factor . Near zero, the quadratic piece makes small corrections for small errors, where L1 loss would take full-size steps.
(Bad Inputs Still Hurt) Huber loss caps the pull of large residuals and leaves the pull of extreme inputs uncapped.
For a linear model, point contributes to the gradient with respect to . The factor is capped at . The factor has no cap. A point with an extreme input can tilt the fit until its own residual is small. It then counts as an ordinary point. Huber regression therefore has the same breakdown point as least squares when inputs can be corrupted. Weighted variants, called GM-estimators, also down-weight extreme inputs. When outliers are a large fraction of the data, 4.4.1. RANSAC is the safer method.
Derivation
Huber loss follows from a model in which each point may carry an outlier shift. Write the residual as , where is a gross error and is ordinary noise. Charge for the noise and for the shift, and pick the cheapest :
The inner problem is a one-dimensional lasso. Its solution is soft-thresholding, . If , then and the cost is . If , then and the cost is
Both cases match the definition. Differentiating the minimum value with respect to at the optimal gives , the clipped residual. Fitting a model with Huber loss therefore equals fitting least squares jointly with a sparse vector of outlier shifts . Only points whose residual exceeds receive a nonzero shift.
Examples
(Easy) Four Sensor Residuals
A temperature model has residuals degrees Celsius on four test readings. Use .
Compute the Huber loss of each residual and compare it with half the squared error. What share of the total pull comes from the worst reading? and . The other two lie outside, so their losses are and . The Huber total is . Half the squared error gives , , and , a total of . The pulls under Huber loss are , with total size , so the worst reading supplies . Under squared error the pulls are the residuals themselves, with total size , so the worst reading supplies .
The first two residuals lie inside the threshold, so their losses are
(Medium) A Robust Typical Delivery Time
Six deliveries on one route took minutes. The -minute trip hit a road closure. Use minutes.
Find the Huber estimate of the typical time and compare it with the mean and the median. -minute trip lies outside the threshold. Its pull is then , and the equation becomes . The solution is . The check confirms the guess. The other residuals are , all inside , and the outlier's residual is . The mean is and the median is . The Huber estimate depends on . It equals the median for . Between and it equals , for example at . Here is the sum of the five ordinary trips. From on, no residual exceeds the threshold, and the estimate is the mean .
Guess that only the
(Hard) Setting the Threshold From the Data
A delivery-time model has residuals minutes on eight training trips. The last trip was delayed by a road closure.
Set with from the MAD, and find the IRLS weights. What happens if is the sample standard deviation instead? The median residual is . The absolute deviations from it, sorted, are . Their median is . Then and minutes. Only the residual exceeds . Its weight is , and every other trip keeps weight . The sample standard deviation is minutes, against without the delayed trip. It gives and a weight of for the delayed trip. The outlier inflates its own threshold and keeps more than twice the weight.
See Also
- 2.1.1. Mean Squared Error and 2.1.2. Mean Absolute Error, the two losses that Huber loss joins.
- 3.2.2. Lasso, whose soft-thresholding step appears in the Derivation.
- 4.4.1. RANSAC, for data with many outliers or corrupted inputs.
- 6.4. Gradient Descent, whose steps Huber loss keeps bounded.
- 1.2.1 Maximum Likelihood Estimation, which links each loss to a noise density.