Splines & Additive Models
Smoothing splines, GAMs and interpretable nonlinearity.
Among all functions with a square-integrable second derivative, which one best trades closeness to the data against roughness? The question ranges over an infinite-dimensional space, yet the answer is a natural cubic spline with a knot at every data point. This lesson builds the B-spline machinery, proves that theorem, turns smoothing into a generalized ridge regression with an effective number of parameters, and scales up to additive models and the explainable boosting machines built on them.
What you’ll learn
- Build regression splines in the B-spline basis with the Cox–de Boor recursion, and explain their local support, partition of unity and banded systems.
- Prove that the minimizer of is a natural cubic spline with knots at the data, and analyze the fit as a linear smoother with effective degrees of freedom .
- Choose by generalized cross-validation or REML, and state the optimal rate and the equivalent-kernel view of smoothing splines.
- Fit additive models by backfitting, explain why they escape the curse of dimensionality, and assess GA²M models and explainable boosting machines, including what they cannot tell you.
Regression splines and B-splines
A regression spline is least squares on a spline basis with knots fixed in advance, for example at quantiles of , so that each piece sees about the same amount of data. The truncated power basis of Basis Functions & Generalized Linear Models spans the right space but is numerically poor: each function is nonzero from its knot to the end of the range, and functions with nearby knots are nearly collinear. The B-spline basis spans exactly the same space with functions that are local, nonnegative and well conditioned.
Start from indicators of the knot intervals (degree 0):
Raise the degree by blending two neighbors with linear weights that rise across one's support and fall across the other's:
By induction, is a piecewise polynomial of degree , nonnegative, zero outside , that is on all but consecutive intervals, and times continuously differentiable at simple knots. Away from the ends of the knot sequence the functions sum to one:
Local support has two payoffs. Each row of the design matrix has at most nonzero entries (four for cubics), so is banded: fitting costs to form it and to solve, and moving one data point changes the fit only nearby. And the coefficients act like control points of the curve, which makes penalties on their differences natural: the P-splines of Eilers & Marx (1996) use many equally spaced B-splines and penalize differences between neighboring coefficients.
Near the boundary, the polynomial pieces are fitted from one side only and their variance balloons. Natural splines force linearity beyond the outermost knots, trading a little bias at the edges for much less variance there.
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import SplineTransformer
from sklearn.linear_model import RidgeCV
# 19 cubic B-splines (17 knots at quantiles of x), each row summing to 1;
# a ridge penalty on their coefficients, chosen by efficient leave-one-out CV
spline = make_pipeline(
SplineTransformer(n_knots=17, degree=3, knots="quantile"),
RidgeCV(alphas=np.logspace(-4, 2, 30)),
)
spline.fit(x.reshape(-1, 1), y)A penalized regression spline in a few lines. The ridge penalty here shrinks the coefficients toward zero; P-splines and smoothing splines penalize roughness instead, which leaves straight lines unpenalized.
Smoothing splines: the variational theorem
Take any admissible , and let be the natural cubic spline that interpolates at the knots (it exists and is unique). The two functions agree at the data, so they have the same fit term. It suffices to show , with equality only when . Put , which vanishes at every knot, and expand:
Integrate the middle term by parts. Since is linear outside , vanishes there and at and , so the boundary term disappears:
Between consecutive knots is a cubic, so is a constant on , and vanishes at both ends of each interval:
So . Equality forces : is linear and vanishes at points, so . Replacing any candidate by its interpolating natural spline never increases the objective, and the minimization reduces to the -dimensional space of natural cubic splines, where the objective is strictly convex.
In a basis of natural splines with these knots, , the problem is a generalized ridge regression with penalty matrix :
is symmetric with eigen-decomposition , where . The two zero eigenvalues belong to the straight lines, which the penalty does not see; the eigenvectors oscillate faster as grows, so the smoother is a low-pass filter. The effective degrees of freedom
fall from (interpolation, ) to (the least-squares line, ). Reinsch's algorithm exploits banded structure to compute the fit in operations.
What does the smoothing spline converge to as ?
- The constant function
- The least-squares straight line
- The zero function
- The natural cubic spline that interpolates the data
Show the answer
B. The least-squares straight line
The penalty is zero exactly for straight lines, so an infinitely heavy penalty restricts to lines, and among lines the fit term picks least squares. That is why , not or .
Choosing λ, rates and equivalent kernels
Choosing . Smoothing splines are linear smoothers, so leave-one-out costs one fit, and generalized cross-validation, , was in fact introduced for splines (Craven & Wahba, 1978); Ridge Regression in Depth derives both. A popular alternative treats the curvy part of as a random effect and maximizes the restricted likelihood (REML), the marginal likelihood of the Bayesian view; it is often preferred in practice because the GCV criterion can be flat or have several minima and occasionally undersmooths badly.
How fast can any method learn a curve? Stone (1982) showed that if the regression function has bounded derivatives in dimensions, the best achievable rate for the mean squared error is . Cubic smoothing splines (, ) attain when shrinks like (for the objective normalized by ), slower than the parametric but with no shape assumed beyond smoothness. The exponent collapses as grows: the curse of dimensionality in its sharpest form.
With the optimal rate and , halving the mean squared error in one dimension takes times more data. How many times more data does it take in dimensions?
Show the answer
11.3
The error scales as , so halving it needs multiplied by . Every further halving costs another factor 11.3: a hundredfold error reduction would need about times the data.
The equivalent kernel. Silverman (1984) showed that for large a smoothing spline behaves like a kernel smoother with a variable bandwidth: the fit at is approximately a kernel-weighted average of the nearby , with kernel and a bandwidth that varies with , where
and is the density of the inputs. integrates to one, has small negative side lobes, and its Fourier transform is exactly , the frequency response of the penalty . The spline adapts its window to the data automatically, narrower where inputs are dense, though only through the fourth root of the density.
Additive models and backfitting
A generalized additive model (Hastie & Tibshirani, 1986, 1990) keeps the curse at bay by assuming no interactions:
where each is a smooth function, usually a penalized spline, and the centering makes the decomposition identifiable. If the truth is additive, each can be estimated at the one-dimensional rate, and so can the whole model: Stone (1985) proved the rate whatever the dimension .
BACKFIT(y, x_1..x_d, smoothers S_1..S_d) # identity link, squared error
alpha <- mean(y); f_j <- 0 for every j
repeat until no f_j changes:
for j = 1..d:
r <- y - alpha - sum over k != j of f_k # partial residual
f_j <- S_j(r against x_j) # smooth it in one variable
f_j <- f_j - mean(f_j) # keep the components centered
return alpha, f_1..f_dBackfitting is block coordinate descent on the penalized least-squares problem, the same idea as the lasso's coordinate descent with one-dimensional smoothers in place of soft thresholding. For a non-Gaussian response, local scoring wraps it in the IRLS loop of Generalized Linear Models: compute working responses and weights, backfit, repeat.
Buja, Hastie & Tibshirani (1989) studied when backfitting converges: for symmetric smoothers with eigenvalues in , such as smoothing splines, it converges to a solution of the penalized problem. They also named the main failure mode, concurvity, the nonlinear analogue of collinearity: when some combination of smooth functions of different inputs is nearly zero, the components are poorly determined even though their sum is fine. Modern software, such as the R package described by Wood (2017), fits all components jointly as penalized regression splines and estimates every smoothing parameter by GCV or REML.
GA²M and explainable boosting machines. Lou, Caruana & Gehrke (2012) compared ways of fitting the shape functions and found that gradient boosting with bagged shallow trees gave the most accurate additive models on low- to medium-dimensional data sets. Adding a few pairwise terms chosen by a fast interaction search gives GA²M (Lou, Caruana, Gehrke & Hooker, 2013). The open-source explainable boosting machine (Nori, Jenkins, Koch & Caruana, 2019) implements it: boosting cycles through the features one at a time with a very small learning rate, bagging stabilizes the shapes, and pairwise interactions are detected automatically. Each term is a curve or a heat map that a domain expert can read and, if necessary, edit.
Key takeaways
- B-splines span the same space as truncated powers but are local (a cubic lives on 4 intervals), nonnegative and a partition of unity, giving banded, well-conditioned least squares.
- The minimizer of is a natural cubic spline with knots at the data; the fit is with running from down to 2.
- Choose by GCV or REML. With derivatives the optimal rate is , for cubic splines in one dimension; the spline is asymptotically a kernel smoother with Fourier response and bandwidth .
- Additive models attain the one-dimensional rate in any dimension and are fitted by backfitting or joint penalized splines; concurvity is their collinearity.
- GA²M and explainable boosting machines add a few pairwise terms and make every term inspectable, which exposes problems like the asthma paradox but does not make the terms causal.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A smoothing spline is fitted to points with distinct values. What range do its effective degrees of freedom cover as goes from to ?
- From 50 down to 0
- From 50 down to 1
- From 50 down to 2
- They stay at 50; only the coefficients shrink
Show the answer
C. From 50 down to 2
with . At every term is 1 (interpolation, 50 degrees of freedom); as only the two unpenalized directions, intercept and slope, survive.
On how many consecutive knot intervals is a single cubic B-spline basis function nonzero?
- 1
- 2
- 4
- All of them
Show the answer
C. 4
A degree- B-spline lives on consecutive intervals, so a cubic lives on four. That local support makes the design matrix sparse and banded.
Why can an additive model in inputs be estimated far more accurately from examples than a general smooth function of 20 inputs?
- Because additive models are linear in the inputs
- Because backfitting is faster than other algorithms
- Because it never overfits
- Because each component is a function of one input, so if the truth is additive the error decreases at the one-dimensional rate
Show the answer
D. Because each component is a function of one input, so if the truth is additive the error decreases at the one-dimensional rate
Stone (1985) showed that additive structure removes from the exponent: the rate is the one-dimensional for , against for a general function of 20 inputs. The price is the assumption itself: interactions it leaves out are lost.
A GA²M for pneumonia mortality shows a lower risk for patients with asthma. The model will be used to decide who can be treated at home. What is the right response?
- Trust it: the model is interpretable, so its terms are causal effects
- Recognize a treatment-driven pattern (asthmatics received aggressive care), and correct or constrain the term before deployment
- Remove the asthma feature and keep everything else unchanged, which fully solves the problem
- Switch to a black-box model with the same accuracy, which will not have this problem
Show the answer
B. Recognize a treatment-driven pattern (asthmatics received aggressive care), and correct or constrain the term before deployment
The pattern is real in the data but reflects how asthmatic patients were treated, so acting on it would send high-risk patients home. An intelligible model makes the problem visible; fixing it takes domain knowledge, for instance editing that term.
Further reading
- C. H. Reinsch, "Smoothing by spline functions," Numerische Mathematik 10, 177–183 (1967).
- G. Wahba, Spline Models for Observational Data (SIAM, 1990).
- T. Hastie & R. Tibshirani, Generalized Additive Models (Chapman & Hall, 1990).
- Y. Lou, R. Caruana & J. Gehrke, "Intelligible models for classification and regression," Proceedings of the 18th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (2012).
- T. Hastie, R. Tibshirani & J. Friedman, The Elements of Statistical Learning, 2nd ed. (Springer, 2009), chapters 5 and 9.