simple-statistics
Version:
44 lines (42 loc) • 1.23 kB
JavaScript
/**
* **[Gaussian error function](http://en.wikipedia.org/wiki/Error_function)**
*
* The `errorFunction(x/(sd * Math.sqrt(2)))` is the probability that a value in a
* normal distribution with standard deviation sd is within x of the mean.
*
* This function returns a numerical approximation to the exact value.
* It uses Horner's method to evaluate the polynomial of τ (tau).
*
* @param {number} x input
* @return {number} error estimation
* @example
* errorFunction(1).toFixed(2); // => '0.84'
*/
function errorFunction(x) {
const t = 1 / (1 + 0.5 * Math.abs(x));
const tau =
t *
Math.exp(
-x * x +
((((((((0.17087277 * t - 0.82215223) * t + 1.48851587) * t -
1.13520398) *
t +
0.27886807) *
t -
0.18628806) *
t +
0.09678418) *
t +
0.37409196) *
t +
1.00002368) *
t -
1.26551223
);
if (x >= 0) {
return 1 - tau;
} else {
return tau - 1;
}
}
export default errorFunction;