Search in sources :

Example 1 with AbstractIntegerDistribution

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

the class BenchmarkColumnValueGenerator method generateSingleRowValue.

private Object generateSingleRowValue() {
    Object ret = null;
    ValueType type = schema.getType();
    if (distribution instanceof AbstractIntegerDistribution) {
        ret = ((AbstractIntegerDistribution) distribution).sample();
    } else if (distribution instanceof AbstractRealDistribution) {
        ret = ((AbstractRealDistribution) distribution).sample();
    } else if (distribution instanceof EnumeratedDistribution) {
        ret = ((EnumeratedDistribution) distribution).sample();
    }
    ret = convertType(ret, type);
    return ret;
}
Also used : AbstractRealDistribution(org.apache.commons.math3.distribution.AbstractRealDistribution) ValueType(io.druid.segment.column.ValueType) EnumeratedDistribution(org.apache.commons.math3.distribution.EnumeratedDistribution) AbstractIntegerDistribution(org.apache.commons.math3.distribution.AbstractIntegerDistribution)

Example 2 with AbstractIntegerDistribution

use of org.apache.commons.math3.distribution.AbstractIntegerDistribution in project gatk by broadinstitute.

the class FisherExactTest method twoSidedPValue.

/**
     * Computes the 2-sided pvalue of the Fisher's exact test on a normalized table that ensures that the sum of
     * all four entries is less than 2 * 200.
     */
public static double twoSidedPValue(final int[][] normalizedTable) {
    Utils.nonNull(normalizedTable);
    Utils.validateArg(normalizedTable.length == 2, () -> "input must be 2x2 " + Arrays.deepToString(normalizedTable));
    Utils.validateArg(normalizedTable[0] != null && normalizedTable[0].length == 2, () -> "input must be 2x2 " + Arrays.deepToString(normalizedTable));
    Utils.validateArg(normalizedTable[1] != null && normalizedTable[1].length == 2, () -> "input must be 2x2 " + Arrays.deepToString(normalizedTable));
    //Note: this implementation follows the one in R base package
    final int[][] x = normalizedTable;
    final int m = x[0][0] + x[0][1];
    final int n = x[1][0] + x[1][1];
    final int k = x[0][0] + x[1][0];
    final int lo = Math.max(0, k - n);
    final int hi = Math.min(k, m);
    final IndexRange support = new IndexRange(lo, hi + 1);
    if (support.size() <= 1) {
        //special case, support has only one value
        return 1.0;
    }
    final AbstractIntegerDistribution dist = new HypergeometricDistribution(null, m + n, m, k);
    final double[] logds = support.mapToDouble(dist::logProbability);
    final double threshold = logds[x[0][0] - lo] * REL_ERR;
    final double[] log10ds = DoubleStream.of(logds).filter(d -> d <= threshold).map(MathUtils::logToLog10).toArray();
    final double pValue = MathUtils.sumLog10(log10ds);
    // min is necessary as numerical precision can result in pValue being slightly greater than 1.0
    return Math.min(pValue, 1.0);
}
Also used : HypergeometricDistribution(org.apache.commons.math3.distribution.HypergeometricDistribution) AbstractIntegerDistribution(org.apache.commons.math3.distribution.AbstractIntegerDistribution)

Example 3 with AbstractIntegerDistribution

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

AbstractIntegerDistribution (org.apache.commons.math3.distribution.AbstractIntegerDistribution)3 ValueType (io.druid.segment.column.ValueType)2 AbstractRealDistribution (org.apache.commons.math3.distribution.AbstractRealDistribution)2 EnumeratedDistribution (org.apache.commons.math3.distribution.EnumeratedDistribution)2 ArrayList (java.util.ArrayList)1 HypergeometricDistribution (org.apache.commons.math3.distribution.HypergeometricDistribution)1 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)1 UniformRealDistribution (org.apache.commons.math3.distribution.UniformRealDistribution)1 ZipfDistribution (org.apache.commons.math3.distribution.ZipfDistribution)1 Pair (org.apache.commons.math3.util.Pair)1