Post Reply 
Fit data to normal and lognormal distribution
11-14-2023, 02:24 PM
Post: #1
Fit data to normal and lognormal distribution
This program is 95% the same as the program in the thread "program to fit data to normal distribution." So I could have changed that thread. But it could be that someone wants to fit the normal distribution function but not the lognormal distribution function and prefers the shorter and simpler program. So I decided to present this program in a new thread. In the forum.swissmicros.com you will find the raw-file as an attechment: Here

a) Application:
1. Set calculator in User-Mode.
2. XEQ NLFIT (Start of program, deleting all registers, setting statistics registers to Reg05)
You are asked whether you want to fit a normal or a log-normal distribution:
Put in 1 for normal and 2 for log-normal distribution.
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 1: Normal Distribution - 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 NLFIT Alpha -> NV=1, LNNV=2? We want to use normal distribution, therefore 1 R/S -> 0
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

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)
Example 2: Lognormal distribution: Data:
X Y
100 330
200 300
300 270
400 240
500 220
USR
XEQ Alpha NLFIT Alpha -> NV=1, LNNV=2?
We want to use log-normal distribution, therefore 2 R/S -> 0
Data Input (all data have to be greater than zero):
100 ENTER 330 A -> 1

500 ENTER 220 A -> 5
Correction can be done as in example for normal distribution.
Computation: C -> a=331,3041 -> R/S -> b=4.4288 -> R/S -> c=-7.7372 -> R/S -> R2=0.9992
Fitted log-normal distribution is Y = a*EXP[(ln(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=600: E -> X? -> 600 R/S -> Ye= 200.8183 (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 or ∑ln(Xi)
06 ∑Xi² or ∑(ln(Xi))²
07 ∑ln(Yi)
08 ∑(ln(Yi))²
09 ∑Xi * ln(Yi) or ∑ln(Xi) * ln(Yi)
10 n
11 ∑Xi^3 or ∑(ln(Xi))^3
12 ∑Xi^4 or ∑(ln(Xi))^4
13 ∑Xi² * ln(Yi) or ∑(ln(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 (283 Bytes):
Code:
01 LBL “NLFIT”
02 CF 01
03 CLRG
04 ∑REG 05
05 2
06 “NV=1, LNNV=2?”
07 PROMPT
08 X=Y?
09 SF 01 
10 CLST
11 RTN

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

49 LBL C
50 RCL 10
51 STO 14
52 STO 15
53 STO 16
54 STO 17
55 RCL 12
56 *
57 STO 18
58 RCL 06
59 ST* 14
60 X^2
61 ST- 18 
62 RCL 13
63 ST* 15
64 RCL 11 
65 ST*16
66 RCL 09
67 ST* 17
68 RCL 05
69 X^2
70 ST- 14
71 RCL 06
72 RCL 07
73 *
74 ST- 15
75 RCL 05
76 RCL 06
77 *
78 ST- 16
79 RCL 05
80 RCL 07
81 *
82 ST- 17
83 RCL 14
84 RCL 15
85 *
86 RCL 16
87 RCL 17
88 *
89 -
90 RCL 14
91 RCL 18
92 *
93 RCL 16
94 X^2
95 -
96 /
97 STO 19
98 RCL 17
99 RCL 16
100 RCL 19
101 *
102 -
103 RCL 14
104 /
105 STO 20
106 RCL 07
107 RCL 20
108 RCL 05
109 *
110 -
111 RCL 19
112 RCL 06
113 *
114 -
115 RCL 10
116 /
117 STO 21
118 RCL 20
119 X^2
120 4
121 /
122 RCL 19
123 /
124 -
125 E^X
126 STO 01
127 RCL 20
128 CHS
129 2
130 /
131 RCL 19
132 1/X
133 STO 03
134 *
135 STO 02
136 RCL 21
137 RCL 07
138 *
139 RCL 20
140 RCL 09
141 *
142 +
143 RCL 19
144 RCL 13
145 *
146 +
147 RCL 07
148 X^2
149 RCL 10
150 /
151 -
152 RCL 08
153 RCL 07
154 X^2 
155 RCL 10
156 /
157 -
158 /
159 STO 04

160 LBL D
161 “a=”
162 ARCL 01
163 PROMPT
164 “b=”
165 ARCL 02
166 PROMPT
167 “c=”
168 ARCL 03
169 PROMPT
170 “R2=”
171 ARCL 04
172 PROMPT
173 RTN
174 LBL E
175 “X?”
176 PROMPT
177 FS? 01
178 LN
179 RCL 02
180 -
181 X^2
182 RCL 03
183 /
184 E^X
185 RCL 01
186 *
187 “Ye=”
188 ARCL X
189 PROMPT
190 RTN
191 END
e) Acknowledgement: Formulas for program and example taken from the book “Curve Fitting For Programmable Calculators – Third Edition” by William M. Kolb, 1984.
Find all posts by this user
Quote this message in a reply
11-14-2023, 04:01 PM
Post: #2
RE: Fit data to normal and lognormal distribution
Your code has nostalgic effect on me! I remember coding probability distributions that can be put in a linearized multiple regression (with 2 terms) form. That was in 1980/1981.

Cool code! Thanks!

Namir
Find all posts by this user
Quote this message in a reply
11-15-2023, 08:40 AM
Post: #3
RE: Fit data to normal and lognormal distribution
Glad to hear that you like it. It was fun for me programming it.
Thank you as well for your helpful suggestions for improvement, which I used.

Best

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




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