Example usage for org.apache.mahout.math.random Sampler sample

List of usage examples for org.apache.mahout.math.random Sampler sample

Introduction

In this page you can find the example usage for org.apache.mahout.math.random Sampler sample.

Prototype

T sample();

Source Link

Usage

From source file:com.mapr.synth.ForeignKeySamplerTest.java

License:Apache License

private void check(int n, DoubleFunction distribution, Sampler<JsonNode> s) {
    int[] counts = new int[n];
    for (int i = 0; i < 100000; i++) {
        counts[s.sample().asInt()]++;
    }/*www .  j  av  a  2  s  . c  o m*/

    double sum = 0;
    double[] p = new double[n];
    for (int i = 0; i < n; i++) {
        p[i] = distribution.apply(i);
        sum += p[i];
    }

    double z1 = 0;
    double z2 = 0;
    for (int i = 0; i < n; i++) {
        z1 += (p[i] - counts[i] / 100000.0) * Math.log(p[i] / sum);
        double expected = p[i] * 100000;
        double deviation = expected - counts[i];
        z2 += deviation * deviation / expected;
    }
    System.out.printf("%.4f %.4f\n", z1, z2);
}