Search in sources :

Example 1 with TDistribution

use of org.apache.commons.math3.distribution.TDistribution in project incubator-systemml by apache.

the class ParameterizedBuiltin method computeFromDistribution.

/**
	 * Helper function to compute distribution-specific cdf (both lowertail and uppertail) and inverse cdf.
	 * 
	 * @param dcode probablility distribution code
	 * @param params map of parameters
	 * @param inverse true if inverse
	 * @return cdf or inverse cdf
	 * @throws MathArithmeticException if MathArithmeticException occurs
	 * @throws DMLRuntimeException if DMLRuntimeException occurs
	 */
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:
            // default values for mean and sd
            double mean = 0.0, sd = 1.0;
            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:
            // default value for 1/mean or rate
            double exp_rate = 1.0;
            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;
}
Also used : AbstractRealDistribution(org.apache.commons.math3.distribution.AbstractRealDistribution) ChiSquaredDistribution(org.apache.commons.math3.distribution.ChiSquaredDistribution) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) ExponentialDistribution(org.apache.commons.math3.distribution.ExponentialDistribution) TDistribution(org.apache.commons.math3.distribution.TDistribution) FDistribution(org.apache.commons.math3.distribution.FDistribution) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

AbstractRealDistribution (org.apache.commons.math3.distribution.AbstractRealDistribution)1 ChiSquaredDistribution (org.apache.commons.math3.distribution.ChiSquaredDistribution)1 ExponentialDistribution (org.apache.commons.math3.distribution.ExponentialDistribution)1 FDistribution (org.apache.commons.math3.distribution.FDistribution)1 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)1 TDistribution (org.apache.commons.math3.distribution.TDistribution)1 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)1