The Museum of HP Calculators

HP Forum Archive 20

[ Return to Index | Top of Index ]

A program for Normal Distribution on the 42s
Message #1 Posted by snaggs on 19 Sept 2011, 8:50 p.m.

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
      
Re: A program for Normal Distribution on the 42s
Message #2 Posted by Ángel Martin on 21 Sept 2011, 1:32 a.m.,
in response to message #1 by snaggs

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!

      
Re: A program for Normal Distribution on the 42s
Message #3 Posted by Dieter on 21 Sept 2011, 1:48 p.m.,
in response to message #1 by snaggs

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
            
Re: A program for Normal Distribution on the 42s
Message #4 Posted by snaggs on 21 Sept 2011, 6:46 p.m.,
in response to message #3 by Dieter

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.

                  
Re: A program for Normal Distribution on the 42s
Message #5 Posted by Paul Dale on 21 Sept 2011, 7:25 p.m.,
in response to message #4 by snaggs

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

                  
Re: A program for Normal Distribution on the 42s
Message #6 Posted by Dieter on 22 Sept 2011, 2:19 p.m.,
in response to message #4 by snaggs

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


[ Return to Index | Top of Index ]

Go back to the main exhibit hall