Example usage for org.apache.commons.math3.distribution IntegerDistribution cumulativeProbability

List of usage examples for org.apache.commons.math3.distribution IntegerDistribution cumulativeProbability

Introduction

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

Prototype

double cumulativeProbability(int x);

Source Link

Document

For a random variable X whose values are distributed according to this distribution, this method returns P(X <= x) .

Usage

From source file:com.cloudera.oryx.common.OryxTest.java

/**
 * Asserts that the probability of sampling a value as or more extreme than the given value,
 * from the given discrete distribution, is at least 0.001.
 *
 * @param value sample value//  w  w w. ja va  2  s .c  o m
 * @param dist discrete distribution
 */
public static void checkDiscreteProbability(int value, IntegerDistribution dist) {
    double probAsExtreme = value <= dist.getNumericalMean() ? dist.cumulativeProbability(value)
            : (1.0 - dist.cumulativeProbability(value - 1));
    assertTrue(value + " is not likely (" + probAsExtreme + " ) to differ from expected value "
            + dist.getNumericalMean() + " by chance", probAsExtreme >= 0.001);
}

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

@Test
public void testMLUpdate() throws Exception {
    Path tempDir = getTempDir();/*from w ww  . j a v a 2s  . c om*/
    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:org.apache.solr.client.solrj.io.eval.CumulativeProbabilityEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }//from ww  w. j a  va2s  .  c  o  m
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof RealDistribution) && !(first instanceof IntegerDistribution)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a real or integer Distribution",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof Number)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a Number",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    if (first instanceof RealDistribution) {
        RealDistribution rd = (RealDistribution) first;
        Number predictOver = (Number) second;
        return rd.cumulativeProbability(predictOver.doubleValue());
    } else {
        IntegerDistribution id = (IntegerDistribution) first;
        Number predictOver = (Number) second;
        return id.cumulativeProbability(predictOver.intValue());
    }
}