How to usethe Error Function (erf) in Javascript
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)}`); |
CDF at Z=1.5: 0.8222
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.
Table of Content
- Using the Error Function (erf)
- Using Taylor Series Expansion
We will explore all the above methods along with their basic implementation with the help of examples.