Derivation
With the
Vandermonde matrix \(X\):
\(
X =
\begin{bmatrix}
1&x_{1}&x_{1}^{2}&x_{1}^{3} \\
1&x_{2}&x_{2}^{2}&x_{2}^{3} \\
1&x_{3}&x_{3}^{2}&x_{3}^{3} \\
\vdots &\vdots &\vdots &\vdots \\
1&x_{n}&x_{n}^{2}&x_{n}^{3} \\
\end{bmatrix}
\)
… and the corresponding \(y\) vector:
\(
y =
\begin{bmatrix}
y_{1} \\
y_{2} \\
y_{3} \\
\vdots\\
y_{n} \\
\end{bmatrix}
\)
… we can write the equation for the coefficients \(c\) as:
\(
X^\top X c = X^\top y
\)
This leads to:
\(
\begin{bmatrix}
n & \sum x & \sum x^2 & \sum x^3 \\
\sum x & \sum x^2 & \sum x^3 & \sum x^4 \\
\sum x^2 & \sum x^3 & \sum x^4 & \sum x^5 \\
\sum x^3 & \sum x^4 & \sum x^5 & \sum x^6 \\
\end{bmatrix}
\,
\begin{bmatrix}
c_{0} \\
c_{1} \\
c_{2} \\
c_{3} \\
\end{bmatrix}
=
\begin{bmatrix}
\sum y \\
\sum x y \\
\sum x^2 y \\
\sum x^3 y \\
\end{bmatrix}
\)
Since the matrix \(A = X^\top X \) is positive semidefinite, we can find a
Cholesky decomposition:
\(
A = L \, L^\top
\)
Here the matrix \(L\) is a real lower triangular matrix with positive diagonal entries.
I dully followed the steps in the linked
Lecture 7 Notes to calculate \(L\) and then used forward and back substitution to solve:
\(
L \, L^\top c = X^\top y
\)
Programs
CLΣ:
Clear stack and registers and set
ΣREG to 20.
Σ+
Add a data point \((x, y)\).
Σ-
Remove a data point \((x, y)\).
CFIT
Calculate the coefficients of the cubic polynomial.
P(X)
Evaluate the cubic polynomial at \(x\).
This program is for the
HP-42s, but can also be used with the
HP-41C:
Code:
00 { 298-Byte Prgm }
01▸LBL "CLΣ"
02 CLRG
03 ΣREG 20
04 CLST
05 RTN
06▸LBL "Σ+"
07 Σ+
08 LASTX
09 ENTER
10 ENTER
11 X↑2
12 R↑
13 ×
14 STO+ 18
15 ×
16 STO+ 19
17 R↓
18 ENTER
19 ×
20 ×
21 STO+ 14
22 ×
23 STO+ 15
24 ×
25 STO+ 16
26 ×
27 STO+ 17
28 RCL 25
29 RTN
30▸LBL "Σ-"
31 Σ-
32 LASTX
33 ENTER
34 ENTER
35 X↑2
36 R↑
37 ×
38 STO- 18
39 ×
40 STO- 19
41 R↓
42 ENTER
43 ×
44 ×
45 STO- 14
46 ×
47 STO- 15
48 ×
49 STO- 16
50 ×
51 STO- 17
52 RCL 25
53 RTN
54▸LBL "CFIT"
55 RCL 22
56 STO 00
57 RCL 24
58 STO 01
59 RCL 18
60 STO 02
61 RCL 19
62 STO 03
63 RCL 25
64 STO 04
65 RCL 20
66 STO 05
67 RCL 21
68 STO 06
69 STO 08
70 RCL 14
71 STO 07
72 STO 09
73 RCL 15
74 STO 10
75 STO 11
76 RCL 16
77 STO 12
78 RCL 17
79 STO 13
80 RCL 04
81 SQRT
82 STO 04
83 STO÷ 05
84 STO÷ 06
85 STO÷ 07
86 RCL 05
87 X↑2
88 STO- 08
89 RCL 05
90 RCL 06
91 ×
92 STO- 09
93 RCL 05
94 RCL 07
95 ×
96 STO- 10
97 RCL 06
98 X↑2
99 STO- 11
100 RCL 06
101 RCL 07
102 ×
103 STO- 12
104 RCL 07
105 X↑2
106 STO- 13
107 RCL 08
108 SQRT
109 STO 08
110 STO÷ 09
111 STO÷ 10
112 RCL 09
113 X↑2
114 STO- 11
115 RCL 09
116 RCL 10
117 ×
118 STO- 12
119 RCL 10
120 X↑2
121 STO- 13
122 RCL 11
123 SQRT
124 STO 11
125 STO÷ 12
126 RCL 12
127 X↑2
128 STO- 13
129 RCL 13
130 SQRT
131 STO 13
132 RCL 04
133 STO÷ 00
134 RCL 05
135 RCL 00
136 ×
137 STO- 01
138 RCL 08
139 STO÷ 01
140 RCL 06
141 RCL 00
142 ×
143 RCL 09
144 RCL 01
145 ×
146 +
147 STO- 02
148 RCL 11
149 STO÷ 02
150 RCL 07
151 RCL 00
152 ×
153 RCL 10
154 RCL 01
155 ×
156 +
157 RCL 12
158 RCL 02
159 ×
160 +
161 STO- 03
162 RCL 13
163 STO÷ 03
164 RCL 13
165 STO÷ 03
166 RCL 12
167 RCL 03
168 ×
169 STO- 02
170 RCL 11
171 STO÷ 02
172 RCL 09
173 RCL 02
174 ×
175 RCL 10
176 RCL 03
177 ×
178 +
179 STO- 01
180 RCL 08
181 STO÷ 01
182 RCL 05
183 RCL 01
184 ×
185 RCL 06
186 RCL 02
187 ×
188 +
189 RCL 07
190 RCL 03
191 ×
192 +
193 STO- 00
194 RCL 04
195 STO÷ 00
196 RCL 03
197 RCL 02
198 RCL 01
199 RCL 00
200 RTN
201▸LBL "P(X)"
202 ENTER
203 ENTER
204 ENTER
205 RCL 03
206 ×
207 RCL 02
208 +
209 ×
210 RCL 01
211 +
212 ×
213 RCL 00
214 +
215 END
Registers
Matrix Registers:
00: \(t \to c_0\)
01: \(u \to c_1\)
02: \(v \to c_2\)
03: \(w \to c_3\)
04: \(n\)
05: \(a\)
06: \(b\)
07: \(c\)
08: \(b\)
09: \(c\)
10: \(d\)
11: \(d\)
12: \(e\)
13: \(f\)
Statistics Registers:
14: \(Σ x^3 \to c\)
15: \(Σ x^4 \to d\)
16: \(Σ x^5 \to e\)
17: \(Σ x^6 \to f\)
18: \(Σ x^2 y \to v\)
19: \(Σ x^3 y \to w\)
20: \(Σ x \to a\)
21: \(Σ x^2 \to b\)
22: \(Σ y \to t\)
23: \(Σ y^2\)
24: \(Σ x y \to u\)
25: \(n \to n\)
Correspondence to the matrix:
\(
\begin{bmatrix}
n & a & b & c & \, & t \\
& b & c & d & \, & u \\
& & d & e & \, & v \\
& & & f & \, & w \\
\end{bmatrix}
\begin{bmatrix}
04 & 05 & 06 & 07 & \, & 00 \\
& 08 & 09 & 10 & \, & 01 \\
& & 11 & 12 & \, & 02 \\
& & & 13 & \, & 03 \\
\end{bmatrix}
\)
Example
Let us find the cubic regression function for the following dataset:
(0, 1), (2, 0), (3, 3), (4, 5), (5, 4).
CLΣ
1 ENTER 0 Σ+
0 ENTER 2 Σ+
3 ENTER Σ+
5 ENTER 4 Σ+
4 ENTER 5 Σ+
CFIT
T: -0.3868
Z: 3.0687
Y: -5.0755
X: 0.9973
Therefore, the cubic regression function that best fits our data is:
\(
y = 0.9973 - 5.0755x + 3.0687x^2 - 0.3868x^3
\)
Let us estimate the value for \(x = 1\):
1 P(X)
-1.3962
References