HP Forums
HP 42s Program Normal Distibution - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: HP 42s Program Normal Distibution (/thread-22523.html)



HP 42s Program Normal Distibution - cylurian - 10-17-2024 05:06 AM

Does anyone know where I can find a working program for calculating the Normal Distribution for the HP 42s? I'm working with a DM42 right now. I tried the one from this link: https://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv020.cgi?read=196607, but it didn’t work on my DM42. Then, I checked out https://github.com/NicoB-codes/HP-42S-programs, and that didn’t work either. I’m looking for a program that can calculate both a lower and upper bound simultaneously, rather than the old approach we used in the 80s where it was just left tail or right tail.


RE: HP 42s Program Normal Distibution - Thomas Klemm - 10-18-2024 06:51 AM

(10-17-2024 05:06 AM)cylurian Wrote:  (…) but it didn’t work on my DM42.
(…) and that didn’t work either.

That is not very specific.
Describe what you did and what the result was.

(10-17-2024 05:06 AM)cylurian Wrote:  I’m looking for a program that can calculate both a lower and upper bound simultaneously, rather than the old approach we used in the 80s where it was just left tail or right tail.

The pdf of the normal distribution is:

\(
pdf(x)=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}
\)

This is the corresponding program for the HP-42S:
Code:
00 { 34-Byte Prgm }
01▸LBL "pdf"
02 MVAR "x"
03 RCL "x"
04 RCL- "μ"
05 RCL÷ "s"
06 X↑2
07 -2
08 ÷
09 E↑X
10 RCL÷ "s"
11 2
12 PI
13 ×
14 SQRT
15 ÷
16 END

We can then use ∫f(x) to integrate it.

Initialization

Store \(\mu\) and \(\sigma\) in registers "μ" and "s".

Example

\(\mu=0\)
\(\sigma=1\)

\(\int_1^2 pdf(x) \;\mathrm{d}x\)

FIX 06

0 STO "μ"
1 STO "s"

∫f(x)

Select ∫f(x) Program
pdf

Set Vars; Select ∫var
x

LLIM=1.000000
ULIM=2.000000
ACC=0.000001

∫=0.135905



This can then be used in a program to calculate the normal distribution:
Code:
00 { 26-Byte Prgm }
01▸LBL "N"
02 STO "ULIM"
03 R↓
04 STO "LLIM"
05 PGMINT "pdf"
06 INTEG "x"
07 END

Example

1 ENTER 2
XEQ "N"

0.135905



RE: HP 42s Program Normal Distibution - cylurian - 10-21-2024 09:41 PM

This worked out perfectly. Thanks. It worked on my DM 42. How would I be able to calculate a CDF with Lower Limits and Upper Limits and not mu = 0 and s = 1. thx.


RE: HP 42s Program Normal Distibution - Thomas Klemm - 10-21-2024 10:36 PM

(10-21-2024 09:41 PM)cylurian Wrote:  How would I be able to calculate a CDF with Lower Limits and Upper Limits and not mu = 0 and s = 1.

(10-18-2024 06:51 AM)Thomas Klemm Wrote:  Store \(\mu\) and \(\sigma\) in registers "μ" and "s".

You can also add them as menu variables to the program pdf:
Code:
00 { 40-Byte Prgm }
01▸LBL "pdf"
02 MVAR "x"
03 MVAR "μ"
04 MVAR "s"
05 RCL "x"
06 RCL- "μ"
07 RCL÷ "s"
08 X↑2
09 -2
10 ÷
11 E↑X
12 RCL÷ "s"
13 2
14 PI
15 ×
16 SQRT
17 ÷
18 END

Example

\(\mu=5\)
\(\sigma=2\)

\(\int_7^9 pdf(x) \;\mathrm{d}x\)

FIX 06

∫f(x)

Select ∫f(x) Program
pdf

Set Vars; Select ∫var
μ=5.000000
s=2.000000
x

LLIM=7.000000
ULIM=9.000000
ACC=0.000001

∫=0.135905



RE: HP 42s Program Normal Distibution - cylurian - 10-22-2024 05:04 AM

Ok, I got these to programs.

00 { 40-Byte Prgm }
01▸LBL "pdf"
02 MVAR "x"
03 MVAR "μ"
04 MVAR "s"
05 RCL "x"
06 RCL- "μ"
07 RCL÷ "s"
08 X↑2
09 -2
10 ÷
11 E↑X
12 RCL÷ "s"
13 2
14 PI
15 ×
16 SQRT
17 ÷
18 END

Then

00 { 40-Byte Prgm }
01▸LBL "integrate"
02 INPUT "μ"
03 INPUT "s"
04 INPUT "x1"
05 INPUT "x2"
06 ∫FN "pdf"
07 VIEW X
08 END

I know 06 ∫FN "pdf" is wrong, can you help me fix it? Thanks .


RE: HP 42s Program Normal Distibution - Thomas Klemm - 10-22-2024 05:58 AM

If you don't want to integrate pdf manually, you can omit the menu variable declarations using MVAR:
Code:
00 { 31-Byte Prgm }
01▸LBL "pdf"
02 RCL "x"
03 RCL- "μ"
04 RCL÷ "s"
05 X↑2
06 -2
07 ÷
08 E↑X
09 RCL÷ "s"
10 2
11 PI
12 ×
13 SQRT
14 ÷
15 END

You have to specify both LLIM and ULIM:
Code:
00 { 34-Byte Prgm }
01▸LBL "∫pdf"
02 INPUT "μ"
03 INPUT "s"
04 INPUT "LLIM"
05 INPUT "ULIM"
06 PGMINT "pdf"
07 INTEG "x"
08 END

Example

FIX 06

XEQ "∫pdf"

μ?0.000000

5 R/S

s?1.000000

2 R/S

LLIM?1.000000

7 R/S

ULIM?2.000000

9 R/S

0.135905



RE: HP 42s Program Normal Distibution - Thomas Klemm - 10-22-2024 06:44 AM

HP-42S Owner’s Manual
Using Integration in a Program, pp. 203


RE: HP 42s Program Normal Distibution - cylurian - 10-22-2024 06:58 PM

I did load both programs on my DM 42. I only ran the CDF program and type mu = 5, s = 2, LB = 3, UB = 7 and used the R/S button. Seems like it's on a loop, doesn't stop. Just keeps going. Any suggestions? Thanks.


RE: HP 42s Program Normal Distibution - Thomas Klemm - 10-22-2024 07:44 PM

Make sure to also set:

ACC=0.000001

The result should be:

0.682689



RE: HP 42s Program Normal Distibution - cylurian - 10-23-2024 04:41 AM

Thank you for your help with this code, Thomas. The program is now finalized and works on the DM42 to calculate the CDF of the Normal Distribution. By using the NCDF program (down below) in combination with the NPDF program (see below), you can calculate the CDF between two x-values. When running NCDF, it will prompt you to enter the mean, standard deviation, lower limit, and upper limit. The result offers an accuracy of approximately 10^-6. If you just want the PDF, then check out my last program pdf, it will ask you all the questions to find the PDF.

Code:

00 { 31-Byte Prgm }
01▸LBL "NPDF"
02 RCL "x"
03 RCL- "μ"
04 RCL÷ "s"
05 X↑2
06 -2
07 ÷
08 E↑X
09 RCL÷ "s"
10 2
11 PI
12 ×
13 SQRT
14 ÷
15 END

Code:

00 { 31-Byte Prgm }
01▸LBL "NPDF"
02 RCL "x"
03 RCL- "μ"
04 RCL÷ "s"
05 X↑2
06 -2
07 ÷
08 E↑X
09 RCL÷ "s"
10 2
11 PI
12 ×
13 SQRT
14 ÷
15 END

Code:

00 { 50-Byte Prgm }
01▸LBL "pdf"
02 MVAR "x"
03 MVAR "μ"
04 MVAR "s"
05 INPUT "x"
06 INPUT "μ"
07 INPUT "s"
08 RCL "x"
09 RCL- "μ"
10 RCL÷ "s"
11 X↑2
12 -2
13 ÷
14 E↑X
15 RCL÷ "s"
16 2
17 PI
18 ×
19 SQRT
20 ÷
21 END