@Test
public void compareMergingDigests() {

    TDigest d1 = new MergingDigest(100);
    TDigest d2 = new MergingDigest(100);
    d1.add(1);
    d2.add(3);
    assertEquals(2.77, Comparison.compareChi2(d1, d2, new double[]{1}), 0.01);
    Random r = new Random();
    int failed = 0;
    for (int i = 0; i < 1000; i++) {
        d1 = new MergingDigest(100);
        d2 = new MergingDigest(100);
        MergingDigest d3 = new MergingDigest(100);
        for (int j = 0; j < 10000; j++) {
            // these should look the same
            d1.add(r.nextGaussian());
            d2.add(r.nextGaussian());
            // can we see a small difference
            d3.add(r.nextGaussian() + 0.3);
        }
        // 5 degrees of freedom, Pr(llr > 20) < 0.005
        if (Comparison.compareChi2(d1, d2, new double[]{0.1, 0.3, 0.5, 0.8, 0.9}) > 25) {
            failed++;
        }
        // 1 degree of freedom, Pr(llr > 10) < 0.005
        if (Comparison.compareChi2(d1, d2, new double[]{0.1}) > 20) {
            failed++;
        }
        // 1 degree of freedom, Pr(llr > 10) < 0.005
        if (Comparison.compareChi2(d1, d2, new double[]{0.5}) > 20) {
            failed++;
        }
        if (Comparison.compareChi2(d1, d3, new double[]{0.1, 0.5, 0.9}) < 90) {
            failed++;
        }
    }
    assertEquals(0, failed, 5);
    System.out.printf("Failed %d times (up to 5 acceptable)", failed);
}