Recently, I’ve decided that I want to write my own statistics package in Java (I’m too bored for my own good). Originally, I wanted to write my own classes to handle the probability calculations under curves. However, this became a bit much when it came time to do f and student-T distributions. As such, I gave up and am now using a Java class provided by CERN laboratories under the GNU license.

Before I gave up, I wrote a simple method for calculating the area under normal distribution curves based on Z statistics (the result of a statistical Z-Test). It uses the Error Function to approximate left-tail probability (-infinity -> Z-statistic).

/* * Normal probability method * @author Nicholas Pier */ static public double getCDF(double x){ double p = .5 * (1 + getERF( x / Math.sqrt(2))); return p; } static private double getERF(double x){ double a = (8 * (Math.PI - 3)) / (3 * Math.PI * (4- Math.PI)); double erf = (x / Math.abs(x)) * Math.sqrt(1 - Math.exp(-Math.pow(x, 2) *(((4/Math.PI) + (a * Math.pow(x, 2))) / (1 + (a * Math.pow(x, 2)))))); return erf; } |

Just for efficiency’s sake, here’s a few modifications to your code:

static public double getCDF(double x){

final double isqrt2 = 1.0 / Math.sqrt(2);

double p = 0.5 * (1 + getERF( x * isqrt2 ) );

return p;

}

static private double getERF(double x) {

final double a = (8 * (Math.PI - 3)) / (3 * Math.PI * (4- Math.PI));

double sq = x * x;

double asq = a * sq;

double erf = Math.signum(x) * Math.sqrt(1 - Math.exp(-sq *(((4/Math.PI) + asq) / (1 + asq))));

return erf;

}

It ensures Java does a little bit more inlining, and there’s less method call overhead.