Search

Search papers and lessons

Basis Expansion

Level 2 · 11 min read

Basis expansion writes a function as a weighted sum of simple building-block functions, so fitting a curve becomes choosing a short list of numbers.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Probability
  • Calculus
  • Classical models
  • Losses & regularization
  • ReadReady to readNot yet

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, 11, tt, t2t^2, t3t^3, 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 8×88 \times 8 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, (0,1)(0, 1), (1,2)(1, 2) and (2,5)(2, 5). We want a function ff that passes through all three. Here is the process, one idea per step.

  1. Start from the goal. We need f(0)=1f(0) = 1, f(1)=2f(1) = 2 and f(2)=5f(2) = 5. Any curve through those points will do.
  2. Choose the building blocks. Pick three simple polynomial functions that we can evaluate anywhere: φ1(x)=1\varphi_1(x) = 1, φ2(x)=x\varphi_2(x) = x and φ3(x)=x2\varphi_3(x) = x^2. Check: at x=2x = 2 they give 11, 22 and 44.
  3. Write ff as a weighted sum. Let f(x)=w1φ1(x)+w2φ2(x)+w3φ3(x)f(x) = w_1 \varphi_1(x) + w_2 \varphi_2(x) + w_3 \varphi_3(x). The shape of each block is fixed. The unknowns are only the three numbers w1w_1, w2w_2, w3w_3. This is the basis expansion.
  4. Turn each data point into one equation. At x=0x = 0 the blocks are 1,0,01, 0, 0, so w1=1w_1 = 1. At x=1x = 1 they are 1,1,11, 1, 1, so w1+w2+w3=2w_1 + w_2 + w_3 = 2. At x=2x = 2 they are 1,2,41, 2, 4, so w1+2w2+4w3=5w_1 + 2w_2 + 4w_3 = 5. Check: three points give three equations for three unknowns.
  5. Solve the equations. From w1=1w_1 = 1 the second equation gives w2+w3=1w_2 + w_3 = 1, and the third gives 2w2+4w3=42w_2 + 4w_3 = 4, which is w2+2w3=2w_2 + 2w_3 = 2. Subtracting the first of these from the second gives w3=1w_3 = 1, so w2=0w_2 = 0.
  6. Read off the answer. The weights are (1,0,1)(1, 0, 1), so f(x)=1+x2f(x) = 1 + x^2. Check: f(0)=1f(0) = 1, f(1)=2f(1) = 2, f(2)=5f(2) = 5. All three points are hit.
−0.500.511.522.50123456xf(x)
  • f(x) = 1 + x^2
The three data points lie exactly on the curve built from the weights (1, 0, 1).

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

Definition: Basis expansion

Given fixed functions φ1,φ2,…,φK\varphi_1, \varphi_2, \dots, \varphi_K (the basis), a basis expansion is a function of the form

f(x)=∑k=1Kwk φk(x).f(x) = \sum_{k=1}^{K} w_k \, \varphi_k(x).

The functions are chosen before fitting. The numbers w1,…,wKw_1, \dots, w_K (the weights) are what we fit to data. The sum runs over KK blocks, where KK is the number of blocks.

Step 3 in symbols

Each block φk\varphi_k is a known function of xx. The model is linear in the weights: doubling every wkw_k doubles ff, and adding two expansions adds their weights. It is not linear in xx, because φ3(x)=x2\varphi_3(x) = x^2 bends the curve. The phrase "linear model" refers to the weights, not to the curve.

Step 4 in symbols

For NN data points (x1,y1),…,(xN,yN)(x_1, y_1), \dots, (x_N, y_N), build a matrix called the design matrix Φ\Phi with one row per data point and one column per block:

Φik=φk(xi).\Phi_{ik} = \varphi_k(x_i).

Collect the weights into a vector w\mathbf{w} and the targets into a vector y\mathbf{y}. The predictions for all points at once are Φw\Phi \mathbf{w}. For the example above,

Φ=(100111124),w=(101),Φw=(125).\Phi = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 1 & 1 \\ 1 & 2 & 4 \end{pmatrix}, \quad \mathbf{w} = \begin{pmatrix} 1 \\ 0 \\ 1 \end{pmatrix}, \quad \Phi \mathbf{w} = \begin{pmatrix} 1 \\ 2 \\ 5 \end{pmatrix}.

Each row of Φw\Phi\mathbf{w} is one of the equations from step 4.

Step 5 in symbols

When Φ\Phi is square and invertible, the exact solution is w=Φ−1y\mathbf{w} = \Phi^{-1}\mathbf{y}. 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:

w=(Φ⊤Φ)−1Φ⊤y.\mathbf{w} = (\Phi^{\top}\Phi)^{-1}\Phi^{\top}\mathbf{y}.

This is the same formula that solves Linear Regression, with the columns of Φ\Phi playing the role of the input features.

Worked examples

Example
1. A two-block line

Model: f(x)=w1⋅1+w2⋅xf(x) = w_1 \cdot 1 + w_2 \cdot x, with weights w1=2w_1 = 2 and w2=3w_2 = 3. Find f(2)f(2).

  1. The blocks are φ1(x)=1\varphi_1(x) = 1 and φ2(x)=x\varphi_2(x) = x.
  2. At x=2x = 2 the blocks are φ1(2)=1\varphi_1(2) = 1 and φ2(2)=2\varphi_2(2) = 2.
  3. Multiply each block by its weight: 2⋅1=22 \cdot 1 = 2 and 3⋅2=63 \cdot 2 = 6.
  4. Add: f(2)=2+6=8f(2) = 2 + 6 = 8.

The answer is 88. The curve f(x)=2+3xf(x) = 2 + 3x is the straight line through (0,2)(0, 2) and (2,8)(2, 8).

Example
2. Three points, three blocks

Data: (0,2)(0, 2), (1,3)(1, 3), (2,10)(2, 10). Blocks: 11, xx, x2x^2. Find the weights.

  1. Let f(x)=w1+w2x+w3x2f(x) = w_1 + w_2 x + w_3 x^2.
  2. At x=0x = 0 the blocks are 1,0,01, 0, 0, so w1=2w_1 = 2.
  3. At x=1x = 1 the blocks are 1,1,11, 1, 1, so w1+w2+w3=3w_1 + w_2 + w_3 = 3. With w1=2w_1 = 2 this gives w2+w3=1w_2 + w_3 = 1.
  4. At x=2x = 2 the blocks are 1,2,41, 2, 4, so w1+2w2+4w3=10w_1 + 2w_2 + 4w_3 = 10. With w1=2w_1 = 2 this gives 2w2+4w3=82w_2 + 4w_3 = 8, which is w2+2w3=4w_2 + 2w_3 = 4.
  5. Subtract the equation w2+w3=1w_2 + w_3 = 1 from w2+2w3=4w_2 + 2w_3 = 4. This gives w3=3w_3 = 3.
  6. Then w2=1−3=−2w_2 = 1 - 3 = -2.
  7. So f(x)=2−2x+3x2f(x) = 2 - 2x + 3x^2. Check: f(1)=2−2+3=3f(1) = 2 - 2 + 3 = 3 and f(2)=2−4+12=10f(2) = 2 - 4 + 12 = 10.

The weight w2w_2 is negative. That is fine: the blocks are allowed to cancel each other in the sum.

Example
3. A seasonal model for a yearly cycle

A made-up fitted model for the average temperature in degrees Celsius in a city is

y(t)=10+4sin⁡(2πt12)+1cos⁡(2πt12),y(t) = 10 + 4\sin\left(\frac{2\pi t}{12}\right) + 1\cos\left(\frac{2\pi t}{12}\right),

where tt is the number of months after January. The basis is 11, sin⁡(2πt/12)\sin(2\pi t / 12) and cos⁡(2πt/12)\cos(2\pi t / 12), and the weights are 1010, 44 and 11. Find yy for t=0t = 0, t=3t = 3 and t=6t = 6, then find yy for t=9t = 9.

  1. At t=0t = 0: sin⁡0=0\sin 0 = 0 and cos⁡0=1\cos 0 = 1. So y=10+0+1=11y = 10 + 0 + 1 = 11.
  2. At t=3t = 3: the angle is 2π⋅3/12=π/22\pi \cdot 3 / 12 = \pi / 2. So sin⁡=1\sin = 1 and cos⁡=0\cos = 0. Then y=10+4+0=14y = 10 + 4 + 0 = 14.
  3. At t=6t = 6: the angle is π\pi. So sin⁡=0\sin = 0 and cos⁡=−1\cos = -1. Then y=10+0−1=9y = 10 + 0 - 1 = 9.
  4. Try it: at t=9t = 9 the angle is 3π/23\pi / 2, so sin⁡=−1\sin = -1 and cos⁡=0\cos = 0. Find yy before reading on.

Answer: y(9)=10−4+0=6y(9) = 10 - 4 + 0 = 6. The model is warmest near t=3t = 3 (April) and coldest near t=9t = 9 (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 xx, 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 f(x)f(x) as ∑kwkφk(x)\sum_k w_k \varphi_k(x), 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 φk(x)\varphi_k(x) 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 x2x^2.

QuestionA model is f(x)=3+xf(x) = 3 + x, written as weights w1=3w_1 = 3 on 11 and w2=1w_2 = 1 on xx. What is f(4)f(4)?Show answer

The blocks at x=4x = 4 are 11 and 44. Multiply: 3⋅1=33 \cdot 1 = 3 and 1⋅4=41 \cdot 4 = 4. Add: 3+4=73 + 4 = 7. So f(4)=7f(4) = 7.

QuestionWhy can a model with as many blocks as data points fit every point and still predict badly?Show answer

With NN blocks and NN 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 11 and tt for the trend, plus sin⁡(2πt/12)\sin(2\pi t / 12) and cos⁡(2πt/12)\cos(2\pi t / 12) 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.

Sources