%mathpiper,def="ZScoreToProbability"

/* 
    This function was adapted from the Javascript version of function
    that is located here:
    
    http://www.fourmilab.ch/rpkp/experiments/analysis/zCalc.js
    http://www.fourmilab.ch/rpkp/experiments/analysis/zCalc.html?
    
    The following JavaScript functions for calculating normal and
    chi-square probabilities and critical values were adapted by
    John Walker from C implementations
    written by Gary Perlman of Wang Institute, Tyngsboro, MA
    01879. Both the original C code and this JavaScript edition
    are in the public domain. 
*/



/* 
    POZ -- probability of normal z value

    Adapted from a polynomial approximation in:
    Ibbetson D, Algorithm 209
    Collected Algorithms of the CACM 1963 p. 616
    Note:
    This routine has six digit accuracy, so it is only useful for absolute
    z values <:= 6. For z values > to 6.0, poz() returns 1.0.
*/

ZScoreToProbability(zScore) :=
[
    zScore := N(zScore);
    
    Local( y, x, w, ZMAX, result);
    
    ZMAX := 6; // Maximum �z value
    
    if(zScore = 0.0)
    [
        x := 0.0;
    ]
    else
    [
        y := 0.5 * AbsN(zScore);
        
        if(y > ZMAX * 0.5)
        [
            x := 1.0;
        ]
        else if(y < 1.0)
        [
            w := y * y;
            x := ((((((((0.000124818987 * w
            - 0.001075204047) * w + 0.005198775019) * w
            - 0.019198292004) * w + 0.059054035642) * w
            - 0.151968751364) * w + 0.319152932694) * w
            - 0.531923007300) * w + 0.797884560593) * y * 2.0;
        ]
        else
        [
            y := y - 2.0;
            
            x := (((((((((((((-0.000045255659 * y
            + 0.000152529290) * y - 0.000019538132) * y
            - 0.000676904986) * y + 0.001390604284) * y
            - 0.000794620820) * y - 0.002034254874) * y
            + 0.006549791214) * y - 0.010557625006) * y
            + 0.011630447319) * y - 0.009279453341) * y
            + 0.005353579108) * y - 0.002141268741) * y
            + 0.000535310849) * y + 0.999936657524;
        ];
    ];
    
        
    If(zScore > 0.0 , result := (x + 1.0) * 0.5 , result := (1.0 - x) * 0.5);
    
    result;
];


%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output





%mathpiper_docs,name="ZScoreToProbability",categories="User Functions;Statistics & Probability",access="experimental"
*CMD ZScoreToProbability --- calculates the probability of a given z-score
*STD
*CALL
	ZScoreToProbability(z_score)

*PARMS

{z_score} -- a z-score value

*DESC

This function calculates the probability of a given z-score.

*E.G.
In> ZScoreToProbability(1.08)
Result: 0.8599289100

*SEE NormalDistribution,ProbabilityToZScore,ValueToZScore,ZScoreToValue,ConfidenceLevelToZScore
%/mathpiper_docs

    %output,preserve="false"
      
.   %/output


