Foundations to Frontier
Tests
D. Classical Machine Learning › D1 Linear Models for Regression

Locally Weighted Regression (LOESS)

LOESS (LOWESS) fits a smooth curve through scatterplot data with no global formula: at each point it fits a low-degree polynomial by weighted least squares to the nearest fraction of the data, with weights that fall off with distance. A single span parameter trades bias against variance, local linear fits remove the boundary bias of moving averages, and robustness iterations protect against outliers. This page covers the algorithm, tricube and bisquare weights, the smoother matrix and effective degrees of freedom, choosing the span by cross-validation, the bias–variance of local polynomials, and the curse of dimensionality. In the exercises you implement LOESS (degree 0–2, with robustness iterations) through its smoother matrix; then you smooth battery-temperature traces and compare local constant, linear and quadratic fits with cross-validated spans in the interior and at the boundaries.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A ruler that bends

A straight line through all the data is too stiff, and a high-degree polynomial is wild. LOESS takes another route: to estimate the curve at one spot, look only at nearby points, trust the closest ones most, fit a small straight line through them, and read off its height at that spot. Slide along and repeat. Together, the many small local fits make a smooth curve that follows the data without being told its shape.

LOESS: a small weighted fit around every point
degree
estimate at x₀: 0.523 · truth 0.409 · window half-width h = 0.201 · degree 1
Dashed: the true curve; green: the LOESS curve; orange: the current window's points (size = tricube weight) and its local fit. At the edges the window is one-sided: a local constant (degree 0) averages points that all lie on one side, so it is biased toward the interior, while a local line follows the trend to the edge.
Why engineers care

Telemetry trends, calibration drifts and test data rarely follow a known formula. LOESS is the go-to exploratory smoother. It is used for trend lines in plots, detrending before anomaly detection, and the trend in STL seasonal decomposition. Its robust version shrugs off telemetry glitches.

📝 Quick check (Level 0)
0/3 answered
  1. 1. LOESS estimates the curve at a point using…
  2. 2. The span controls…
  3. 3. In the demo, near the edges a local constant fit is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

One local fit by hand

Data: x = 0, 1, 2, 3, 4 and y = 1, 2, 1, 4, 3. Use tricube weights w = (1 − |d/h|³)³.

At x₀ = 2 with h = 3:

  • The weights for distances 0, 1, 2 are 1, 0.893 and 0.349.
  • The local constant (weighted mean) is 7.752/3.483 = 2.226.
  • The local line gives the same intercept, 2.226. With symmetric weights, Σw(x − x₀) = 0, so the slope does not affect the value at x₀.

At the edge, x₀ = 0 with h = 4.5:

  • The weights are 1, 0.967, 0.759, 0.349 and 0.026, all on one side.
  • The weighted mean is 1.666: pulled up by the larger y values to the right.
  • The local line gives 0.993, much closer to the global trend’s value at 0 (the least-squares line is 0.6 + x, so 0.6).

The steps in plain English:

  1. Choose the span (fraction of points per window, e.g. 0.3) and the degree (1 or 2).
  2. For each target x₀, take the span·n nearest points; h is the distance to the farthest of them.
  3. Weight them by tricube(|x − x₀|/h).
  4. Fit a weighted polynomial and keep its value at x₀.
  5. Robust version: compute residuals, down-weight large ones with bisquare weights, and refit (2–3 times).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Tricube weight at distance 0?
  2. 2. Tricube weight at distance h or beyond?
  3. 3. With symmetric weights, the local-line and local-mean values at x₀ are…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The algorithm

Local polynomial estimate: f̂(x₀) = β̂₀(x₀), where β̂(x₀) minimises Σᵢ wᵢ(x₀)(yᵢ − Σₖ βₖ(xᵢ − x₀)ᵏ)².

Tricube weights: wᵢ = (1 − |(xᵢ − x₀)/h(x₀)|³)³ inside the window, 0 outside. The nearest-neighbour bandwidth h(x₀) adapts to the local density of x.

Linear smoother. Each fitted value is a linear combination of the yᵢ, so ŷ = Ly with the smoother matrix L:

  • each row of L sums to 1;
  • local linear fits reproduce straight lines exactly (L(a + bx) = a + bx);
  • the effective degrees of freedom are tr(L): 6.5 for the span 0.3 in exercise 1.

Robustness iterations (LOWESS). From the residuals, set s = 6·median|r|, then weights δᵢ = (1 − (rᵢ/s)²)² for |rᵢ| below s and 0 otherwise. Refit with wᵢ·δᵢ. Two or three iterations suffice.

Choosing the span:

  • Small spans give low bias and high variance (wiggly); large spans the reverse.
  • Use leave-one-out CV in closed form, mean((yᵢ − ŷᵢ)/(1 − Lᵢᵢ))², or GCV.
from statsmodels.nonparametric.smoothers_lowess import lowess
smoothed = lowess(y, x, frac=0.3, it=3, return_sorted=True)      # degree 1, 3 robustness iterations
# R: loess(y ~ x, span = 0.3, degree = 2, family = "symmetric")

Use it when you need a smooth trend in one or two dimensions with no formula, and n is moderate (up to ~10⁴–10⁵). Avoid it in high dimensions, where neighbourhoods are empty (curse of dimensionality), or when you need an explicit model to extrapolate.

🐍 Exercise 1: LOESS through its smoother matrix
Implement tricube weights, the LOESS smoother matrix for local polynomials of degree 0–2 with nearest-neighbour bandwidths, and LOESS with bisquare robustness iterations; check that rows sum to one and lines are reproduced, that it recovers a noisy sine, and that robustness iterations remove the effect of 6% spikes.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Effective degrees of freedom of a linear smoother?
  2. 2. LOO-CV for a linear smoother needs…
  3. 3. Robustness weights in LOWESS use…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Bias, variance and dimension

Bias. For a local polynomial of degree d with bandwidth h:

  • in the interior, the bias is O(h^(d+1)) for odd d and O(h^(d+2)) for even d (the even-order term cancels by symmetry), so local linear and local constant both have O(h²) interior bias;
  • at the boundary, local constant has O(h) bias, while local linear keeps O(h²);
  • this automatic boundary correction is why degree 1 is the default (Fan and Gijbels, 1996).

Variance is about σ²/(nh·density), larger for higher degree at the same h.

Rates. The optimal h is about n^(−1/5) for degree 1, giving MSE ~ n^(−4/5). This is slower than parametric n⁻¹ but needs no model.

Equivalent kernels. Each row of L is an “equivalent kernel”. For local linear fits, it adapts its shape near boundaries and in sparse regions. Local polynomial regression is minimax-efficient among linear smoothers (Fan, 1993).

Curse of dimensionality. In d dimensions a window holding a fraction s of uniform data has side length s^(1/d). With d = 10 and s = 0.1, that is 0.79 of each axis: not local at all. LOESS is practical up to about 3–4 dimensions. Additive models (D1.20) avoid the problem.

Relatives:

  • kernel regression (Nadaraya–Watson = degree 0);
  • smoothing splines (D1.20), which are nearly equivalent to a kernel smoother with variable bandwidth;
  • k-NN regression (D4), an unweighted local constant;
  • Savitzky–Golay filters, local polynomials on an evenly spaced grid.

Interview questions.

  • Why degree 1 rather than 0? It removes the first-order boundary bias and design bias at no real cost.
  • How is LOESS different from a moving average? The window adapts to the data density, the weights are smooth, it fits local slopes, and it has a robust option.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Boundary bias of local constant regression is of order…
  2. 2. A 10-D window holding 10% of uniform data spans what fraction of each axis?
  3. 3. Savitzky–Golay filtering is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Smoothing a battery-temperature trace

History.

  • Local polynomial smoothing goes back to actuarial graduation (Woolhouse, 1870; Spencer’s formulas).
  • Savitzky and Golay (1964) applied it to spectra.
  • Cleveland (1979) introduced robust LOWESS.
  • Cleveland and Devlin (1988) extended it to LOESS with multiple predictors and inference.
  • Fan (1992–93) and Fan and Gijbels (1996) established the theory of boundary correction and minimax efficiency.
  • STL (Cleveland et al., 1990) uses LOESS for seasonal decomposition.

Exercise 1.

  • On a noisy sine (n = 200, σ = 0.2), LOESS (span 0.3, degree 1) has MSE 0.0041 with 6.5 effective degrees of freedom.
  • With 6% spikes of +4, plain LOESS has MSE 0.069. Three bisquare robustness iterations bring it back to 0.0071.

Exercise 2: battery temperature over an orbit. Setup: the true curve is 2t + sin 2πt, with σ = 0.3 and n = 100. Each smoother’s span is chosen by leave-one-out CV; 40 traces.

Smoother Interior MSE (t in 0.1–0.9) Boundary MSE Median CV span
Local constant (degree 0) 0.0120 0.0372 0.15
Local linear (degree 1) 0.0091 0.0152 0.30
Local quadratic (degree 2) 0.0066 0.0133 0.70
  • Local constant suffers badly at the boundary: 3 times its interior error, and 2.4 times local linear’s. Its windows are one-sided and the curve slopes there.
  • Higher degree lets CV choose wider spans (0.15 → 0.30 → 0.70). Wider windows average more noise away without adding bias, so local quadratic wins in both regions on this curvy signal.
  • With a degree-0 smoother, CV keeps the span small to limit bias, and pays in variance.
🐍 Exercise 2 (research): degree, span and the boundary
Simulate battery-temperature traces over an orbit; fit local constant, linear and quadratic LOESS with spans chosen by closed-form leave-one-out CV; compare the MSE against the true curve in the interior and near the boundaries over 40 traces.

LOESS in AI, robotics and aerospace

  • Telemetry trends and detrending before anomaly detection.
  • Seasonal decomposition (STL) of periodic signals such as orbital thermal cycles.
  • Calibration drift curves and exploratory plots in test campaigns.
  • Spectral smoothing: Savitzky–Golay filters in spectroscopy and IMU data.

References

  1. W. S. Cleveland (1979). “Robust locally weighted regression and smoothing scatterplots.” JASA 74(368).
  2. W. S. Cleveland, S. J. Devlin (1988). “Locally weighted regression: an approach to regression analysis by local fitting.” JASA 83(403).
  3. J. Fan, I. Gijbels (1996). Local Polynomial Modelling and Its Applications. Chapman & Hall.
  4. A. Savitzky, M. J. E. Golay (1964). “Smoothing and differentiation of data by simplified least squares procedures.” Analytical Chemistry 36(8).
  5. R. B. Cleveland, W. S. Cleveland, J. E. McRae, I. Terpenning (1990). “STL: a seasonal-trend decomposition procedure based on loess.” Journal of Official Statistics 6(1).