# How to calculate basis functions by hand

I am learning about natural splines and basis functions and am struggling with it a lot.

I understand the concepts of knots being the part where first and second derivatives are equal on either side. But when it comes to calculating basis functions and eventually a basis matrix I get confused.

I understand that for a cubic spline $$d_i(x) = \frac{(x-x_i)^{3}_+ - (x-x_n)^{3}_+}{x_n - x_i}, i=1,\dots,n-1.$$

for the example n = 4 ,

$$d_i(x) = \frac{(x-x_i)^{3}_+ - (x-x_4)^{3}_+}{x_4 - x_i}$$

From here onwards I get lost. How do I find $$d_1(x_1)$$? I would assume you just replace i with 1 and x with $$x_1$$ however this would result in $$d_1(x_1) = \frac{(x_1-x_i)^{3}_+ - (x_1-x_4)^{3}_+}{x_4 - x_1} = \frac{(0)^{3}_+ - (x_1-x_4)^{3}_+}{x_4 - x_1} = \frac{- (x_1-x_4)^{3}_+}{x_4 - x_1}$$

However the text book says the answer is 0. For $$d_1(x_2)$$ it gives an answer of $$\frac{(x_2-x_1)^3}{x_4-x_1}$$.

I do not understand why it appears that for every answer it doesn't include the second term of the numerator.

Any clarification as to how $$d_1(x_1)$$ or $$d_3(x_2)$$ is 0 would be very appreciated.

Not sure what book or reference you are using, but it is standard to assume you ordered the data so that $$x_1 \leq x_2 \leq \dots \leq x_n$$. So for $$d_1(x_1)$$, recalling that $$(w)_+ = \max(w,0)$$, we have

$$d_1(x_1) = \frac{(x_1 - x_4)_+^3}{x_4-x_1} = \frac{\big(\max(0,x_1-x_4)\big)^3}{x_4-x_1} = 0$$

where the last equality followed because $$x_1-x_4 \leq 0$$ so that the numerator is $$0$$.

Also, just in case, this question on the site may be of interest to you: Why are the basis functions for natural cubic splines expressed as they are? (ESL)

• Thankyou, I must have missed that if the answer would result in less than 0 it becomes 0. Commented Mar 26, 2021 at 1:16
• yep, that's the purpose of the little plus sign below the parenthesis $(x)_+$ Commented Mar 26, 2021 at 1:56