Horje
JavaScript Program for Normal CDF Calculator

In this article, we will discuss a normal CDF calculator in JavaScript. A CDF calculator computes the Cumulative Distribution Function (CDF) for a given statistical variable. It indicates the probability that the variable’s value is less than or equal to a specified point, calculating the CDF is essential for understanding the probabilities associated with a normal distribution.

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using the Error Function (erf)

Using the Error Function (erf) is a mathematical approach commonly employed to solve problems related to probability and statistics. The formula CDF(x) = 0.5 * (1 + erf(x / sqrt(2))) transforms input values into probabilities, aiding statistical analysis.

Syntax:

function name(x, mean, stdDev) 
{
    // code
};
  const z = (x - mean) / (stdDev * Math.sqrt(2));
  return 0.5 * (1 + erf(z));
}

Example: In this example, we are using the above-explained approach.

Javascript

function GFG(x) {
    const a1 = 0.48592;
    const a2 = -1.14736;
    const a3 = 2.52741;
    const a4 = -1.45527;
    const a5 = 4.256;
    const A = 0.37711;
    const s = (x < 0) ? -1 : 1;
    x = Math.abs(x) / Math.sqrt(2.0);
    const B = 1.0 / (1.0 + A * x);
    const C = 1.0 - (((((a5 * B + a4)
        * B + a3) * B + a2) * B + a1) * B)
        * Math.exp(-x * x);
    return 0.5 * (1.0 + s * C);
}
const Z = 1.5;
const D1 = GFG(Z);
console.log(`CDF at Z=${Z}: ${D1.toFixed(4)}`);

Output

CDF at Z=1.5: 0.8222

Approach 2: Using Taylor Series Expansion

Taylor series expansion approximates normal CDF. Calculate terms based on input x, coefficients, and constants. Return result or its complement for x > 0.

Syntax:

function GFG(x) {
    const T = 1 / (1 + 0.2316419 * Math.abs(x));
    const cd = D * T * (0.3193815 + T * (-0.3565638 + T * (1.781478 + T * (-1.821256 + T * 1.330274))));
    return cd;
};

Example : In this example we are using the above-explained approach.

Javascript

function GFG(x) {
    const T = 
        1 / (1 + 0.2316419 * Math.abs(x));
    const D = 
        0.3989423 * Math.exp(-x * x / 2);
    const cd = 
        D * T * (0.3193815 + T * (-0.3565638 + T *
            (1.781478 + T * (-1.821256 + T * 1.330274))));
    if (x > 0) return 1 - cd;
    return cd;
}
console.log(GFG(1.5));

Output

0.9331927822029031



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Validate String Date Format in JavaScript ? How to Validate String Date Format in JavaScript ?
JavaScript Program to Find the Most Frequently Occurring Element in an Array JavaScript Program to Find the Most Frequently Occurring Element in an Array
Map() vs Filter() in Javascript Map() vs Filter() in Javascript
JavaScript Program to Swap Characters in a String JavaScript Program to Swap Characters in a String
JavaScript Program for Pattern Matching for Switch JavaScript Program for Pattern Matching for Switch

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13