TL;DR
Basis expansion describes a function as a weighted sum of simple building-block functions, called basis functions. The building blocks are chosen in advance, so the only thing left to learn is one weight per block. That turns the hard problem of finding a curve into the easier problem of finding a short list of numbers.
Builds on: Vector Space Basis, Linear Regression · Leads to: Radial Basis Function, Fourier Transform
Where it's used
Smoothing measurements. A sensor records a noisy value every second. An engineer fits a sum of polynomial blocks, , , , , to the readings. The few weights that come out describe the trend, and the leftover noise is what the curve does not capture.
JPEG image compression. A JPEG file cuts the picture into pixel blocks. Each block is written as a weighted sum of 64 fixed cosine patterns, the discrete cosine basis. Most weights are close to zero, so dropping them shrinks the file while the picture still looks right.
Time-series forecasting. The interpretable version of the N-BEATS forecasting model (N-BEATS) predicts the future as a sum of a polynomial trend basis and a seasonal basis built from Fourier waves. Each weight can be read as "how much trend" or "how much seasonal wave" the forecast contains.
Scattered-data interpolation. Geologists who have measured ground height at scattered points use radial basis functions, bumps centred on the measurement points, to estimate height everywhere in between.
How it works
Take three data points, , and . We want a function that passes through all three. Here is the process, one idea per step.
- Start from the goal. We need , and . Any curve through those points will do.
- Choose the building blocks. Pick three simple polynomial functions that we can evaluate anywhere: , and . Check: at they give , and .
- Write as a weighted sum. Let . The shape of each block is fixed. The unknowns are only the three numbers , , . This is the basis expansion.
- Turn each data point into one equation. At the blocks are , so . At they are , so . At they are , so . Check: three points give three equations for three unknowns.
- Solve the equations. From the second equation gives , and the third gives , which is . Subtracting the first of these from the second gives , so .
- Read off the answer. The weights are , so . Check: , , . All three points are hit.
- f(x) = 1 + x^2
The same pattern works for any list of building blocks. The process is always: choose blocks, write the weighted sum, make one equation per data point, solve.
flowchart LR A[Choose basis functions] --> B[Evaluate them at each data point] B --> C[Solve for the weights] C --> D[Prediction is the weighted sum]
The math
Given fixed functions (the basis), a basis expansion is a function of the form
The functions are chosen before fitting. The numbers (the weights) are what we fit to data. The sum runs over blocks, where is the number of blocks.
Step 3 in symbols
Each block is a known function of . The model is linear in the weights: doubling every doubles , and adding two expansions adds their weights. It is not linear in , because bends the curve. The phrase "linear model" refers to the weights, not to the curve.
Step 4 in symbols
For data points , build a matrix called the design matrix with one row per data point and one column per block:
Collect the weights into a vector and the targets into a vector . The predictions for all points at once are . For the example above,
Each row of is one of the equations from step 4.
Step 5 in symbols
When is square and invertible, the exact solution is . When there are more points than blocks, no exact solution usually exists. Least squares instead picks the weights that make the sum of squared errors as small as possible:
This is the same formula that solves Linear Regression, with the columns of playing the role of the input features.
Worked examples
Model: , with weights and . Find .
- The blocks are and .
- At the blocks are and .
- Multiply each block by its weight: and .
- Add: .
The answer is . The curve is the straight line through and .
Data: , , . Blocks: , , . Find the weights.
- Let .
- At the blocks are , so .
- At the blocks are , so . With this gives .
- At the blocks are , so . With this gives , which is .
- Subtract the equation from . This gives .
- Then .
- So . Check: and .
The weight is negative. That is fine: the blocks are allowed to cancel each other in the sum.
A made-up fitted model for the average temperature in degrees Celsius in a city is
where is the number of months after January. The basis is , and , and the weights are , and . Find for , and , then find for .
- At : and . So .
- At : the angle is . So and . Then .
- At : the angle is . So and . Then .
- Try it: at the angle is , so and . Find before reading on.
Answer: . The model is warmest near (April) and coldest near (October).
Common mistakes and tradeoffs
It is tempting to think a basis expansion is only for straight lines. In fact the curve can be as bent as the blocks allow. The model is linear in the weights, which is why least squares still works, and nonlinear in , which is why it can bend.
It is tempting to think more blocks always give a better fit. In fact, with as many blocks as data points, the curve can pass through every point and still swing wildly between them. The fit then describes noise, not the trend. This is Overfitting. Use fewer blocks than points, or check the fit on data you did not use.
It is tempting to think the blocks must be perpendicular to each other. In fact they need not be. Perpendicular blocks make the weights easier to compute and to interpret, which is why the cosine basis in JPEG and the Fourier basis are chosen that way, but a polynomial basis works fine even though its blocks overlap.
When to use it. Use a basis expansion when you know roughly what shape to expect: a trend that bends a few times, a repeating cycle, or a picture made of local patterns. The weights are then few, easy to read, and easy to fit.
When not to use it. Do not use it when the input has many dimensions, because the number of blocks grows very fast. Do not use it when the right shape is unknown and you cannot pick good blocks; a neural network learns its own building blocks from the data instead.
Alternatives. Splines use blocks that are only nonzero on short stretches, which avoids the swings of high-degree polynomials. Kernel methods use a block per data point. Neural networks learn the blocks during training.
History
In the early 1800s, Gauss and Legendre developed the method of least squares to fit astronomical observations, which made it possible to choose weights for any list of blocks. Polynomial interpolation was older still, going back to the work of Newton and Lagrange in the 1700s.
Joseph Fourier showed in the early 1800s that sums of sines and cosines could describe the flow of heat, which made the trigonometric basis famous. The idea that any reasonable function can be written as a sum of simple blocks grew out of that work.
Radial basis functions for scattered data were studied from the 1970s on. In the late 1980s, they were adopted as a way to build neural networks with one layer of bumps, which kept the idea of a weighted sum of blocks alive in machine learning.
Recap
- A basis is a fixed list of building-block functions whose shapes you choose in advance.
- A basis expansion writes as , so the only unknowns are the weights.
- Each data point gives one equation in the weights; with more points than blocks, least squares picks the best weights.
- The model is linear in the weights, which is why linear regression applies, even though the curve can bend.
Read next: Radial Basis Function for bumps centred on data points, and Fourier Transform for the trigonometric basis taken to its limit.
Check yourself
QuestionWhat does it mean that a basis expansion is linear in its weights?Show answer
Doubling every weight doubles the output, and the output for a sum of two sets of weights is the sum of the two outputs. The blocks are fixed known functions, so they do not change when the weights change. The curve itself may still bend, for example when one block is .
QuestionA model is , written as weights on and on . What is ?Show answer
The blocks at are and . Multiply: and . Add: . So .
QuestionWhy can a model with as many blocks as data points fit every point and still predict badly?Show answer
With blocks and points, the equations can be solved exactly, so the curve passes through every point. Between the points the curve is free to swing, and those swings are driven by the noise in the data rather than the trend. The model has learned the noise, which is overfitting. Using fewer blocks, or checking the error on new data, exposes the problem.
QuestionA monthly sales record has a rising trend and a yearly cycle. You want to fit it with a basis expansion. Which blocks would you pick, and how many weights would you expect to need?Show answer
A reasonable choice is the blocks and for the trend, plus and for the yearly cycle. That is four blocks, so four weights. The constant and linear blocks cover the trend, and the sine and cosine pair covers one yearly wave of any height and phase. Fewer blocks would miss the cycle; many more would start to fit the noise.