CFLib.org – Common Function Library Project

NormDist(x, mean, sd)

Last updated June 14, 2003

author

Rob Ingram

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Calculates the normal distribution for a given mean and standard deviation with cumulative=true. Equivalent to the MS Excel function NORMDIST(x,mean,standard_dev, 1). This code is based on the C++ version by Oliver Maag (http://www.alina.ch/oliver/faq-excel-normdist.shtml)

Return Values:
Returns a number.

Example:

<cfset val = 4.2>
<cfset mean = 4.7>
<cfset SD = 0.65>
<cfset nDist = NormDist(val, mean, SD)>
    
<cfoutput>#nDist#</cfoutput>

Parameters:

Name Description Required
x Value to compute the cumulative normal distribution for. Yes
mean Mean value. Yes
sd Standard deviation. Yes

Full UDF Source:

/**
 * Calculates the normal distribution for a given mean and standard deviation with cumulative=true
 * 
 * @param x      Value to compute the cumulative normal distribution for. (Required)
 * @param mean      Mean value. (Required)
 * @param sd      Standard deviation. (Required)
 * @return Returns a number. 
 * @author Rob Ingram (rob.ingram@broadband.co.uk) 
 * @version 1, June 14, 2003 
 */
function NormDist(x, mean, sd) {
    var res = 0.0;
    var x2 = 0.0;
    var oor2pi = 0.0;
    var t = 0.0;
    
    x2 = (x - mean) / sd;
    if (x2 eq 0) res = 0.5;
    else
    {
        oor2pi = 1/(sqr(2.0 * 3.14159265358979323846));
        t = 1 / (1.0 + 0.2316419 * abs(x2));
        t = t * oor2pi * exp(-0.5 * x2 * x2) 
             * (0.31938153   + t 
             * (-0.356563782 + t
             * (1.781477937  + t 
             * (-1.821255978 + t * 1.330274429))));
        if (x2 gte 0)
        {
            res = 1.0 - t;
        }
        else
        {
            res = t;
        }
    }
    return res;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson