ROCAUC implements Metric<Double> { @Override public double score(Iterator<LabelPair<Double>> iter) { PriorityQueue<Pair<Double, Double>> queue = new PriorityQueue<>(Comparator.comparingDouble(Pair::getKey)); long pos = 0; long neg = 0; while (iter.hasNext()) { LabelPair<Double> e = iter.next(); Double prediction = e.getPrediction(); Double truth = e.getTruth(); queue.add(new Pair<>(prediction, truth)); if (truth == positiveClsLb) pos++; else if (truth == negativeClsLb) neg++; else throw new UnknownClassLabelException(truth, positiveClsLb, negativeClsLb); } return calculateROCAUC(queue, pos, neg, positiveClsLb); }  @Override double score(Iterator<LabelPair<Double>> iter); static double calculateROCAUC(PriorityQueue<Pair<Double, Double>> queue, long pos, long neg, double positiveClsLb); double positiveClsLb(); ROCAUC withPositiveClsLb(double positiveClsLb); double negativeClsLb(); ROCAUC withNegativeClsLb(double negativeClsLb); @Override String name();  }
@Test public void testTotalTruth() { Metric<Double> scoreCalculator = new ROCAUC(); LabelPairCursor<Double> cursor = new TestLabelPairCursor<>( Arrays.asList(1.0, 1.0, 1.0, 1.0, 1.0), Arrays.asList(1.0, 1.0, 1.0, 1.0, 1.0) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals(Double.NaN, score, 1e-12); }
@Test public void testTotalUntruth() { Metric<Double> scoreCalculator = new ROCAUC(); LabelPairCursor<Double> cursor = new TestLabelPairCursor<>( Arrays.asList(1.0, 1.0, 1.0, 1.0, 1.0), Arrays.asList(0.0, 0.0, 0.0, 0.0, 0.0) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals(Double.NaN, score, 1e-12); }
@Test public void testOneDifferent() { Metric<Double> scoreCalculator = new ROCAUC(); LabelPairCursor<Double> cursor = new TestLabelPairCursor<>( Arrays.asList(1.0, 1.0, 1.0, 0.0), Arrays.asList(1.0, 1.0, 1.0, 1.0) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals(0.5, score, 1e-12); }
@Test public void testOneDifferentButBalanced() { Metric<Double> scoreCalculator = new ROCAUC(); LabelPairCursor<Double> cursor = new TestLabelPairCursor<>( Arrays.asList(1.0, 1.0, 0.0, 0.0), Arrays.asList(1.0, 1.0, 0.0, 1.0) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals(0.75, score, 1e-12); }
@Test public void testTwoDifferentAndBalanced() { Metric<Double> scoreCalculator = new ROCAUC(); LabelPairCursor<Double> cursor = new TestLabelPairCursor<>( Arrays.asList(1.0, 1.0, 0.0, 0.0), Arrays.asList(1.0, 0.0, 0.0, 1.0) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals(0.5, score, 1e-12); }
@Test public void testNotOnlyBinaryValues() { Metric<Double> scoreCalculator = new ROCAUC(); LabelPairCursor<Double> cursor = new TestLabelPairCursor<>( Arrays.asList(1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0), Arrays.asList(0.40209054, 0.33697626, 0.5449324, 0.13010869, 0.19019675, 0.39767829, 0.9686739, 0.91783275, 0.7503783, 0.5306605) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals(0.625, score, 1e-12); }