12-03-2018, 04:20 PM
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.
NOTE: The first two programs below should be considered obsolete. They are inaccurate for large values of x as pointed out by Dieter and Albert Chan in following posts. I am leaving them here for illustrative purposes only. The programs in posts 3 and 18 should be used instead.
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 (but inaccuratly) be
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 (but inaccurately) calculated with the following program:
UPDATE: Yes the normal PDF is included. It's called NDIST. Sorry to all for the confusion.
Next, a program to generate normally distributed random numbers. This program uses the Marsaglia polar method and returns two random numbers:
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:
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.
NOTE: The first two programs below should be considered obsolete. They are inaccurate for large values of x as pointed out by Dieter and Albert Chan in following posts. I am leaving them here for illustrative purposes only. The programs in posts 3 and 18 should be used instead.
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 (but inaccuratly) be
Code:
\<< UTPN 1. SWAP - \>>
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 (but inaccurately) calculated with the following program:
Code:
\<< UNROT 2. * UNROT - SQ OVER / NEG EXP SWAP 3.14159265359 * \v/ /
\>>
UPDATE: Yes the normal PDF is included. It's called NDIST. Sorry to all for the confusion.
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.