(50g) Normal Distribution

+- HP Forums (http://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (50g) Normal Distribution (/thread-11898.html)



(50g) Normal Distribution - John Keith - 12-03-2018 10:20 AM

I have been following these two threads and rather than trolling either with HP 50g programs I am starting this thread of normal distribution programs.

The function Q(x), the upper tail normal CDF, is implemented in the HP 50 as UTPN. It requires the mean on level 3, the variance on level 2, and x on level 1. Note that the HP Prime implements P(x), the lower-tail CDF as NORMALD_CDF. Either function can be obtained from the other by subtracting the value from 1 as detailed in the above linked threads. For example, the lower-tail normal CDF would simply be
Code:

\<< UNROT PICK3 ABS UTPN SWAP 0. > { 1. SWAP - } IFT
\>>

The normal PDF Z(x), which is implemented on the Prime as NORMALD, is not built in to the HP 50. It can be easily calculated with the following program:
Code:

\<< ROT - SWAP \v/ SWAP OVER / DUP SQ IP .5 * UNROT DUP IP SWAP FP \-> s i f
  \<< DUP NEG EXP SWAP i SQ .5 * - i f * - f SQ .5 * - EXP * 2.50662827463 s * /
  \>>
\>>

Next, a program to generate normally distributed random numbers. This program uses the Marsaglia polar method and returns two random numbers:
Code:

\<<
  WHILE RAND 2. * 1. - RAND 2. * 1. - DUP2 SQ SWAP SQ + DUP 1. \>=
  REPEAT DROP2 DROP
  END DUP LN -2. * SWAP / \v/ SWAP OVER * UNROT *
\>>

Finally, a somewhat useful example program that takes advantage of the fact that the above program returns two random numbers at a time. Given an integer or a list of array dimensions on level 1, the following program returns an array of normally distributed complex numbers. The program requires the above program stored in a variable named 'N2' in an accessible directory:
Code:

\<< DUPDUP \-> d
  \<< TYPE 5. SAME { 1. + \PILIST } IFT # 1d SWAP R\->B
    START N2 R\->C
    NEXT d \->ARRY
  \>>
\>>

I believe that all of the above programs will run on the 48g and 49g calculators but they have only been tested on the 50g.