The Museum of HP Calculators


HP-42S: Polynomial Regression

Copyleft (C) 2003 Glen Kilpatrick

Distributed under GNU General Public License

This program is supplied without representation or warranty of any kind. The author and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

Description

In this context, the word "regression" refers to the simplification of a set of ordered pairs (of coordinates, for a scatter plot, or of independent and dependent variables in the more general sense) to a "best fit" trend line, and "polynomial" to this line being a function in higher powers of the independent variable.

To amplify by example on the above, most HP calculators will do a linear regression of sets of X,Y points; here the resultant best fit is an equation with X to the first power, or mX+b. Those that will also do a "power" fit will yield a quadratic equation, or power of two, a*X2+b*X+c; cubic (also known as "spline" for the use of a thin strip of wood that used to be bent to fit pins on a board, and by flexing, or better, relaxing to minimum energy would reveal adjacent sections of different cubic equations) regression will be a power of three for X, etc.

This program will yield a polynomial curve fit of arbitrary power. But this requires more discussion. It should be obvious that a linear regression requires a minimum of two points, a quadratic three, a cubic four, etc.

If that is the case, then why not always pick a power that is one less than the number of points. The curve generated will pass through every point in the set (note that I gloss over multivalued sets, a particular X must yield one and only one Y), and the fit will be "perfect". The problem with this is that inherent measurement noise will be magnified, the curve will gyrate wildly outside the points in the set, the "energy" will be nowhere near minimized, and nothing will have been made simple.

In the real world, one will know that, e.g. measurements of a velocity distribution as a function of time will have a quadratic "dimension", as S=1/2*a*T2+v*T+S[0] is a quadratic (yes, this is circular reasoning, but it's also an illustration of why picking the proper dimension to the problem is important). A cubic regression may yield a cubic equation, but the model won't be a good fit for reality whatever the regression coefficient is. See Dimensional Analysis for more on this way of approaching real-world modeling.

Notes

This program is a HP-42S port of PREGR (Polynomial Regression) or "Polynomial Curve Fit By Dennis York", "Copyright (c) 1992 Hewlett-Packard Company", which is itself based upon an HP-28S program submitted to HP by Dr. Robert C. Wyckoff.

Example

Please see HP's example (and attribution to Dr. Wyckoff).

Program Listing

01>LBL "PREGR"
02 REAL?
03 GTO 00
04 GTO 15
05>LBL 00
06 ENTER
07 ENTER
08 IP
09 -
10 X=0?
11 GTO 01
12 R↓
13 GTO 15
14>LBL 01
15 R↓
16 X≤0?
17 GTO 15
18 X⇔Y?
19 MAT?
20 GTO 02
21 X⇔Y
22 GTO 15
23>LBL 02
24 ENTER
25 DIM?
26 X⇔Y
27 R↓
28 2
29 X=Y?
30 GTO 16
31 R↓
32 R↓
33 X⇔Y
34>LBL 15
35 "USAGE: [n*2] of"
36 ⊢" [x,y]\lf→ Y, na"
37 ⊢"t# order → x \.."
38 AVIEW
39 STOP
40 "Inputs are save"
41 ⊢"d in\lfvars ΣDAT"
42 ⊢" & ord     \.."
43 AVIEW
44 STOP
45 "[1+ord*1] ANS^ "
46 ⊢"← coefs\lfSOLVER"
47 ⊢" HRNR: x, f(x)"
48 ⊢"\.."
49 AVIEW
50 STOP
51 "PREGR uses STac"
52 ⊢"k, and\lfnames t"
53 ⊢"temp vars 'p*'"
54 AVIEW
55 STOP
56 GTO 15
57>LBL 16
58 "Initialization\lf"
59 AVIEW
60 WRAP
61 REALRES
62 CLV "ΣDAT"
63 CLV "ord"
64 CLV "ANS^"
65 CLV "x"
66 CLV "f(x)"
67 R↓
68 R↓
69 STO "ΣDAT"
70 X⇔Y
71 STO "ord"
72 1
73 +
74 1
75 NEWMAT
76 STO "ANS^"
77 STO "pM1"
78 2
79 RCLx "ord"
80 1
81 +
82 1
83 NEWMAT
84 STO "pM2"
85 1
86 RCL+ "ord"
87 ENTER
88 NEWMAT
89 STO "pM3"
90 1E-3
91 RCL× "ord"
92 STO "pLC1"
93 2
94 ×
95 1
96 +
97 STO "pLC2"
98 1
99 ENTER
100 COMPLEX
101 STO "pIJ→"
102 STO "pIJ←"
103 "Loop 1"
104>LBL 17
105 AVIEW
106 0
107>LBL 18
108 INDEX "ΣDAT"
109 RCL "pIJ→"
110 COMPLEX
111 STOIJ
112 R↓
113 R↓
114 RCLEL
115 J+
116 RCL "pLC1"
117 IP
118 yx
119 RCLEL
120 J+
121 ×
122 +
123 RCLIJ
124 COMPLEX
125 STO "pIJ→"
126 R↓
127 FC? 77
128 GTO 18
129 INDEX "pM1"
130 RCL "pIJ←"
131 COMPLEX
132 STOIJ
133 R↓
134 R↓
135 STOEL
136 J+
137 RCLIJ
138 COMPLEX
139 STO "pIJ←"
140 ⊢"."
141 ISG "pLC1"
142 GTO 17
143 "Loop 2"
144 INDEX "pM2"
145 RCL "ΣDAT"
146 DIM?
147 R↓
148 STOEL
149 J+
150 RCLIJ
151 COMPLEX
152 STO "pIJ←"
153>LBL 19
154 AVIEW
155 1
156 ENTER
157 COMPLEX
158 STO "pIJ→"
159 0
160>LBL 20
161 INDEX "ΣDAT"
162 RCL "pIJ→"
163 COMPLEX
164 STOIJ
165 R↓
166 R↓
167 RCLEL
168 I+
169 RCL "pLC2"
170 IP
171 yx
172 +
173 RCLIJ
174 COMPLEX
175 STO "pIJ→"
176 R↓
177 FC? 76
178 GTO 20
179 INDEX "pM2"
180 RCL "pIJ←"
181 COMPLEX
182 STOIJ
183 R↓
184 R↓
185 STOEL
186 J+
187 RCLIJ
188 COMPLEX
189 STO "pIJ←"
190 ⊢"."
191 ISG "pLC2"
192 GTO 19
193 "Loop 3"
194 RCL "pIJ←"
195 STO "pIJ→"
196>LBL 21
197 AVIEW
198 INDEX "pM2"
199 RCL "pIJ→"
200 COMPLEX
201 STOIJ
202 RCLEL
203 I+
204 RCLIJ
205 COMPLEX
206 STO "pIJ→"
207 R↓
208 INDEX "pM3"
209 RCL "pIJ←"
210 COMPLEX
211 STOIJ
212 R↓
213 R↓
214 STOEL
215 J+
216 RCLIJ
217 COMPLEX
218 STO "pIJ←"
219 FC? 76
220 GTO 21
221 FS? 77
222 GTO 03
223 RCL "pIJ←"
224 STO "pIJ→"
225 ⊢"."
226 GTO 21
227>LBL 03
228 RCL "pM1"
229 RCL÷ "pM3"
230 STO "ANS^"
231 CPXRES
232 CLV "pM1"
233 CLV "pM2"
234 CLV "pM3"
235 CLV "pLC1"
236 CLV "pLC2"
237 CLV "pIJ→"
238 CLV "pIJ←"
239 CLV "x"
240 CLV "f(x)"
241 CLST
242 "Done! SOLVER pg"
243 ⊢"m HRNR\lfcompute"
244 ⊢"s x _vs_ f(x)"
245 AVIEW
246 STOP
247 GTO "PREGR"
248>LBL "HRNR"
249 MVAR "x"
250 MVAR "f(x)"
251 INDEX "ANS^"
252 I-
253 RCL "x"
254 ENTER
255 ENTER
256 ENTER
257 0
258>LBL 04
259 ×
260 RCLEL
261 I-
262 +
263 FC? 76
264 GTO 04
265 RCL- "f(x)"
266 END

Resources Used

LaBeLs PREGR, HRNR, 00, 01, 02, 03, 04, 15, 16, 17, 18, 19, 20, & 21. Variables ΣDAT, ord, ANS^, pM1, pM2, pM3, pLC1, pLC2, pIJ→, pIJ←, x, & f(x).


Go back to the software library
Go back to the main exhibit hall