Robust loss and scaling
Quick answer
Robust regression reduces the influence of unusually large residuals compared with ordinary squared-error regression. A Huber loss is quadratic near zero and grows linearly beyond a chosen threshold. This reduces sensitivity to target outliers, not all forms of bad data. Examples are standalone local Python 3.13.5 snippets; library examples use scikit-learn 1.8.0. ByteMind execution is unverified.
Compare fixed-threshold Huber costs
def huber(e,d): a=abs(e) return .5*a*a if a<=d else d*(a-.5*d)print([huber(e,2) for e in [0,1,-2,5]])Expected output:
[0.0, 0.5, 2.0, 8.0]The loss is symmetric and the two branches meet at the threshold.
How it works
For a residual e and threshold delta>0, Huber loss is 0.5e² when |e|<=delta and delta(|e|-0.5*delta) otherwise. Its slope is e inside the threshold and saturates at ±delta outside it. For delta=2, residual 1 costs 0.5 and residual 5 costs 8, compared with half-squared loss 12.5 for residual 5. This is a fixed-scale teaching formula; scikit-learn HuberRegressor jointly estimates a residual scale and uses a dimensionless epsilon (at least 1), plus optional L2 regularization. Do not equate epsilon numerically with delta. RobustScaler uses training medians and interquartile ranges, reducing sensitivity of feature scaling to extreme values. Scaling features is distinct from changing the residual loss. Extreme leverage in input space can still affect robust fits. Inspect data provenance and residuals rather than dropping every unusual observation. Choose loss and scaling by validation and the error cost of the problem. MAE weights error magnitude linearly; Huber loss offers smooth transitions and a different training objective. Compare models using the same held-out metric.
Compare robustness to a target outlier
from sklearn.linear_model import LinearRegression, HuberRegressorX=[[i] for i in range(7)]y=[1,3,5,7,9,11,50]ols=LinearRegression().fit(X,y)rob=HuberRegressor(alpha=0,epsilon=1.35,max_iter=1000).fit(X,y)print(abs(rob.coef_[0]-2)<abs(ols.coef_[0]-2))Expected output:
TrueThe robust fit is closer to the uncontaminated slope in this controlled example; this is not a universal guarantee.
Common mistakes
Do not treat every outlier as an error or assume Huber protects against high-leverage points. Do not use the test set to select epsilon. The fixed threshold formula is not the full HuberRegressor optimizer.
Practice and next steps
For delta=3, compute loss for residuals -3 and 7. Explain how a changed target measurement and an extreme predictor differ as sources of influence. Complete the knowledge check and coding exercise. Next: Logit and sigmoid.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Robust loss and scaling — knowledge check
Apply robust loss and scaling to new examples.
What is the fixed-threshold Huber loss for e=5, delta=2?
How does Huber loss grow for large absolute residuals?
What does RobustScaler estimate on training data?
Is HuberRegressor epsilon the same as a fixed residual delta?
What is not guaranteed by robust residual loss?