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

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

Introduction

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

Prototype

double probability(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.github.rinde.rinsim.util.StochasticSuppliersTest.java

@Test
public void testUniform() {
    final RandomGenerator rng = new MersenneTwister(123L);
    final StochasticSupplier<Integer> sup = uniformInt(2, 10);
    final IntegerDistribution id = new UniformIntegerDistribution(2, 10);

    final Multiset<Integer> ms = TreeMultiset.create();
    for (int i = 0; i < 1000; i++) {
        ms.add(sup.get(rng.nextLong()));
    }//from  w w  w .  j  a  v a2s .  c o  m
    final List<Integer> observations = newArrayList();
    final List<Double> expectations = newArrayList();
    for (final Multiset.Entry<Integer> entry : ms.entrySet()) {
        observations.add(entry.getCount());
        expectations.add(id.probability(entry.getElement()));
    }
    assertTrue(chiSquare(expectations, observations, .01));
}

From source file:org.apache.mahout.math.random.PoissonSamplerTest.java

private static void checkDistribution(Sampler<Double> pd, double alpha) {
    int[] count = new int[(int) Math.max(10, 5 * alpha)];
    for (int i = 0; i < 10000; i++) {
        count[pd.sample().intValue()]++;
    }//from ww w .j a  v a 2  s.  c o  m

    IntegerDistribution ref = new PoissonDistribution(RandomUtils.getRandom().getRandomGenerator(), alpha,
            PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    for (int i = 0; i < count.length; i++) {
        assertEquals(ref.probability(i), count[i] / 10000.0, 2.0e-2);
    }
}

From source file:org.apache.solr.client.solrj.io.eval.ProbabilityEvaluator.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 w  w  w . j  ava  2 s  .  c om*/
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof IntegerDistribution)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a IntegerDistribution",
                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()));
    }

    IntegerDistribution d = (IntegerDistribution) first;
    Number predictOver = (Number) second;
    return d.probability(predictOver.intValue());
}