Post Reply 
Program to fit data to normal distribution function
11-12-2023, 07:37 PM (This post was last modified: 11-13-2023 08:41 AM by rawi.)
Post: #1
Program to fit data to normal distribution function
Program to fit data to normal distribution function.
a) Application:
1. Set calculator in User-Mode.
2. XEQ NFIT (Start of program, deleting all registers and stack, setting statistics registers to Reg05)
3. Put in data:
For every data point: X ENTER Y A -> Number of data points so far is shown.
Correction of last data point: B
4. Compute results: C. Parameters a, b, c, R² are shown. Show results again: D
Whereas a, b, c are the parameters of the following function: Y = a*EXP[(X-b)²/c]
and R² is the coefficient of determination.
5. Compute estimate of Y for given X:
E -> X? -> Put in given X-value R/S -> Estimated Y Ye
b) Example: Data of heights of a group of persons and frequency:
Height (X) Frequency in % (Y)
53 6.25
54 6.25
55 6.25
56 12.5
57 18.75
58 18.75
59 12.5
60 12.5
62 6.25
USR
XEQ Alpha NFIT Alpha
Data Input:
53 ENTER 6.25 A -> 1
54 ENTER 6.25 A -> 2
55 ENTER 6.75 A -> 3 (Error)
B -> 2 (The last data point is deleted)
55 ENTER 6.25 A -> 3 (Correct input)
56 ENTER 12.5 A -> 4
57 ENTER 18.75 A ->5

62 ENTER 6.25 ->9
Computation: C -> a=15.0203 -> R/S -> b=57.8945 -> R/S -> c=-20.586 – R/S -> R2=0.7477
Fitted normal distribution is Y = a*EXP[(X-b)²/c]
The parameters a, b, c, R² are stored in registers 01, 02, 03, 04.
Results are shown again by pressing D.
Estimate Y for X=61: E -> X? 61 R/S -> Ye= 9.4020 (Estimate for Y)
c) Registers used: 01 – 21
01 a / ln Y a=EXP(R21-R20²/4/R19)
02 b / X b=-R20/2/R19
03 c c=1/R19
04 R² R²=(R21*R07+R20*R09+R19*R13-R07²/R10)/(R08-R07²/R10)
05 ∑Xi
06 ∑Xi²
07 ∑ln(Yi)
08 ∑(ln(Yi))²
09 ∑Xi * ln(Yi)
10 n
11 ∑Xi^3
12 ∑Xi^4
13 ∑Xi² * ln(Yi)
14 R06*R10-R05²
15 R10*R13-R06*R07
16 R10*R11-R05*R06
17 R10*R09-R05*R07
18 R10*R12-R06²
19 (R14*R15-R16*R17)/(R14*R18-R16²)
20 (R17-R16*R19)/R14
21 (R07-R20*R05-R19*R06)/R10
d) Program listing (256 Bytes):
Code:
01 LBL "NFIT"
02 CLRG
03 ∑REG 05
04 CLST
05 RTN

06 LBL A
07 CF 00
08 LN
09 STO 01
10 X<>Y
11 STO 02
12 ∑+
13 GTO 01
14 LBL B
15 RCL 01
16 RCL 02
17 ∑-
18 SF 00
19 LBL 01
20 RCL 02
21 3
22 Y^X
23 FS? 00
24 CHS
25 ST+ 11
26 RCL 02
27 4
28 Y^X
29 FS? 00
30 CHS
31 ST+ 12
32 RCL 01
33 RCL 02
34 X^2
35 *
36 FS? 00
37 CHS
38 ST+ 13
39 RCL 10
40 RTN

41 LBL C
42 RCL 10
43 STO 14
44 STO 15
45 STO 16
46 STO 17
47 RCL 12
48 *
49 STO 18
50 RCL 06
51 ST* 14
52 X^2
53 ST- 18 
54 RCL 13
55 ST* 15
56 RCL 11 
57 ST*16
58 RCL 09
59 ST* 17
60 RCL 05
61 X^2
62 ST- 14
63 RCL 06
64 RCL 07
65 *
66 ST- 15
67 RCL 05
68 RCL 06
69 *
70 ST- 16
71 RCL 05
72 RCL 07
73 *
74 ST- 17
75 RCL 14
76 RCL 15
77 *
78 RCL 16
79 RCL 17
80 *
81 -
82 RCL 14
83 RCL 18
84 *
85 RCL 16
86 X^2
87 -
88 /
89 STO 19
90 RCL 17
91 RCL 16
92 RCL 19
93 *
94 -
95 RCL 14
96 /
97 STO 20
98 RCL 07
99 RCL 20
100 RCL 05
101 *
102 -
103 RCL 19
104 RCL 06
105 *
106 -
107 RCL 10
108 /
109 STO 21
110 RCL 20
111 X^2
112 4
113 /
114 RCL 19
115 /
116 -
117 E^X
118 STO 01
119 RCL 20
120 CHS
121 2
122 /
123 RCL 19
124 1/X
125 STO 03
126 *
127 STO 02
128 RCL 21
129 RCL 07
130 *
131 RCL 20
132 RCL 09
133 *
134 +
135 RCL 19
136 RCL 13
137 *
138 +
139 RCL 07
140 X^2
141 RCL 10
142 /
143 -
144 RCL 08
145 RCL 07
146 X^2 
147 RCL 10
148 /
149 -
150 /
151 STO 04
152 LBL D
153 "a="
154 ARCL 01
155 PROMPT
156 "b="
157 ARCL 02
158 PROMPT
159 "c="
160 ARCL 03
161 PROMPT
162 "R2="
163 ARCL 04
164 PROMPT
165 RTN
166 LBL E
167 "X?"
168 PROMPT
169 RCL 02
170 -
171 X^2
172 RCL 03
173 /
174 E^X
175 RCL 01
176 *
177 "Ye="
178 ARCL X
179 PROMPT
180 RTN
181 END
e) Acknowledgement: Formulas for program and example taken from the book “Curve Fitting For Programmable Calculators – Third Edition” by William M. Kolb, 1984.
f) Link to RAW-file
https://www.dropbox.com/scl/fo/ri5v9o43r...7b5w9&dl=0
Find all posts by this user
Quote this message in a reply
11-12-2023, 11:04 PM
Post: #2
RE: Program to fit data to normal distribution function
Link to .raw file?

Love the program. I remember in the early 80s I wrote several programs that fit data to popular statistical distributions and set them the the Users' Library in Corvallis!

Namir
Find all posts by this user
Quote this message in a reply
11-13-2023, 05:30 AM
Post: #3
RE: Program to fit data to normal distribution function
Hi Namir,

Thank you for your interest and your friendly word.
I wanted to attach the raw-file to the post but did not realize that raw-files are not accepted as attachments. So I added a link to a Dropbox at the end of the original post, I hope that works. Furthermore I improved the program and added the possibility to estimate Y for a given X. This is included in the original post.

Best

Raimund
Find all posts by this user
Quote this message in a reply
11-13-2023, 06:19 AM
Post: #4
RE: Program to fit data to normal distribution function
Thanks for the link. It worked and I was able to download the .raw file and import it to my HP-41CX emulator. I went ahead and added the following lines (as the last commands in LBL C) to display the values of a, b, c, and R squared using:

Code:
"a="
ARCL 01
PROMPT
"b="
ARCL 02
PROMPT
"c="
ARCL 03
PROMPT
"R2="
ARCL 04
PROMPT

The program woks like a charm! Thank you!

Namir
Find all posts by this user
Quote this message in a reply
11-13-2023, 08:01 AM
Post: #5
RE: Program to fit data to normal distribution function
Hi Namir,
thank you very much. Your suggestion improves the user experience a lot. So I included it in the program. I added another subroutine name D to display results again and the subroutine for the estimation of Y for a given X was renamed E, which is better for estimation.
Original post and raw-file were updated accordingly.
Best
Raimund
Find all posts by this user
Quote this message in a reply
11-13-2023, 12:37 PM
Post: #6
RE: Program to fit data to normal distribution function
(11-13-2023 05:30 AM)rawi Wrote:  Hi Namir,

Thank you for your interest and your friendly word.
I wanted to attach the raw-file to the post but did not realize that raw-files are not accepted as attachments. So I added a link to a Dropbox at the end of the original post, I hope that works. Furthermore I improved the program and added the possibility to estimate Y for a given X. This is included in the original post.

Best

Raimund

An easy workaround is to simply rename the file as a text file and then you can attach it to a post here. Although you should note in your post that it has been renamed for convenience, it's better to also keep that right in the file's name as a reminder, so for example rename MYFILE.RAW to MYFILE.RAW.TXT.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
11-13-2023, 08:22 PM
Post: #7
RE: Program to fit data to normal distribution function
To download the .raw file for my slightly enhanced version of the curve fite program click here.

Namir
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)