![]() |
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. Table of ContentWe 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
Output
CDF at Z=1.5: 0.8222 Approach 2: Using Taylor Series ExpansionTaylor 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
Output
0.9331927822029031 |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |