Search in sources :

Example 6 with AbstractRealDistribution

use of org.apache.commons.math3.distribution.AbstractRealDistribution in project gatk-protected by broadinstitute.

the class AdaptiveMetropolisSampler method sample.

public double sample(final RandomGenerator rng, final Function<Double, Double> logPDF) {
    Utils.nonNull(rng);
    Utils.nonNull(logPDF);
    final AbstractRealDistribution normal = new NormalDistribution(rng, 0, 1);
    final double proposal = xCurrent + stepSize * normal.sample();
    final double acceptanceProbability = (proposal < lowerBound || upperBound < proposal) ? 0 : Math.min(1, Math.exp(logPDF.apply(proposal) - logPDF.apply(xCurrent)));
    //adjust stepSize larger/smaller to decrease/increase the acceptance rate
    final double correctionFactor = (acceptanceProbability - optimalAcceptanceRate) * adjustmentRate * (timeScale / (timeScale + iteration));
    stepSize *= Math.exp(correctionFactor);
    iteration++;
    return rng.nextDouble() < acceptanceProbability ? proposal : xCurrent;
}
Also used : AbstractRealDistribution(org.apache.commons.math3.distribution.AbstractRealDistribution) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution)

Example 7 with AbstractRealDistribution

use of org.apache.commons.math3.distribution.AbstractRealDistribution 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
 */
private static double computeFromDistribution(ProbabilityDistributionCode dcode, HashMap<String, String> params, boolean inverse) {
    // 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)

Example 8 with AbstractRealDistribution

use of org.apache.commons.math3.distribution.AbstractRealDistribution in project 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
 */
private static double computeFromDistribution(ProbabilityDistributionCode dcode, HashMap<String, String> params, boolean inverse) {
    // 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)8 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)6 AbstractIntegerDistribution (org.apache.commons.math3.distribution.AbstractIntegerDistribution)4 EnumeratedDistribution (org.apache.commons.math3.distribution.EnumeratedDistribution)3 ValueType (io.druid.segment.column.ValueType)2 ArrayList (java.util.ArrayList)2 ChiSquaredDistribution (org.apache.commons.math3.distribution.ChiSquaredDistribution)2 ExponentialDistribution (org.apache.commons.math3.distribution.ExponentialDistribution)2 FDistribution (org.apache.commons.math3.distribution.FDistribution)2 TDistribution (org.apache.commons.math3.distribution.TDistribution)2 UniformRealDistribution (org.apache.commons.math3.distribution.UniformRealDistribution)2 ZipfDistribution (org.apache.commons.math3.distribution.ZipfDistribution)2 Pair (org.apache.commons.math3.util.Pair)2 ValueType (org.apache.druid.segment.column.ValueType)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 UniformIntegerDistribution (org.apache.commons.math3.distribution.UniformIntegerDistribution)1