A program for Normal Distribution on the 42s - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: A program for Normal Distribution on the 42s (/thread-196607.html) A program for Normal Distribution on the 42s - snaggs - 09-19-2011 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 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 - snaggs - 09-21-2011 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 - Paul Dale - 09-21-2011 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 - Dieter - 09-22-2011 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