List of usage examples for org.apache.commons.math3.distribution AbstractRealDistribution cumulativeProbability
double cumulativeProbability(double x);
From source file:com.ibm.bi.dml.runtime.functionobjects.ParameterizedBuiltin.java
/** * Helper function to compute distribution-specific cdf (both lowertail and uppertail) and inverse cdf. * /*from w w w . j av a 2 s . com*/ * @param dcode * @param params * @param inverse * @return * @throws MathArithmeticException * @throws DMLRuntimeException */ private double computeFromDistribution(ProbabilityDistributionCode dcode, HashMap<String, String> params, boolean inverse) throws MathArithmeticException, DMLRuntimeException { // given value is "quantile" when inverse=false, and it is "probability" when inverse=true double val = Double.parseDouble(params.get("target")); boolean lowertail = true; if (params.get("lower.tail") != null) { lowertail = Boolean.parseBoolean(params.get("lower.tail")); } AbstractRealDistribution distFunction = null; switch (dcode) { case NORMAL: double mean = 0.0, sd = 1.0; // default values for mean and sd String mean_s = params.get("mean"), sd_s = params.get("sd"); if (mean_s != null) mean = Double.parseDouble(mean_s); if (sd_s != null) sd = Double.parseDouble(sd_s); if (sd <= 0) throw new DMLRuntimeException( "Standard deviation for Normal distribution must be positive (" + sd + ")"); distFunction = new NormalDistribution(mean, sd); break; case EXP: double exp_rate = 1.0; // default value for 1/mean or rate if (params.get("rate") != null) exp_rate = Double.parseDouble(params.get("rate")); if (exp_rate <= 0) { throw new DMLRuntimeException( "Rate for Exponential distribution must be positive (" + exp_rate + ")"); } // For exponential distribution: mean = 1/rate distFunction = new ExponentialDistribution(1.0 / exp_rate); break; case CHISQ: if (params.get("df") == null) { throw new DMLRuntimeException( "" + "Degrees of freedom must be specified for chi-squared distribution " + "(e.g., q=qchisq(0.5, df=20); p=pchisq(target=q, df=1.2))"); } int df = UtilFunctions.parseToInt(params.get("df")); if (df <= 0) { throw new DMLRuntimeException( "Degrees of Freedom for chi-squared distribution must be positive (" + df + ")"); } distFunction = new ChiSquaredDistribution(df); break; case F: if (params.get("df1") == null || params.get("df2") == null) { throw new DMLRuntimeException("" + "Degrees of freedom must be specified for F distribution " + "(e.g., q = qf(target=0.5, df1=20, df2=30); p=pf(target=q, df1=20, df2=30))"); } int df1 = UtilFunctions.parseToInt(params.get("df1")); int df2 = UtilFunctions.parseToInt(params.get("df2")); if (df1 <= 0 || df2 <= 0) { throw new DMLRuntimeException( "Degrees of Freedom for F distribution must be positive (" + df1 + "," + df2 + ")"); } distFunction = new FDistribution(df1, df2); break; case T: if (params.get("df") == null) { throw new DMLRuntimeException( "" + "Degrees of freedom is needed to compute probabilities from t distribution " + "(e.g., q = qt(target=0.5, df=10); p = pt(target=q, df=10))"); } int t_df = UtilFunctions.parseToInt(params.get("df")); if (t_df <= 0) { throw new DMLRuntimeException( "Degrees of Freedom for t distribution must be positive (" + t_df + ")"); } distFunction = new TDistribution(t_df); break; default: throw new DMLRuntimeException("Invalid distribution code: " + dcode); } double ret = Double.NaN; if (inverse) { // inverse cdf ret = distFunction.inverseCumulativeProbability(val); } else if (lowertail) { // cdf (lowertail) ret = distFunction.cumulativeProbability(val); } else { // cdf (upper tail) // TODO: more accurate distribution-specific computation of upper tail probabilities ret = 1.0 - distFunction.cumulativeProbability(val); } return ret; }