/** * The calculation is done here. Example: From 1.96 (Z-score) to 0.975 (P-value) From -1.96 * (Z-score) to 0.025 * * @return double a p value */ public static double cumulativeProbability(Distribution obj, Number n) { NormalDistribution dist = new NormalDistributionImpl(); double lQuantile = 0; try { lQuantile = dist.cumulativeProbability(obj.zScore(n)); } catch (MathException e) { e.printStackTrace(); } return lQuantile; }
// return Phi(z) = standard Gaussian cdf using Taylor approximation public static double Phi(double z) throws MathException { return nd.cumulativeProbability(z); /* if (z < -8.0) return 0.0; if (z > 8.0) return 1.0; double sum = 0.0, term = z; int iter = 0; for (int i = 3; sum + term != sum && iter++ < 500; i += 2) { sum = sum + term; term = term * z * z / i; } return 0.5 + sum * phi(z);*/ }
// Compute z such that Phi(z) = y via bisection search public static double PhiInverse(double y) throws MathException { return nd.inverseCumulativeProbability(y); // return PhiInverse(y, .00000001, -8, 8); }
/** * @param param mean * @param param2 standard deviation * @return normal distribution */ NormalDistribution getNormalDistribution(double param, double param2) { if (normal == null || normal.getMean() != param || normal.getStandardDeviation() != param2) normal = new NormalDistributionImpl(param, param2); return normal; }