Search in sources :

Example 6 with BinomialDistribution

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

the class AlleleFractionSegmenterUnitTest method generateAllelicCount.

protected static AllelicCount generateAllelicCount(final double minorFraction, final SimpleInterval position, final RandomGenerator rng, final GammaDistribution biasGenerator, final double outlierProbability) {
    final int numReads = 100;
    final double bias = biasGenerator.sample();
    //flip a coin to decide alt minor (alt fraction = minor fraction) or ref minor (alt fraction = 1 - minor fraction)
    final double altFraction = rng.nextDouble() < 0.5 ? minorFraction : 1 - minorFraction;
    //the probability of an alt read is the alt fraction modified by the bias or, in the case of an outlier, random
    final double pAlt = rng.nextDouble() < outlierProbability ? rng.nextDouble() : altFraction / (altFraction + (1 - altFraction) * bias);
    final int numAltReads = new BinomialDistribution(rng, numReads, pAlt).sample();
    final int numRefReads = numReads - numAltReads;
    return new AllelicCount(position, numAltReads, numRefReads);
}
Also used : BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution) AllelicCount(org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount)

Example 7 with BinomialDistribution

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

the class ArtifactStatisticsScorer method calculateArtifactPValue.

/** p-value for being an artifact
     *
     * @param totalAltAlleleCount total number of alt reads
     * @param artifactAltAlleleCount alt read counts in a pair orientation that supports an artifact
     * @param biasP believed bias p value for a binomial distribution in artifact variants
     * @return p-value for this being an artifact
     */
public static double calculateArtifactPValue(final int totalAltAlleleCount, final int artifactAltAlleleCount, final double biasP) {
    ParamUtils.isPositiveOrZero(biasP, "bias parameter must be positive or zero.");
    ParamUtils.isPositiveOrZero(totalAltAlleleCount, "total alt allele count must be positive or zero.");
    ParamUtils.isPositiveOrZero(artifactAltAlleleCount, "artifact supporting alt allele count must be positive or zero.");
    ParamUtils.isPositiveOrZero(totalAltAlleleCount - artifactAltAlleleCount, "Total alt count must be same or greater than the artifact alt count.");
    return new BinomialDistribution(null, totalAltAlleleCount, biasP).cumulativeProbability(artifactAltAlleleCount);
}
Also used : BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution)

Example 8 with BinomialDistribution

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

the class FractionalDownsamplerUnitTest method testClear.

@Test
public void testClear() throws Exception {
    final double f = 0.01;
    final int N = 100000;
    final ReadsDownsampler fd = new FractionalDownsampler(f);
    for (int i = 0; i < N; i++) {
        fd.submit(ArtificialReadUtils.createArtificialRead("100M"));
    }
    final BinomialDistribution bin = new BinomialDistribution(N, f);
    //we can fail this often
    final double errorRate = 1.0 / 1000;
    Assert.assertTrue(fd.size() <= bin.inverseCumulativeProbability(1 - errorRate / 2));
    Assert.assertTrue(fd.size() >= bin.inverseCumulativeProbability(errorRate / 2));
    Assert.assertTrue(fd.hasFinalizedItems());
    Assert.assertFalse(fd.hasPendingItems());
    Assert.assertFalse(fd.requiresCoordinateSortOrder());
    Assert.assertNotNull(fd.peekFinalized());
    Assert.assertNull(fd.peekPending());
    fd.clearItems();
    Assert.assertEquals(fd.size(), 0);
    Assert.assertFalse(fd.hasFinalizedItems());
    Assert.assertFalse(fd.hasPendingItems());
    Assert.assertFalse(fd.requiresCoordinateSortOrder());
    Assert.assertNull(fd.peekFinalized());
    Assert.assertNull(fd.peekPending());
}
Also used : BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 9 with BinomialDistribution

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

the class Sampling method rotate.

/**
     * sample desired size from first to last with given probabilities
     * if one pass is not enough, rotate
     * assume non-zero probabilities
     */
public static Set<Integer> rotate(List<Pair<Integer, Double>> probs, int size) {
    Set<Integer> res = new HashSet<>();
    if (size == 0) {
        return res;
    }
    // if not enough candidates, return all
    if (probs.size() < size) {
        probs.stream().forEach(pair -> res.add(pair.getFirst()));
        return res;
    }
    boolean next = true;
    while (next) {
        for (Pair<Integer, Double> pair : probs) {
            int dataIndex = pair.getFirst();
            double prob = pair.getSecond();
            if (res.size() == size) {
                next = false;
                break;
            }
            if (!res.contains(dataIndex)) {
                BinomialDistribution distribution = new BinomialDistribution(1, prob);
                int sample = distribution.sample();
                if (sample == 1) {
                    res.add(dataIndex);
                }
            }
        }
    }
    return res;
}
Also used : BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution)

Example 10 with BinomialDistribution

use of org.apache.commons.math3.distribution.BinomialDistribution in project presto by prestodb.

the class TestResourceGroups method testWeightedScheduling.

@Test(timeOut = 10_000)
public void testWeightedScheduling() {
    RootInternalResourceGroup root = new RootInternalResourceGroup("root", (group, export) -> {
    }, directExecutor());
    root.setSoftMemoryLimit(new DataSize(1, MEGABYTE));
    root.setMaxQueuedQueries(4);
    // Start with zero capacity, so that nothing starts running until we've added all the queries
    root.setMaxRunningQueries(0);
    root.setSchedulingPolicy(WEIGHTED);
    InternalResourceGroup group1 = root.getOrCreateSubGroup("1");
    group1.setSoftMemoryLimit(new DataSize(1, MEGABYTE));
    group1.setMaxQueuedQueries(2);
    group1.setMaxRunningQueries(2);
    InternalResourceGroup group2 = root.getOrCreateSubGroup("2");
    group2.setSoftMemoryLimit(new DataSize(1, MEGABYTE));
    group2.setMaxQueuedQueries(2);
    group2.setMaxRunningQueries(2);
    group2.setSchedulingWeight(2);
    Set<MockQueryExecution> group1Queries = fillGroupTo(group1, ImmutableSet.of(), 2);
    Set<MockQueryExecution> group2Queries = fillGroupTo(group2, ImmutableSet.of(), 2);
    root.setMaxRunningQueries(1);
    int group2Ran = 0;
    for (int i = 0; i < 1000; i++) {
        for (Iterator<MockQueryExecution> iterator = group1Queries.iterator(); iterator.hasNext(); ) {
            MockQueryExecution query = iterator.next();
            if (query.getState() == RUNNING) {
                query.complete();
                iterator.remove();
            }
        }
        for (Iterator<MockQueryExecution> iterator = group2Queries.iterator(); iterator.hasNext(); ) {
            MockQueryExecution query = iterator.next();
            if (query.getState() == RUNNING) {
                query.complete();
                iterator.remove();
                group2Ran++;
            }
        }
        root.processQueuedQueries();
        group1Queries = fillGroupTo(group1, group1Queries, 2);
        group2Queries = fillGroupTo(group2, group2Queries, 2);
    }
    // group1 has a weight of 1 and group2 has a weight of 2, so group2 should account for (2 / (1 + 2)) of the queries.
    // since this is stochastic, we check that the result of 1000 trials are 2/3 with 99.9999% confidence
    BinomialDistribution binomial = new BinomialDistribution(1000, 2.0 / 3.0);
    int lowerBound = binomial.inverseCumulativeProbability(0.000001);
    int upperBound = binomial.inverseCumulativeProbability(0.999999);
    assertLessThan(group2Ran, upperBound);
    assertGreaterThan(group2Ran, lowerBound);
}
Also used : RootInternalResourceGroup(com.facebook.presto.execution.resourceGroups.InternalResourceGroup.RootInternalResourceGroup) RootInternalResourceGroup(com.facebook.presto.execution.resourceGroups.InternalResourceGroup.RootInternalResourceGroup) DataSize(io.airlift.units.DataSize) BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution) MockQueryExecution(com.facebook.presto.execution.MockQueryExecution) Test(org.testng.annotations.Test)

Aggregations

BinomialDistribution (org.apache.commons.math3.distribution.BinomialDistribution)13 Test (org.testng.annotations.Test)3 ClusterPoint (gdsc.core.clustering.ClusterPoint)2 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)2 AllelicCount (org.broadinstitute.hellbender.tools.exome.alleliccount.AllelicCount)2 MockQueryExecution (com.facebook.presto.execution.MockQueryExecution)1 RootInternalResourceGroup (com.facebook.presto.execution.resourceGroups.InternalResourceGroup.RootInternalResourceGroup)1 Plot2 (ij.gui.Plot2)1 DataSize (io.airlift.units.DataSize)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)1 BernoulliReconstructionDistribution (org.deeplearning4j.nn.conf.layers.variational.BernoulliReconstructionDistribution)1 ExponentialReconstructionDistribution (org.deeplearning4j.nn.conf.layers.variational.ExponentialReconstructionDistribution)1 GaussianReconstructionDistribution (org.deeplearning4j.nn.conf.layers.variational.GaussianReconstructionDistribution)1 ReconstructionDistribution (org.deeplearning4j.nn.conf.layers.variational.ReconstructionDistribution)1 Test (org.junit.Test)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1