Tutorials › Regression Analysis (Undergraduate) › Constructing Natural Cubic Splines

Piecewise Regression and Splines · Tutorial 459 of 991

Constructing Natural Cubic Splines

See how boundary constraints turn the truncated-power representation of a cubic spline into a natural spline basis with two fewer free coefficients.

Advanced 9 min read

What You'll Learn

  • Define a natural cubic spline on a specified boundary interval
  • Count the free coefficients before and after imposing natural constraints
  • Derive a reduced basis from the truncated-power representation
  • Verify the zero-second-derivative constraints at both boundary knots
  • Distinguish boundary-curvature constraints from constraints on fitted values

Why impose natural boundary constraints?

A cubic regression spline can bend flexibly between its knots. But a cubic polynomial continued toward the edge of the data range can behave in ways that are driven more by its mathematical form than by the observations. A natural cubic spline addresses the boundary by requiring zero second derivative at each boundary knot. This tutorial develops that definition and shows exactly how the two constraints reduce the number of free basis functions.

Let \(a\) and \(b\) be the boundary knots, with \(a<b\), and let \(\xi_1,\ldots,\xi_K\) be distinct interior knots satisfying \(a<\xi_1<\cdots<\xi_K<b\). We work with cubic splines that are twice continuously differentiable at the interior knots, as in the earlier material on cubic regression splines and knot continuity.

Definition: A natural cubic spline on the boundary interval \([a,b]\) is a twice continuously differentiable piecewise-cubic function \(f(x)\) with the specified interior knots and with \(f''(a)=0\) and \(f''(b)=0\). These are constraints on the second derivative at the two boundary knots; they are not constraints that the function itself equal zero there.

For regression, the mean response is represented as \(E(Y_i\mid x_i)=f(x_i)\). The spline construction concerns the form of \(f\), rather than a particular error distribution. When fitting by ordinary least squares, the usual regression model writes \(Y_i=f(x_i)+\varepsilon_i\); for classical standard errors, one commonly assumes independent errors with mean zero and constant variance, and normal errors when exact small-sample \(t\)- or \(F\)-inference is required. None of those error assumptions changes the boundary constraints or the basis derivation.

Starting from the cubic truncated-power basis

With \(K\) interior knots, a general cubic spline can be written in the truncated-power basis. Define \((u)_+=\max(u,0)\), so that \((x-\xi_j)_+^3\) is zero to the left of knot \(\xi_j\) and equals \((x-\xi_j)^3\) to its right. The general representation is

$$ f(x)=\beta_0+\beta_1x+\beta_2x^2+\beta_3x^3+ \sum_{j=1}^{K}\gamma_j(x-\xi_j)_+^3. $$

There are \(K+4\) coefficients: four polynomial coefficients and one coefficient for each interior knot. Its second derivative is

$$ f''(x)=2\beta_2+6\beta_3x+ 6\sum_{j=1}^{K}\gamma_j(x-\xi_j)_+. $$

At the lower boundary \(a\), every interior-knot term is zero because \(a<\xi_j\). At the upper boundary \(b\), each truncated linear term is \(b-\xi_j\). Therefore the natural constraints become two linear equations in the coefficients:

$$ \begin{aligned} f''(a)=0 &\quad\Longrightarrow\quad 2\beta_2+6a\beta_3=0,\\ f''(b)=0 &\quad\Longrightarrow\quad 2\beta_2+6b\beta_3+ 6\sum_{j=1}^{K}\gamma_j(b-\xi_j)=0. \end{aligned} $$

How the two constraints reduce the basis

The first equation gives \(\beta_2=-3a\beta_3\). Substituting this into the second equation and solving for \(\beta_3\) gives

$$ \begin{aligned} -6a\beta_3+6b\beta_3+ 6\sum_{j=1}^{K}\gamma_j(b-\xi_j)&=0,\\ 6(b-a)\beta_3&=-6\sum_{j=1}^{K}\gamma_j(b-\xi_j),\\ \beta_3&=-\sum_{j=1}^{K}\gamma_j\frac{b-\xi_j}{b-a}. \end{aligned} $$

Because \(b-a\ne 0\), these equations determine \(\beta_2\) and \(\beta_3\) from the knot coefficients. The coefficients \(\beta_0,\beta_1\) and \(\gamma_1,\ldots,\gamma_K\) remain free. Thus the natural spline has \(K+2\) free coefficients, two fewer than the unrestricted cubic spline.

Substituting the solved coefficient into the original function gives a reduced basis representation. For each interior knot, define

$$ g_j(x)=(x-\xi_j)_+^3- \frac{b-\xi_j}{b-a}(x^3-3ax^2), \qquad j=1,\ldots,K. $$

Then every natural cubic spline with these knots can be written as

$$ f(x)=\beta_0+\beta_1x+\sum_{j=1}^{K}\gamma_jg_j(x). $$

The reduced set of basis functions is therefore \(1,x,g_1(x),\ldots,g_K(x)\). The two ordinary polynomial columns \(x^2\) and \(x^3\) no longer appear as independent columns: their coefficients have been determined by the boundary constraints. The resulting basis has \(K+2\) columns, matching the coefficient count.

Key result: With \(K\) distinct interior knots and two boundary knots \(a<b\), the unrestricted cubic spline has \(K+4\) free coefficients. Requiring \(f''(a)=f''(b)=0\) imposes two independent linear constraints, leaving \(K+2\) free coefficients and the reduced basis \(1,x,g_1(x),\ldots,g_K(x)\).

Worked constructions

Worked Example: Counting the Natural Spline Coefficients

Suppose a cubic spline has four distinct interior knots between its boundary knots. How many coefficients does the unrestricted spline have, and how many remain after imposing the natural constraints?

The truncated-power representation contains the four polynomial terms \(1,x,x^2,x^3\) and four truncated-power terms, one for each interior knot. Thus it has \(4+4=8\) free coefficients.

The natural conditions \(f''(a)=0\) and \(f''(b)=0\) are two independent linear constraints. They reduce the number of free coefficients by two:

$$ 8-2=6. $$

Equivalently, with \(K=4\), the reduced basis has \(K+2=6\) functions: an intercept, \(x\), and four natural-spline basis functions. This count is about the function space; an observed data set must also contain enough distinct predictor values for all six coefficients to be estimable.

Worked Example: Deriving a Basis with Two Interior Knots

Let the boundary knots be \(a=0\) and \(b=10\), and let the interior knots be \(\xi_1=3\) and \(\xi_2=7\). Begin with

$$ f(x)=\beta_0+\beta_1x+\beta_2x^2+\beta_3x^3+ \gamma_1(x-3)_+^3+\gamma_2(x-7)_+^3. $$

The second derivative at \(x=0\) is \(2\beta_2\), since both truncated terms are zero there. The first natural constraint therefore gives \(\beta_2=0\). At \(x=10\), the second derivative is

$$ 2\beta_2+60\beta_3+6(7\gamma_1+3\gamma_2)=0. $$

Using \(\beta_2=0\), this becomes \(60\beta_3+42\gamma_1+18\gamma_2=0\), so

$$ \beta_3=-0.7\gamma_1-0.3\gamma_2. $$

The reduced basis functions are consequently

$$ g_1(x)=(x-3)_+^3-0.7x^3, \qquad g_2(x)=(x-7)_+^3-0.3x^3. $$

To verify the boundary constraints directly, \(g_1''(0)=0\) and \(g_2''(0)=0\), because the truncated terms and the second derivative of \(x^3\) are both zero at 0. At 10, the second derivatives are

$$ g_1''(10)=6(10-3)-0.7(6\cdot10)=42-42=0, \qquad g_2''(10)=6(10-7)-0.3(6\cdot10)=18-18=0. $$

Thus \(f(x)=\beta_0+\beta_1x+\gamma_1g_1(x)+\gamma_2g_2(x)\) satisfies both natural constraints for any choices of its four free coefficients.

Worked Example: Evaluating a Natural Spline Basis Function

Use the boundaries and knots from the previous example, and choose \(\beta_0=2\), \(\beta_1=0.5\), \(\gamma_1=0.02\), and \(\gamma_2=-0.01\). The resulting function is

$$ f(x)=2+0.5x+0.02g_1(x)-0.01g_2(x). $$

At \(x=5\), the first truncated term is \((5-3)^3=8\), and the second is zero because \(5<7\). Hence

$$ g_1(5)=8-0.7(125)=-79.5, \qquad g_2(5)=0-0.3(125)=-37.5. $$

Substitution gives the fitted function value

$$ f(5)=2+0.5(5)+0.02(-79.5)-0.01(-37.5) =4.5-1.59+0.375=3.285. $$

The same coefficients in the unrestricted representation would be \(\beta_2=0\) and \(\beta_3=-0.7(0.02)-0.3(-0.01)=-0.011\). Checking at the upper boundary gives

$$ f''(10)=60(-0.011)+6\{0.02(7)-0.01(3)\} =-0.66+0.66=0. $$

At the lower boundary, \(f''(0)=2\beta_2=0\). Both constraints hold, and \(3.285\) is the function value at \(x=5\), in the response units used to define the coefficients.

Using the basis in regression

Once the knots and boundary interval have been chosen, the reduced basis can be evaluated at each observed predictor value. The regression design matrix has one column for the constant function, one for \(x\), and one for each \(g_j(x)\). Ordinary least squares estimates the \(K+2\) coefficients in that representation, provided the design matrix has full column rank. This is a standard linear regression in its coefficients even though the fitted mean can be curved as a function of \(x\).

The boundary knots are part of the specification, not merely bookkeeping. The constraints apply at those chosen values \(a\) and \(b\); changing them changes the basis functions and the spline space. In practice they are commonly set at the ends of the predictor range being modeled. The interior knots control where the cubic pieces join, while the natural constraints reduce the degrees of freedom available for the curve.

Common mistake

A frequent error is to read “natural boundary condition” as \(f(a)=f(b)=0\), or to force the fitted curve through specified endpoint responses. Those are constraints on function values, not on curvature. The natural cubic spline conditions are specifically \(f''(a)=0\) and \(f''(b)=0\). In the truncated-power derivation, it is the second-derivative equations that determine \(\beta_2\) and \(\beta_3\); \(\beta_0\) and \(\beta_1\) remain free to be estimated from the data.

Another mistake is to subtract two coefficients from the model without showing why. The reduction is justified because the two boundary equations are independent when \(a<b\), and the derivation explicitly solves them. Counting alone is not a substitute for applying the constraints to the chosen spline representation.

Key takeaway: Construct a natural cubic spline by starting with the cubic truncated-power basis, imposing zero second derivatives at the two boundary knots, and solving those two linear constraints. The resulting basis has two fewer free coefficients than the unrestricted cubic spline.

Check Your Understanding

Use the definition and basis derivation to answer these questions.

  1. A cubic spline has six distinct interior knots. How many free coefficients does its unrestricted truncated-power representation have, and how many remain after the natural constraints?
  2. In the truncated-power basis, why do the interior-knot terms make no contribution to \(f''(a)\) when \(a<\xi_j\) for every \(j\)?
  3. For \(a=1\), \(b=9\), and one interior knot at \(\xi=5\), write the reduced basis function \(g_1(x)\).
  4. Explain why \(f(a)=f(b)=0\) is not the definition of a natural cubic spline.
  5. What condition on the regression design matrix is needed to estimate all coefficients uniquely by ordinary least squares?