NormalDist { public double cdf(double mean, double std, double x) { double zscore = (x - mean) / std; if (zscore > MAXZSCORE) { return 1.0; } if (zscore < MINZSCORE) { return 0.0; } double exactEntry = zscore / GRANULARITY + LUT_OFFSET; int lowerEntry = (int) FastMath.floor(exactEntry); int higherEntry = (int) FastMath.ceil(exactEntry); if (lowerEntry == higherEntry) { return CDF_LUT[lowerEntry]; } else { return CDF_LUT[lowerEntry] + ((exactEntry - lowerEntry) * (CDF_LUT[higherEntry] - CDF_LUT[lowerEntry])); } } NormalDist(); double cdf(double mean, double std, double x); static final double GRANULARITY; static final double MAXZSCORE; static final double MINZSCORE; static final double LUT_OFFSET; static final double[] CDF_LUT; }
@Test public void testCDF() throws Exception { NormalDist dist = new NormalDist(); double cdf; cdf = dist.cdf(0.0, 1.0, 0.5); assertEquals(0.6915, cdf, 0.001); cdf = dist.cdf(5.0, 2.0, 4.0); assertEquals(0.3085, cdf, 0.001); cdf = dist.cdf(0.0, 1.0, 0.755); assertEquals(0.7749, cdf, 0.001); cdf = dist.cdf(0.0, 1.0, 4.0); assertEquals(1.0, cdf, 0.001); cdf = dist.cdf(0.0, 1.0, -4.0); assertEquals(0.0, cdf, 0.001); }