Spline Function/Examples/Cubic Spline

From ProofWiki
Jump to navigation Jump to search



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.

\(\ds a_0 x^3 + b_0 x^2 + c_0 x + d_0\) \(=\) \(\ds \) Cubic equation from $0$ to $12$ months
\(\ds a_1 x^3 + b_1 x^2 + c_1 x + d_1\) \(=\) \(\ds \) Cubic equation from $12$ to $24$ months
\(\ds a_2 x^3 + b_2 x^2 + c_2 x + d_2\) \(=\) \(\ds \) Cubic equation from $24$ to $36$ months
\(\ds a_3 x^3 + b_3 x^2 + c_3 x + d_3\) \(=\) \(\ds \) Cubic equation from $36$ to $48$ months


We proceed as follows:

\(\ds 1) \ \ \) \(\ds a_0 \paren{0 }^3 + b_0 \paren{0 }^2 + c_0 \paren{0 } + d_0\) \(=\) \(\ds 0\) Value of first equation at $0$ months
\(\ds 2) \ \ \) \(\ds a_0 \paren{12 }^3 + b_0 \paren{12 }^2 + c_0 \paren{12 } + d_0\) \(=\) \(\ds 40\) Value of first equation at $12$ months
\(\ds 3) \ \ \) \(\ds a_1 \paren{12 }^3 + b_1 \paren{12 }^2 + c_1 \paren{12 } + d_1\) \(=\) \(\ds 40\) Value of second equation at $12$ months
\(\ds 4) \ \ \) \(\ds a_1 \paren{24 }^3 + b_1 \paren{24 }^2 + c_1 \paren{24 } + d_1\) \(=\) \(\ds 70\) Value of second equation at $24$ months
\(\ds 5) \ \ \) \(\ds a_2 \paren{24 }^3 + b_2 \paren{24 }^2 + c_2 \paren{24 } + d_2\) \(=\) \(\ds 70\) Value of third equation at $24$ months
\(\ds 6) \ \ \) \(\ds a_2 \paren{36 }^3 + b_2 \paren{36 }^2 + c_2 \paren{36 } + d_2\) \(=\) \(\ds 90\) Value of third equation at $36$ months
\(\ds 7) \ \ \) \(\ds a_3 \paren{36 }^3 + b_3 \paren{36 }^2 + c_3 \paren{36 } + d_3\) \(=\) \(\ds 90\) Value of fourth equation at $36$ months
\(\ds 8) \ \ \) \(\ds a_3 \paren{48 }^3 + b_3 \paren{48 }^2 + c_3 \paren{48 } + d_3\) \(=\) \(\ds 100\) Value of fourth equation at $48$ months
\(\ds 9) \ \ \) \(\ds 3a_0 \paren{12 }^2 + 2b_0 \paren{12 } + c_0\) \(=\) \(\ds 3a_1 \paren{12 }^2 + 2b_1 \paren{12 } + c_1\) First derivative of First and Second equations equal at $12$ months
\(\ds 10) \ \ \) \(\ds 3a_1 \paren{24 }^2 + 2b_1 \paren{24 } + c_1\) \(=\) \(\ds 3a_2 \paren{24 }^2 + 2b_2 \paren{24 } + c_2\) First derivative of Second and Third equations equal at $24$ months
\(\ds 11) \ \ \) \(\ds 3a_2 \paren{36 }^2 + 2b_2 \paren{36 } + c_2\) \(=\) \(\ds 3a_3 \paren{36 }^2 + 2b_3 \paren{36 } + c_3\) First derivative of Third and Fourth equations equal at $36$ months
\(\ds 12) \ \ \) \(\ds 6a_0 \paren{12 } + 2b_0\) \(=\) \(\ds 6a_1 \paren{12 } + 2b_1\) Second derivative of First and Second equations equal at $12$ months
\(\ds 13) \ \ \) \(\ds 6a_1 \paren{24 } + 2b_1\) \(=\) \(\ds 6a_2 \paren{24 } + 2b_2\) Second derivative of Second and Third equations equal at $24$ months
\(\ds 14) \ \ \) \(\ds 6a_2 \paren{36 } + 2b_2\) \(=\) \(\ds 6a_3 \paren{36 } + 2b_3\) Second derivative of Third and Fourth equations equal at $36$ months
\(\ds 15) \ \ \) \(\ds 6a_0 \paren {0 } + 2b_0\) \(=\) \(\ds 0\) Second derivative at endpoint $1$ is set equal to zero
\(\ds 16) \ \ \) \(\ds 6a_3 \paren{48 } + 2b_3\) \(=\) \(\ds 0\) Second derivative at endpoint $2$ is set equal to zero


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 A to our $16 \times 1$ Matrix 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:

\(\ds -0.0012 x^3 - 0.0000 x^2 + 3.5119 x + 0.0000\) \(=\) \(\ds \) Cubic equation from $0$ to $12$ months
\(\ds 0.0004 x^3 - 0.0595 x^2 + 4.2262 x - 2.8571\) \(=\) \(\ds \) Cubic equation from $12$ to $24$ months
\(\ds -0.0004 x^3 + 0.0000 x^2 + 2.7976 x + 8.5714\) \(=\) \(\ds \) Cubic equation from $24$ to $36$ months
\(\ds 0.0012 x^3 - 0.1786 x^2 + 9.2262 x - 68.5714\) \(=\) \(\ds \) Cubic equation from $36$ to $48$ months


As a simple example, at $20$ months, $61.16\text{%}$ 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 \text{%}$

$\blacksquare$