Example usage for org.apache.commons.math3.distribution BinomialDistribution BinomialDistribution

List of usage examples for org.apache.commons.math3.distribution BinomialDistribution BinomialDistribution

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution BinomialDistribution BinomialDistribution.

Prototype

public BinomialDistribution(RandomGenerator rng, int trials, double p) 

Source Link

Document

Creates a binomial distribution.

Usage

From source file:math.test.TestDimpleRandom.java

/**
 * Measures speed of Apache vs Colt generators
 *//*  w  ww  . j  a  va 2 s. c  o  m*/
public static void main(String[] args) {
    RandomGenerator apacheGenerator = new MersenneTwister(42);
    cern.jet.random.engine.RandomEngine cernGenerator = new cern.jet.random.engine.MersenneTwister();

    final int N = 100000;
    long start, end;

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        apacheGenerator.nextDouble();
    }
    end = System.nanoTime();
    long apacheTime = end - start;

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernGenerator.nextDouble();
    }
    end = System.nanoTime();

    long cernTime = end - start;

    System.out.format("MersenneTwister.nextDouble() x %d apache/cern %f\n", N, (double) apacheTime / cernTime);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        BetaDistribution apacheBeta = new BetaDistribution(apacheGenerator, 1.0, 1.0);
        apacheBeta.sample();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    cern.jet.random.Beta cernBeta = new cern.jet.random.Beta(1, 1, cernGenerator);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernBeta.nextDouble();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    System.out.format("Beta x %d apache/cern %f\n", N, (double) apacheTime / cernTime);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        GammaDistribution apacheGamma = new GammaDistribution(apacheGenerator, 1.0, 1.0);
        apacheGamma.sample();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    cern.jet.random.Gamma cernGamma = new cern.jet.random.Gamma(1, 1, cernGenerator);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernGamma.nextDouble();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    System.out.format("Gamma x %d apache/cern %f\n", N, (double) apacheTime / cernTime);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        BinomialDistribution apacheBinomial = new BinomialDistribution(apacheGenerator, 1, .5);
        apacheBinomial.sample();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    cern.jet.random.Binomial cernBinomial = new cern.jet.random.Binomial(1, .5, cernGenerator);

    start = System.nanoTime();
    for (int i = N; --i >= 0;) {
        cernBinomial.nextInt();
    }
    end = System.nanoTime();
    apacheTime = end - start;

    System.out.format("Binomial x %d apache/cern %f\n", N, (double) apacheTime / cernTime);
}

From source file:com.analog.lyric.math.DimpleRandom.java

/**
 * Construct using specified underlying random generator and seed.
 * <p>//  www. j a  v a 2 s  .  co  m
 * @since 0.08
 */
public DimpleRandom(RandomGenerator randomGenerator, long seed) {
    super(randomGenerator);
    _randGenerator = randomGenerator;
    _seed = seed;
    _randEngine = new cern.jet.random.engine.MersenneTwister((int) seed);
    _randGamma = new cern.jet.random.Gamma(1, 1, _randEngine);
    _randBeta = new BetaDistribution(_randGenerator, 1, 1);
    _randBinomial = new BinomialDistribution(_randGenerator, 1, 0.5);
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Integer> getBinomial(final RandomNumberStream rng, final Number trials,
        final Number p) {
    final IntegerDistribution dist = new BinomialDistribution(
            RandomNumberStream.Util.asCommonsRandomGenerator(rng), trials.intValue(), p.doubleValue());
    return new RandomNumberDistribution<Integer>() {
        @Override//ww  w . j a v  a  2s . c o m
        public Integer draw() {
            return dist.sample();
        }
    };
}

From source file:com.cloudera.oryx.ml.SimpleMLUpdateIT.java

@Test
public void testMLUpdate() throws Exception {
    Path tempDir = getTempDir();/* w  ww.j a  va2s. c o  m*/
    Path dataDir = tempDir.resolve("data");
    Path modelDir = tempDir.resolve("model");
    Map<String, String> overlayConfig = new HashMap<>();
    overlayConfig.put("oryx.batch.update-class", MockMLUpdate.class.getName());
    ConfigUtils.set(overlayConfig, "oryx.batch.storage.data-dir", dataDir);
    ConfigUtils.set(overlayConfig, "oryx.batch.storage.model-dir", modelDir);
    overlayConfig.put("oryx.batch.generation-interval-sec", Integer.toString(GEN_INTERVAL_SEC));
    overlayConfig.put("oryx.batch.block-interval-sec", Integer.toString(BLOCK_INTERVAL_SEC));
    overlayConfig.put("oryx.ml.eval.test-fraction", Double.toString(TEST_FRACTION));
    Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());

    startMessaging();

    List<Integer> trainCounts = new ArrayList<>();
    List<Integer> testCounts = new ArrayList<>();

    MockMLUpdate.setCountHolders(trainCounts, testCounts);

    startServerProduceConsumeTopics(config, DATA_TO_WRITE, WRITE_INTERVAL_MSEC);

    // If lists are unequal at this point, there must have been an empty test set
    // which yielded no call to evaluate(). Fill in the blank
    while (trainCounts.size() > testCounts.size()) {
        testCounts.add(0);
    }

    log.info("trainCounts = {}", trainCounts);
    log.info("testCounts = {}", testCounts);

    checkOutputData(dataDir, DATA_TO_WRITE);
    checkIntervals(trainCounts.size(), DATA_TO_WRITE, WRITE_INTERVAL_MSEC, GEN_INTERVAL_SEC);

    assertEquals(testCounts.size(), trainCounts.size());

    RandomGenerator random = RandomManager.getRandom();
    int lastTotalTrainCount = 0;
    int lastTestCount = 0;
    for (int i = 0; i < testCounts.size(); i++) {
        int totalTrainCount = trainCounts.get(i);
        int testCount = testCounts.get(i);
        int newTrainInGen = totalTrainCount - (lastTotalTrainCount + lastTestCount);
        if (newTrainInGen == 0) {
            continue;
        }
        lastTotalTrainCount = totalTrainCount;
        lastTestCount = testCount;
        int totalNew = testCount + newTrainInGen;

        IntegerDistribution dist = new BinomialDistribution(random, totalNew, TEST_FRACTION);
        double probability;
        if (testCount < dist.getNumericalMean()) {
            probability = dist.cumulativeProbability(testCount);
        } else {
            probability = 1.0 - dist.cumulativeProbability(testCount);
        }
        log.info("Probability of observing {} as {} sample of {}: {}", testCount, TEST_FRACTION, totalNew,
                probability);
        assertTrue(probability >= 0.001);
    }

}

From source file:com.analog.lyric.math.DimpleRandom.java

/**
 * Returns sample from beta distribution with specified alpha and beta parameters.
 * @since 0.08/*from   w  w  w .j  a  va  2 s.c  o m*/
 */
public int nextBinomial(int n, double p) {
    // randBinomial doesn't accept zero N value or 1 or 0 p value
    if (n <= 0)
        return 0;
    else if (p <= 0)
        return 0;
    else if (p >= 1)
        return n;

    BinomialDistribution randBinomial = _randBinomial;

    if (randBinomial.getNumberOfTrials() != n || randBinomial.getProbabilityOfSuccess() != p) {
        _randBinomial = randBinomial = new BinomialDistribution(_randGenerator, n, p);
    }

    return randBinomial.sample();
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Integer> getPoisson(final RandomNumberStream rng, final Number alpha,
        final Number beta) {
    final IntegerDistribution dist = new BinomialDistribution(
            RandomNumberStream.Util.asCommonsRandomGenerator(rng), alpha.intValue(), beta.doubleValue());
    return new RandomNumberDistribution<Integer>() {
        @Override/*w  ww.  j  a v  a 2  s  .  com*/
        public Integer draw() {
            return dist.sample();
        }
    };
}

From source file:com.cloudera.oryx.app.speed.rdf.RDFSpeedIT.java

@Test
public void testRDFSpeedClassification() throws Exception {
    Map<String, Object> overlayConfig = new HashMap<>();
    overlayConfig.put("oryx.speed.model-manager-class", RDFSpeedModelManager.class.getName());
    overlayConfig.put("oryx.speed.streaming.generation-interval-sec", 5);
    overlayConfig.put("oryx.input-schema.feature-names", "[\"color\",\"fruit\"]");
    overlayConfig.put("oryx.input-schema.numeric-features", "[]");
    overlayConfig.put("oryx.input-schema.target-feature", "fruit");
    Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());

    startMessaging();//from   w w w  .j a v a  2  s .  c om

    List<Pair<String, String>> updates = startServerProduceConsumeTopics(config,
            new MockRDFClassificationInputGenerator(), new MockRDFClassificationModelGenerator(), NUM_INPUT, 1);

    if (log.isDebugEnabled()) {
        updates.forEach(update -> log.debug("{}", update));
    }

    int numUpdates = updates.size();

    // Model, and then pairs of positive / negative
    assertGreaterOrEqual(numUpdates, 3);
    assertNotEquals(0, numUpdates % 2);
    // Not testing the model much here:
    assertEquals("MODEL", updates.get(0).getFirst());

    PMML pmml = PMMLUtils.fromString(updates.get(0).getSecond());
    CategoricalValueEncodings encodings = AppPMMLUtils.buildCategoricalValueEncodings(pmml.getDataDictionary());
    log.info("{}", encodings);
    Map<String, Integer> fruitEncoding = encodings.getValueEncodingMap(0);
    String red = Integer.toString(fruitEncoding.get("red"));
    String yellow = Integer.toString(fruitEncoding.get("yellow"));

    for (int i = 1; i < numUpdates; i++) {
        Pair<String, String> update = updates.get(i);
        assertEquals("UP", update.getFirst());
        List<?> fields = TextUtils.readJSON(update.getSecond(), List.class);
        int treeID = (Integer) fields.get(0);
        String nodeID = fields.get(1).toString();
        @SuppressWarnings("unchecked")
        Map<String, Integer> countMap = (Map<String, Integer>) fields.get(2);
        assertEquals(0, treeID);
        assertContains(Arrays.asList("r-", "r+"), nodeID);
        int yellowCount = countMap.containsKey(yellow) ? countMap.get(yellow) : 0;
        int redCount = countMap.containsKey(red) ? countMap.get(red) : 0;
        int count = yellowCount + redCount;
        assertGreater(count, 0);
        BinomialDistribution dist = new BinomialDistribution(RandomManager.getRandom(), count, 0.9);
        if ("r+".equals(nodeID)) {
            // Should be about 9x more yellow
            checkProbability(yellowCount, count, dist);
        } else {
            // Should be about 9x more red
            checkProbability(redCount, count, dist);
        }
    }

}

From source file:org.aika.network.neuron.lattice.AndNode.java

public void computeWeight() {
    if (Network.numberOfPositions == 0 || frequency < Node.minFrequency) {
        return;//w w  w. j  a  v  a2s  .c om
    }

    double nullHyp = 1.0;
    for (InputNode ref : parents.keySet()) {
        Node in = ref.inputNeuron.node;
        double p = (double) in.frequency / (double) Network.numberOfPositions;
        if (p > 1.0)
            p = 1.0;
        nullHyp *= p;
    }

    BinomialDistribution binDist = new BinomialDistribution(null, Network.numberOfPositions, nullHyp);

    weight = binDist.cumulativeProbability(frequency - 1);

    n = Network.numberOfPositions;
    /*
            double minPRel = 1.0;
            for(Map.Entry<InputNode, Node> me: parents.entrySet()) {
    double p = 1.0 - ((double) frequency / (double) me.getValue().frequency);
    if(minPRel > p) minPRel = p;
            
    p = 1.0 - ((double) frequency / (double) me.getKey().inputNeuron.node.frequency);
    if(minPRel > p) minPRel = p;
            }
            
            minPRelevance = minPRel;
    */
    if (level == 2) {
        double minPRel = 1.0;
        for (Map.Entry<InputNode, Node> me : parents.entrySet()) {
            double p = 1.0 - ((double) frequency / (double) me.getValue().frequency);
            if (minPRel > p)
                minPRel = p;
        }

        if (minPRel < 0.1) {
            shadowedInput = true;
        }
    }

    setSignificant(weight > 0.99);
}

From source file:org.asoem.greyfish.utils.collect.BitString.java

/**
 * Create a random bit string of given {@code length} where each bit is set with probability {@code p}, and not set
 * with probability {@code 1-p}./* ww w  .java  2 s.  co  m*/
 *
 * @param length the length of the bit string
 * @param rng    the random number generator to use
 * @param p      the probability for each bit in the new bit string to hold the value 1
 * @return a new bit string
 */
public static BitString random(final int length, final RandomGenerator rng, final double p) {
    checkNotNull(rng);
    checkArgument(p >= 0 && p <= 1);
    checkArgument(length >= 0);

    if (length == 0) {
        return emptyBitSequence();
    }

    if (p == 0.5) {
        return random(length, rng); // faster
    }

    final int n;
    if (p == 0) {
        n = 0;
    } else if (p == 1) {
        n = length;
    } else {
        final BinomialDistribution binomialDistribution = new BinomialDistribution(rng, length, p);
        n = binomialDistribution.sample();
    }
    assert n >= 0 && n <= length : n;

    if (n == 0) {
        return zeros(length);
    } else if (n == length) {
        return ones(length);
    }

    final ContiguousSet<Integer> indexRange = ContiguousSet.create(Range.closedOpen(0, length),
            DiscreteDomain.integers());
    final Iterable<Integer> uniqueIndexSample = Samplings.random(rng).withoutReplacement().sample(indexRange,
            n);

    if ((double) n / length < 1.0 / 32) { // < 1 bit per word?
        return new IndexSetString(ImmutableSortedSet.copyOf(uniqueIndexSample), length);
    } else {
        final BitSet bs = new BitSet(length);
        for (Integer index : uniqueIndexSample) {
            bs.set(index, true);
        }
        return new BitSetString(bs, length);
    }
}

From source file:org.asoem.greyfish.utils.collect.BitStringTest.java

public static Map<Integer, Integer> expectedFrequencies(final int length, final double p, final int sampleSize,
        final RandomGenerator rng) {
    final Map<Integer, Integer> frequencies = Maps.newHashMap();
    final BinomialDistribution binomialDistribution = new BinomialDistribution(rng, length, p);
    for (int i = 0; i < sampleSize; i++) {
        final int sample = binomialDistribution.sample();
        Integer integer = frequencies.get(sample);
        if (integer == null) {
            frequencies.put(sample, 0);//from   ww  w  .j  a  v  a2 s  .  c  o  m
            integer = 0;
        }
        frequencies.put(sample, integer + 1);
    }
    return frequencies;
}