Spline Function/Examples/Cubic Spline

== Example of Spline Function ==
:How to find the coefficients $a$, $b$, $c$, and $d$ in the cubic spline function $ax^3 + bx^2 + cx + d$
Assume that an auto insurance company pays out losses incurred in a given year according to the table below:
:$\begin{array} {|r|r|}
\text {Time in Months } & \text {Percent Paid } \\
\hline
0 & 0 \\
12 & 40 \\
24 & 70 \\
36 & 90 \\
48 & 100 \\
\end{array} $
We wish to create a model that will interpolate the amount of loss paid at any point in time between $0$ and $48$ months.
One potential model we could employ is the Cubic Spline.
For our cubic spline, we will need to solve for $4$ coefficients for $4$ separate cubic equations.
In total, we are solving for $16$ variables.
We proceed as follows:
We now have $16$ equations and $16$ unknowns.
By Simultaneous Linear Equations has Unique Solution iff Rank of Matrix of Coefficients equals Number of Columns, we have a unique solution.
:$\mathbf A \mathbf x = \begin{bmatrix}
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
12^3 & 12^2 & 12 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 12^3 & 12^2 & 12 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 24^3 & 24^2 & 24 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 24^3 & 24^2 & 24 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 36^3 & 36^2 & 36 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 36^3 & 36^2 & 36 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 48^3 & 48^2 & 48 & 1 \\
432 & 24 & 1 & 0 & -432 & -24 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 1728 & 48 & 1 & 0 & -1728 & -48 & -1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 3888 & 72 & 1 & 0 & -3888 & -72 & -1 & 0 \\
72 & 2 & 0 & 0 & -72 & -2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 144 & 2 & 0 & 0 & -144 & -2 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 216 & 2 & 0 & 0 & -216 & -2 & 0 & 0 \\
0 & 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 288 & 2 & 0 & 0 \\
\end{bmatrix} 
\begin{bmatrix} a_0 \\ b_0 \\ c_0 \\ d_0 \\ a_1 \\ b_1 \\ c_1 \\ d_1 \\ a_2 \\ b_2 \\ c_2 \\ d_2 \\ a_3 \\ b_3 \\ c_3 \\ d_3 \end{bmatrix} = 
\begin{bmatrix} 0 \\ 40 \\ 40 \\ 70 \\ 70 \\ 90 \\ 90 \\ 100 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \end{bmatrix} = \mathbf B$
To obtain our solution, we must multiply the left inverse matrix of matrix $\mathbf A$ to our $16 \times 1$ matrix $\mathbf B$:
:$\mathbf A \mathbf x = \mathbf B$
:$\mathbf A^{-1} \mathbf A \mathbf x = \mathbf A^{-1}\mathbf B$
:$\mathbf x = \mathbf A^{-1}\mathbf B$
Running the matrix calculation above through a spreadsheet, we get:
:$\begin{bmatrix} a_0 \\ b_0 \\ c_0 \\ d_0 \\ a_1 \\ b_1 \\ c_1 \\ d_1 \\ a_2 \\ b_2 \\ c_2 \\ d_2 \\ a_3 \\ b_3 \\ c_3 \\ d_3 \end{bmatrix} = 
\begin{bmatrix} -0.0012 \\ -0.0000 \\ 3.5119 \\ 0.0000 \\ 0.0004 \\ -0.0595 \\ 4.2262 \\ -2.8571 \\ -0.0004 \\ 0.0000 \\ 2.7976 \\ 8.5714 \\ 0.0012 \\ -0.1786 \\ 9.2262 \\ -68.5714 \end{bmatrix}$
Our $4$ cubic equations are then:
As a simple example, at $20$ months, $61.16 \%$ of the losses incurred will have been paid.
:$0.0004 \paren{20}^3 - 0.0595 \paren{20}^2 + 4.2262 \paren{20} - 2.8571 = 61.16 \%$
Category:Examples of Splines