Std normal cdf, normal cdf or error function

Can any of these functions, the standard cumulative distribution function, the normal cumulative distribution function, or the error function be reliably calculated using javascript?

I would like to have a fully client solution, given the lack of PECL availability on Windows.

Please show me how.

Thank you very much in advance!

+4
source share
1 answer

I use these implementations:

function cdf(x, mean, variance) { return 0.5 * (1 + erf((x - mean) / (Math.sqrt(2 * variance)))); } function erf(x) { // save the sign of x var sign = (x >= 0) ? 1 : -1; x = Math.abs(x); // constants var a1 = 0.254829592; var a2 = -0.284496736; var a3 = 1.421413741; var a4 = -1.453152027; var a5 = 1.061405429; var p = 0.3275911; // A&S formula 7.1.26 var t = 1.0/(1.0 + p*x); var y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x); return sign * y; // erf(-x) = -erf(x); } 

Sources:

The error is related to the erf implementation, I think, although this is not very important for me, so you might want to look deeper into it when the error is important.

Obviously, to get the standard normal cdf, you pass the average value = 0 and variance = 1 to the cdf function, i.e.

 function std_n_cdf(x) { return cdf(x, 0, 1); } 
+19
source

All Articles