Skip to content

Instantly share code, notes, and snippets.

@scottlingran
Created November 29, 2016 23:33
Show Gist options
  • Save scottlingran/e43d411ae74b95f73c17787705a6caf8 to your computer and use it in GitHub Desktop.
Save scottlingran/e43d411ae74b95f73c17787705a6caf8 to your computer and use it in GitHub Desktop.
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment