Search in sources :

Example 11 with NormalDistribution

use of org.apache.commons.math3.distribution.NormalDistribution in project SIMVA-SoS by SESoS.

the class MCIResponseWorld method generateExpectedPatientsMap.

private void generateExpectedPatientsMap() {
    NormalDistribution xND = new NormalDistribution(MAP_SIZE.getLeft() / 2, MAP_SIZE.getLeft() / 4);
    NormalDistribution yND = new NormalDistribution(MAP_SIZE.getRight() / 2, MAP_SIZE.getRight() / 4);
    for (int x = 0; x < MAP_SIZE.getLeft(); x++) for (int y = 0; y < MAP_SIZE.getRight(); y++) {
        double xProb = xND.cumulativeProbability(x) - xND.cumulativeProbability(x - 1);
        double nX = xProb * MAP_SIZE.getLeft() * MAP_SIZE.getRight();
        double yProb = xND.cumulativeProbability(y) - xND.cumulativeProbability(y - 1);
        int nXY = (int) Math.round(yProb * nX);
        this.expectedPatientsMap.setValue(x, y, nXY);
    }
}
Also used : NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution)

Example 12 with NormalDistribution

use of org.apache.commons.math3.distribution.NormalDistribution 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 13 with NormalDistribution

use of org.apache.commons.math3.distribution.NormalDistribution in project pyramid by cheng-li.

the class RegressionSynthesizer method univarStepFeatureNoise.

public RegDataSet univarStepFeatureNoise() {
    NormalDistribution featureNoise = new NormalDistribution(0, 0.1);
    RegDataSet dataSet = RegDataSetBuilder.getBuilder().numDataPoints(numDataPoints).numFeatures(1).dense(true).missingValue(false).build();
    for (int i = 0; i < numDataPoints; i++) {
        double featureValue = Sampling.doubleUniform(0, 1);
        double label;
        if (featureValue > 0.5) {
            label = 0.7;
        } else {
            label = 0.2;
        }
        label += noise.sample();
        featureValue += featureNoise.sample();
        dataSet.setFeatureValue(i, 0, featureValue);
        dataSet.setLabel(i, label);
    }
    return dataSet;
}
Also used : NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) RegDataSet(edu.neu.ccs.pyramid.dataset.RegDataSet)

Example 14 with NormalDistribution

use of org.apache.commons.math3.distribution.NormalDistribution in project pyramid by cheng-li.

the class RegressionSynthesizer method gaussianMixture.

public RegDataSet gaussianMixture() {
    NormalDistribution leftGaussian = new NormalDistribution(0.2, 0.01);
    NormalDistribution rightGaussian = new NormalDistribution(0.7, 0.1);
    RegDataSet dataSet = RegDataSetBuilder.getBuilder().numDataPoints(numDataPoints).numFeatures(1).dense(true).missingValue(false).build();
    for (int i = 0; i < numDataPoints; i++) {
        double featureValue = Sampling.doubleUniform(0, 1);
        double label;
        if (featureValue > 0.5) {
            label = leftGaussian.sample();
        } else {
            label = rightGaussian.sample();
        }
        dataSet.setFeatureValue(i, 0, featureValue);
        dataSet.setLabel(i, label);
    }
    return dataSet;
}
Also used : NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) RegDataSet(edu.neu.ccs.pyramid.dataset.RegDataSet)

Example 15 with NormalDistribution

use of org.apache.commons.math3.distribution.NormalDistribution in project druid by druid-io.

the class BenchmarkColumnValueGenerator method initDistribution.

private void initDistribution() {
    BenchmarkColumnSchema.ValueDistribution distributionType = schema.getDistributionType();
    ValueType type = schema.getType();
    List<Object> enumeratedValues = schema.getEnumeratedValues();
    List<Double> enumeratedProbabilities = schema.getEnumeratedProbabilities();
    List<Pair<Object, Double>> probabilities = new ArrayList<>();
    switch(distributionType) {
        case SEQUENTIAL:
            // not random, just cycle through numbers from start to end, or cycle through enumerated values if provided
            distribution = new SequentialDistribution(schema.getStartInt(), schema.getEndInt(), schema.getEnumeratedValues());
            break;
        case UNIFORM:
            distribution = new UniformRealDistribution(schema.getStartDouble(), schema.getEndDouble());
            break;
        case DISCRETE_UNIFORM:
            if (enumeratedValues == null) {
                enumeratedValues = new ArrayList<>();
                for (int i = schema.getStartInt(); i < schema.getEndInt(); i++) {
                    Object val = convertType(i, type);
                    enumeratedValues.add(val);
                }
            }
            // give them all equal probability, the library will normalize probabilities to sum to 1.0
            for (int i = 0; i < enumeratedValues.size(); i++) {
                probabilities.add(new Pair<>(enumeratedValues.get(i), 0.1));
            }
            distribution = new EnumeratedTreeDistribution<>(probabilities);
            break;
        case NORMAL:
            distribution = new NormalDistribution(schema.getMean(), schema.getStandardDeviation());
            break;
        case ROUNDED_NORMAL:
            NormalDistribution normalDist = new NormalDistribution(schema.getMean(), schema.getStandardDeviation());
            distribution = new RealRoundingDistribution(normalDist);
            break;
        case ZIPF:
            int cardinality;
            if (enumeratedValues == null) {
                Integer startInt = schema.getStartInt();
                cardinality = schema.getEndInt() - startInt;
                ZipfDistribution zipf = new ZipfDistribution(cardinality, schema.getZipfExponent());
                for (int i = 0; i < cardinality; i++) {
                    probabilities.add(new Pair<>((Object) (i + startInt), zipf.probability(i)));
                }
            } else {
                cardinality = enumeratedValues.size();
                ZipfDistribution zipf = new ZipfDistribution(enumeratedValues.size(), schema.getZipfExponent());
                for (int i = 0; i < cardinality; i++) {
                    probabilities.add(new Pair<>(enumeratedValues.get(i), zipf.probability(i)));
                }
            }
            distribution = new EnumeratedTreeDistribution<>(probabilities);
            break;
        case ENUMERATED:
            for (int i = 0; i < enumeratedValues.size(); i++) {
                probabilities.add(new Pair<>(enumeratedValues.get(i), enumeratedProbabilities.get(i)));
            }
            distribution = new EnumeratedTreeDistribution<>(probabilities);
            break;
        default:
            throw new UnsupportedOperationException("Unknown distribution type: " + distributionType);
    }
    if (distribution instanceof AbstractIntegerDistribution) {
        ((AbstractIntegerDistribution) distribution).reseedRandomGenerator(seed);
    } else if (distribution instanceof AbstractRealDistribution) {
        ((AbstractRealDistribution) distribution).reseedRandomGenerator(seed);
    } else if (distribution instanceof EnumeratedDistribution) {
        ((EnumeratedDistribution) distribution).reseedRandomGenerator(seed);
    }
}
Also used : ValueType(io.druid.segment.column.ValueType) ArrayList(java.util.ArrayList) UniformRealDistribution(org.apache.commons.math3.distribution.UniformRealDistribution) EnumeratedDistribution(org.apache.commons.math3.distribution.EnumeratedDistribution) AbstractIntegerDistribution(org.apache.commons.math3.distribution.AbstractIntegerDistribution) AbstractRealDistribution(org.apache.commons.math3.distribution.AbstractRealDistribution) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) ZipfDistribution(org.apache.commons.math3.distribution.ZipfDistribution) Pair(org.apache.commons.math3.util.Pair)

Aggregations

NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)25 Random (java.util.Random)6 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)6 ArrayList (java.util.ArrayList)5 Test (org.testng.annotations.Test)5 AbstractRealDistribution (org.apache.commons.math3.distribution.AbstractRealDistribution)4 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)4 RegDataSet (edu.neu.ccs.pyramid.dataset.RegDataSet)3 UniformRealDistribution (org.apache.commons.math3.distribution.UniformRealDistribution)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 MixtureMultivariateNormalDistribution (org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution)2 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)2 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)2 MaxCountExceededException (org.apache.commons.math3.exception.MaxCountExceededException)2 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)2 Mean (org.apache.commons.math3.stat.descriptive.moment.Mean)2 StandardDeviation (org.apache.commons.math3.stat.descriptive.moment.StandardDeviation)2 MultiLabelClfDataSet (edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet)1 ValueType (io.druid.segment.column.ValueType)1 AbstractIntegerDistribution (org.apache.commons.math3.distribution.AbstractIntegerDistribution)1