An order-M spline is a piecewise polynomial of degree M − 1 whose derivatives up to order M − 2 are continuous at the knots; with K knots it has M + K parameters. Fit one by least squares, drag the knots (the triangles under the upper panel) and watch the fit and its ±2 standard-error band respond. The lower panel shows the basis actually used: the truncated powers 1, x, …, (x − ξ)+M−1, or the B-splines, which span the same space but are local and nearly orthogonal. The fitted curve does not depend on the basis; the condition number does.
Least squares is solved by a Householder QR decomposition (not the normal equations), so both bases give the same fit to rounding error. The natural spline has boundary knots at the smallest and largest x and K interior knots, K + 2 parameters; in the B-spline version the two conditions f″ = 0 at the boundary knots are projected out, as in R's ns(). Band: ±2 σ̂√(h(x)T(HTH)−1h(x)) with σ̂2 = RSS/(N − p). Condition numbers are those of the N × p basis matrix (one-sided Jacobi SVD).
code/ch05_basis_expansions.py (tpower_basis, bspline_basis, ns_basis, ncs_basis_tp, artificial_data, sim_522).