Basis Function Regression
Basis function regression fits a curve by running linear regression on fixed transformations of the input. It picks functions of , called basis functions, adds the constant , and models
where is the intercept and is noise with mean zero. The model can be nonlinear in , but it is linear in . Evaluating each basis function at each input gives the feature matrix
whose row holds the features of sample . Least squares with in place of the design matrix gives
which needs the columns of to be linearly independent. Everything in multiple linear regression carries over. This includes the hat matrix, the -tests and the residual variance with degrees of freedom.Four bases are common for a scalar input :
- Polynomial: for , which fits a polynomial of degree .
- Piecewise linear: the linear term plus one ramp for each knot . Each ramp, also called a hinge function, bends the fitted line at its knot. The ReLU here is the same function that neural networks use.
- Gaussian radial basis function (RBF): , a bump with center and width .
- Fourier: the pair and for each known frequency , which fits a periodic signal of time . This is the sinusoidal model of time series regression.
The number of basis functions sets the flexibility of the curve. Adding a basis function never raises the training error, so is chosen on validation data or by cross-validation. Too few functions underfit and too many overfit, as described in 2.3. Generalization. When the knots, centers or frequencies are learned as well, the model is no longer linear in its parameters. It then becomes a nonlinear regression or a neural network.
Not Taught in Lecture
Neither CS 182 nor STAT 153 covered the polynomial or Gaussian RBF bases. These bases follow Bishop, Pattern Recognition and Machine Learning (2006), §3.1, and ESL (Hastie, Tibshirani and Friedman), chapter 5. The same sources cover the growth of polynomial bases with input dimension. Runge’s phenomenon is from Runge (1901).
Intuition
A straight line cannot follow a curve. Least squares, however, works with any columns in the design matrix, whatever function produced them. Basis function regression keeps the least squares machinery and changes the inputs. Each basis function turns into a new column, such as or a ramp that starts at . OLS then picks the weight of each column. The name comes from linear algebra. The fitted curve is a linear combination of the chosen functions, so it always lies in the space they span.
Each basis allows a different family of curve shapes. A polynomial is smooth everywhere, and moving one data point changes the whole curve. A ramp changes the slope only to the right of its knot. A Gaussian bump changes the curve only near its center. Sines and cosines repeat, so they suit data that cycle with a known period. A good basis needs few functions for the shapes the data show, which keeps the variance of the fit low.
Properties
(Slope Changes at Knots) In the piecewise-linear basis, each ramp coefficient is the change in slope at its knot.
Take a continuous piecewise-linear function with knots . Its slope is left of the first knot and right of knot . It can be written as
where is the line before the first knot. Each ramp is zero left of its knot, so it adds its slope change only from the knot onward. OLS on this basis therefore estimates an intercept, a starting slope and slope changes. Since the linear term equals , the whole model is a one-hidden-layer ReLU network with fixed first-layer weights. With enough knots placed close together, this basis approximates any continuous function on a closed interval (CS 182 lecture 1).
(Fixed Versus Learned Basis) Fixed knots keep the fit a convex least squares problem, and learned knots turn the model into a neural network.
A hidden ReLU unit with has its elbow at . If only the output weights are trained, the hidden units form a fixed basis. The fit is then OLS or ridge regression on , solved in closed form. Training every layer by gradient descent also moves the elbows toward the inputs where the target bends. This is feature learning in its simplest form. With the same number of units, learned elbows can beat randomly placed fixed elbows (CS 182 homework 1). The loss is then no longer a convex quadratic in the parameters. Training is slower and can stop at a local minimum.
(Polynomial Instability) High-degree polynomial bases are badly conditioned and oscillate strongly near the ends of the data.
On the columns and are nearly parallel, so is close to singular. Take equally spaced inputs on . The condition number of is then about at degree and at degree . Forming squares it to about . Solving the normal equations in double precision can then lose all but one digit. Rescaling to lowers the degree-10 value to about , and orthogonal polynomials lower it further. The second problem remains after any rescaling. Interpolating at equally spaced points on shows it. The maximum error is at degree and at degree . The worst errors sit near the ends of the interval. This is Runge’s phenomenon. Piecewise bases such as ramps avoid it, because each piece is a low-degree polynomial.
(Choosing the Number of Basis Functions) Training error alone always favors more basis functions, so the count is chosen on held-out data.
Adding a column to can only enlarge its column space, so the training residual sum of squares cannot rise. With independent columns, is square and invertible, and the fit passes through every point. A polynomial of degree through distinct inputs is the standard case. Test error usually falls at first and then rises, which is the tradeoff in 2.3.1. Bias-Variance Decomposition. There are two practical methods. The first compares a few values of , or a few knot sets, by validation error or cross-validation. The second keeps a large basis and adds a ridge penalty, with chosen by cross-validation.
(Orthogonal Fourier Columns) At the Fourier frequencies , the sine and cosine columns are orthogonal, so each coefficient is a single sum.
Take times and frequencies with . The columns and are then orthogonal to each other and to the constant column. They are also orthogonal to every other such pair. Each has squared length . The matrix is diagonal, and OLS reduces to
where and are the cosine and sine coefficients at . Adding or dropping a frequency leaves the other coefficients unchanged. The sum is the scaled periodogram at (STAT 153 lecture 12). A frequency between the Fourier frequencies breaks the orthogonality. An unknown frequency makes the model nonlinear.
(Growth with Input Dimension) A full polynomial basis of degree in inputs has functions.
With a vector input , a polynomial basis contains every product of inputs up to total degree . Examples are and . Counting these monomials, including the constant, gives . Ten inputs at degree need columns, and inputs need . OLS needs at least as many samples as columns. A grid of bumps or knots grows faster, since centers per input give functions. This is one form of the curse of dimensionality. Neural networks avoid a fixed grid by learning a small number of features from the data.
Examples
(Easy) Income Tax as a Sum of Ramps
A tax code charges on the first $10k of income, on income from $10k to $40k, and above $40k. Income is in thousands of dollars.
Write the tax owed in the piecewise-linear basis. What is the tax on $55k? and . Its slope starts at , rises by at the first knot and rises by at the second. So At this gives , or $12,250. The bracket calculation agrees, since . Suppose an analyst has tax bills for at least four distinct incomes, with every bracket represented. OLS on the columns , , and then returns , , and exactly. Each ramp coefficient is a change in the marginal tax rate.
The tax is piecewise linear with knots at
(Medium) Seasonal Sales with a Fourier Basis
A toy store records quarterly sales, in thousands of units, for two years: . The pattern repeats every four quarters, so the frequency is cycle per quarter. The model is for .
Fit the model without inverting a matrix. How large is the seasonal swing, and which quarter peaks? , the frequency is a Fourier frequency. The cosine column is , and the sine column is . They are orthogonal to each other and to the constant, and each has squared length . So , and The fitted values for quarters 1 to 4 are , , and . They repeat in the second year. Every residual is , so the residual sum of squares is . The amplitude is thousand units around the mean of . Sales peak in the fourth quarter, which matches holiday shopping. A general OLS solver returns the same three coefficients.
With
(Hard) A Knot at Every Data Point
A sensor gives readings at inputs . An analyst builds a piecewise-linear basis with a knot at every input. The columns are , and for .
Which columns are redundant? What does OLS return, and how does it compare with a straight line? has columns but only rows, so its rank is at most . The column is zero at every input. The column equals on the data, a combination of and . Dropping both leaves independent columns, so becomes square and invertible. The fit passes through every point: The slope alternates between and , so the slope changes of follow the noise. The training error is zero. The OLS line is , with residual sum of squares . Leave-one-out cross-validation refits each model with knots at the remaining inputs. It drops the redundant ramps at the first and last remaining knots, as above. It gives a mean squared error of for the interpolating curve and for the line. The line generalizes better. A ridge penalty on the ramp coefficients would give a fit between these two extremes.
The matrix
See Also
- 3.1a. Multiple Linear Regression, the OLS machinery that the feature matrix reuses.
- 3.3. Nonlinear Regression, for models whose knots, centers or frequencies are unknown.
- 3.2.1. Ridge Regression, the usual penalty when the basis is large.
- 2.4 Time Series Regression, where Fourier bases and polynomial trends model time series.
- 3. Activation Functions, for the ReLU and other functions used as learned features.
- 3.10.1 Kernels, which work with infinitely many basis functions through inner products.