A program for Normal Distribution on the 42s



#2

I couldn't find one on the net, so I converted the one on my 35s for the 42s. It's my first 42s program.



SET = Set mean and std dev.
LT = Lower Tail
LTI = Lower Tail Inverse (put in probability, get X)
UT = Upper Tail
UTI = Upper Tail Inverse



00 { 372-Byte Prgm }
01 LBL "NRML"
02 CLMENU
03 "SET"
04 KEY 1 XEQ "NRMSET"
05 "LT"
06 KEY 2 XEQ "NRMLT"
07 "UT"
08 KEY 3 XEQ "NRMUT"
09 "LTI"
10 KEY 4 XEQ "NRMLTI"
11 "UTI"
12 KEY 5 XEQ "NRMUTI"
13 MENU
14 STOP
15 GTO "NRML"
16 LBL "NRMSET"
17 CLMENU
18 0
19 STO "M"
20 INPUT "M"
21 1
22 STO "S"
23 INPUT "S"
24 RTN
25 LBL "NRMUT"
26 CLMENU
27 INPUT "X"
28 XEQ "NRMQ"
29 STO "Q"
30 VIEW "Q"
31 RTN
32 LBL "NRMLT"
33 CLMENU
34 INPUT "X"
35 XEQ "NRMQ"
36 +/-
37 1
38 +
39 STO "Q"
40 VIEW "Q"
41 RTN
42 LBL "NRMUTI"
43 CLMENU
44 INPUT "Q"
45 RCL "M"
46 STO "X"
47 XEQ "NRMT"
48 RTN
49 LBL "NRMLTI"
50 CLMENU
51 INPUT "Q"
52 1
53 RCL "Q"
54 -
55 STO "Q"
56 RCL "M"
57 STO "X"
58 XEQ "NRMT"
59 RTN
60 LBL "NRMT"
61 XEQ "NRMQ"
62 RCL- "Q"
63 RCL "X"
64 STO "D"
65 ROLLDOWN
66 XEQ "NRMF"
67 RCL/ "T"
68 /
69 STO+ "X"
70 ABS
71 0.0001
72 X<Y?
73 GTO "NRMT"
74 RCL "X"
75 VIEW "X"
76 RTN
77 LBL "NRMQ"
78 RCL "M"
79 STO "LLIM"
80 RCL "X"
81 STO "ULIM"
82 PGMINT "NRMF"
83 INTEG "D"
84 2
85 PI
86 x
87 SQRT
88 RCLx "S"
89 STO "T"
90 \
91 +/-
92 0.5
93 +
94 RTN
95 LBL "NRMF"
96 MYVAR "D"
97 RCL "D"
98 RCL- "M"
99 RCL\ "S"
100 X^2
101 2
102 \
103 +/-
104 e^X
105 RTN

#3

Very interesting. You could add this as an article in the museum so it doesn't get lost in the heaps of forum archives.

No I remember why the 42 is great and also how painful typing all these lines can be!

#4

Honestly, I would suggest a different approach, i.e. rational approximations. Expecially for the quantiles. A mid-precision (3;4) type is able to return a result with absolute error less than 5 E-6 within one second (!) on the 35s. The 42s should be able to do the same.

Would you try the following examples with your program and see how long it takes?

Mean = 0 and standard deviation = 1.

 p = 0,1     =>   z =  1,28155
p = 0,01 => z = 2,32635
p = 0,0001 => z = 3,71902
p = 1E-10 => z = 6,36134
p = 1E-400 => z = 42,81023
Dieter

#5

Will do, but it is quite slow, 10's of seonds. Can you link me to some theory or an algorithm on rational approximations? Faster would be. Better!

Also, the programmable menus is a big bonus of the 42s in comparison to the 35s.

Daniel.


#6

Dieter and I had a long long discussion about ways to implement the quantile (& other statistical) functions. I archived many of the tidbits as part of the documentation for the 34S.

- Pauli

#7

As Pauli already pointed out, some of the information that went into the 34s for the statistical distributions is documented, both here in the forum and in the text files that come with the software package.

However, for everyday use on the 35s I designed a simple (3;4) rational approximation with a a maximum absolute error of approx. 5 E-6 so that it will return five valid decimals (+/- 1 ULP). It was designed for p down to 1E-500, i.e. the full working range of the 35s, 42s and others. With a different set of coefficients even higher precision over a smaller range may be achieved. I currently use the following program (slightly modified, that is) to evaluate the quantile: input p, output z.

  LBL Z
STO P
ENTER
SGN
RCL-P
x>y?
x<>y ; r := min(p, 1-p)
LN
ENTER
+
+/-
SQRT
STO T ; t := sqrt(-2 ln r)
0,010285 ; user Horner's method
RCL*T ; for polynomial in t
0,694738
+
RCL*T
4,629359
+
RCL*T
2,94666537818
+ ; nominator of rat. approx.
0,0014392
RCL*T
0,158567
+
RCL*T
1,87944
+
RCL*T
3,47987
+
RCL*T
1
+ ; denominator of rat. approx.
/
-
0,5 ; set negative sign for p > 0,5)
RCL-P ; (or the other way round, if you prefer)
SGN
*
FIX 5
RND ; make sure that only valid digits are returned ;-)
RTN

The normal cdf itself can easily be calculated with the well-known series expansion for smaller z and a continued fraction approach for larger z. On a 12-digit calculator the result in most cases is correct to 11 digits, often even 12. On my 35s the algorithm usually takes between 1 and 4 seconds. IF you don't need that level of precision, there are polynomial and rational approximations for this case as well.

The result of the cdf routine can be used to improve the quantile so that with one single iteration usually 10-12 correct digits are returned. Instead of good old Newton's method I'm using one step of a Hastings iteration for this, so that finally the near-full-precision quantile is returned in 5 seconds or less. The details of this approach have been discussed here as well.

So there are three normal distribution routines on my 35s: One that returns the (almost) exact cdf, a second that returns a more-than-adaequate five-digit approximation for the quantile, and finally a third one that turns this mid-precision result into one with (almost) full precision. The latter two can be combined to return the (almost) exact quantile in 5 seconds or less.

Dieter


Possibly Related Threads…
Thread Author Replies Views Last Post
  HP Prime: run a program in another program Davi Ribeiro de Oliveira 6 2,733 11-11-2013, 08:28 PM
Last Post: Davi Ribeiro de Oliveira
  Maximum number of program steps in HP-42S, 33S, and 35S? Walter B 3 1,694 12-18-2012, 03:44 PM
Last Post: Eric Smith
  New HP-82240B emulator in WP34s distribution pascal_meheut 6 2,182 10-07-2012, 05:07 PM
Last Post: Christoph Giesselink
  42s program question David Persinger (US) 5 1,817 08-09-2012, 06:34 AM
Last Post: David Persinger (US)
  Yet another Normal quantile function for the 34s Dieter 13 3,681 08-06-2012, 09:15 PM
Last Post: Paul Dale
  [WP34S] Inverse F Distribution--Danged "Domain Error" Issue is Back Les Wright 16 4,609 05-23-2012, 10:28 PM
Last Post: Les Wright
  Summary: Normal CDF and quantile function Dieter 3 1,444 05-13-2012, 02:20 AM
Last Post: Paul Dale
  [WP34S] Curious Bug in Inverse Normal Function Les Wright 61 13,136 05-01-2012, 02:44 AM
Last Post: Paul Dale
  42S program review Bill Colisch 4 1,514 04-25-2012, 05:58 PM
Last Post: Bill (Smithville, NJ)
  [WP34S] Bug in Inverse CDF for F-distribution Les Wright 19 4,393 04-15-2012, 11:17 AM
Last Post: Dieter

Forum Jump: