# Normal Distribution

The normal (or Gaussian) distribution is a very commonly occurring continuous probability distribution. Normal distributions are extremely important in statistics and are often used in the natural and social sciences for real-valued random variables whose distributions are not known.

The normal distribution is immensely useful because of the central limit theorem, which states that, under mild conditions, the mean of many random variables independently drawn from the same distribution is distributed approximately normally, irrespective of the form of the original distribution. Physical quantities that are expected to be the sum of many independent processes often have a distribution very close to the normal.

For a given probability the square of the standardized normal deviate corresponds to Chi-square with one degree of freedom. That special relationship is utilized in this program source code, which calculates the precise probability P corresponding to a given value of the standardized normal deviate Z.

The one-sided probability corresponds to the area under the normal curve from plus infinity to Z.

The method applied in the source code is based on series expansion. The results are accurate to 18 decimals.

#COMPILE EXE #REGISTER NONE #DIM ALL ' FUNCTION ProbFromNormalDeviate(BYVAL Z AS EXT, BYVAL ED AS EXT) AS EXT LOCAL flag AS LONG, T, S, P AS EXT ' extended precision IF Z < 0 THEN flag = 1 ' negative Z Z = Z * Z P = PCHI2(Z,1) * 0.5 ' One-sided probability. Start with this. IF flag = 1 THEN P = 1.0 - P ' negative Z. Get complementary probability. P = P * ED ' Get one-sided or two-sided probability according to ED IF P > 1.0 THEN P = 1.0 ' never give a probability of more than one FUNCTION = P END FUNCTION ' FUNCTION PBMAIN LOCAL Result AS STRING, i1 AS LONG, P, OT AS EXT Result$ = INPUTBOX$("Enter Z , X-Sided (1 or 2)", _ "1- or 2-sided Probability from Normal Deviate Z") i1&=INSTR(Result$,",") OT=VAL(MID$(Result$,i1&+1)) IF OT=1.0 OR OT=2.0 THEN P = ProbFromNormalDeviate(VAL(LEFT$(Result$,i1&-1)), OT) MSGBOX "P="+FORMAT$(P," ##.##################" ),, _ "Normal Deviate Z="+LEFT$(Result$,i1&-1)+" "+STR$(OT)+"-sided" END IF END FUNCTION ' FUNCTION PCHI2(BYVAL X AS EXT, BYVAL F AS EXT) AS EXT ' ' FUNCTION PCHI2 - CALCULATES THE P-VALUE FROM ' THE CHI-SQUARE VALUE (X) AND THE DEGREES OF FREEDOM (F, must be integer) ' The probability is calculated using series expansions 26.4.4 and 26.4.5. ' In: Handbook of mathematical Functions. Editors: Abramowitz M & Stegun IA, ' Dover, New York, 1972. ' Original code by Erik Christensen ' LOCAL D, E, R, G, H, SL, S, T, P, C, SK, Q AS EXT ' extended precision Q = 1.0 IF X > 0 AND F > 0 AND ROUND(F, 0) = F THEN D = F E = D / 2.0 R = F - INT(F / 2.0) * 2.0 G = 1.0 IF R < 0.3 THEN ' F is even DO E = E - 1.0 IF E < 1.8 THEN EXIT LOOP G = G * E LOOP ELSE ' F is odd G = SQR(ATN(1.0) * 4.0) ' = sqrt(PI) DO E = E - 1.0 IF E < 0.3 THEN EXIT LOOP G = G * E LOOP END IF H = ((X / 2.0) ^ (D / 2.0) * EXP(-X / 2.0)) / (X * G) SL = H * 2.0 * X / D S = 0.0 T = 0.0 P = 1.0 DO D = D + 2.0 P = P * X / D S = S + P C = ABS(S - T) IF C < 1.0E-18 THEN EXIT LOOP T = S LOOP SK = (S + 1.0) * SL Q = 1.0 - SK IF Q < 1.0E-18 THEN Q = 1.0E-18 END IF FUNCTION = Q END FUNCTION