Post Reply 
HP 42s Program Normal Distibution
10-17-2024, 05:06 AM
Post: #1
HP 42s Program Normal Distibution
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...ead=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.
Find all posts by this user
Quote this message in a reply
10-18-2024, 06:51 AM (This post was last modified: 10-18-2024 08:09 AM by Thomas Klemm.)
Post: #2
RE: HP 42s Program Normal Distibution
(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
Find all posts by this user
Quote this message in a reply
10-21-2024, 09:41 PM (This post was last modified: 10-21-2024 10:06 PM by cylurian.)
Post: #3
RE: HP 42s Program Normal Distibution
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.
Find all posts by this user
Quote this message in a reply
10-21-2024, 10:36 PM
Post: #4
RE: HP 42s Program Normal Distibution
(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
Find all posts by this user
Quote this message in a reply
10-22-2024, 05:04 AM (This post was last modified: 10-22-2024 05:30 AM by cylurian.)
Post: #5
RE: HP 42s Program Normal Distibution
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 .
Find all posts by this user
Quote this message in a reply
10-22-2024, 05:58 AM
Post: #6
RE: HP 42s Program Normal Distibution
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
Find all posts by this user
Quote this message in a reply
10-22-2024, 06:44 AM
Post: #7
RE: HP 42s Program Normal Distibution
HP-42S Owner’s Manual
Using Integration in a Program, pp. 203
Find all posts by this user
Quote this message in a reply
10-22-2024, 06:58 PM (This post was last modified: 10-22-2024 07:01 PM by cylurian.)
Post: #8
RE: HP 42s Program Normal Distibution
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.
Find all posts by this user
Quote this message in a reply
10-22-2024, 07:44 PM
Post: #9
RE: HP 42s Program Normal Distibution
Make sure to also set:

ACC=0.000001

The result should be:

0.682689
Find all posts by this user
Quote this message in a reply
10-23-2024, 04:41 AM
Post: #10
RE: HP 42s Program Normal Distibution
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
Find all posts by this user
Quote this message in a reply
Post Reply 




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