forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85ecbed
commit 1a77659
Showing
4 changed files
with
74 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
plugin/trino-truera/src/main/java/io/trino/plugin/truera/ROCAUCFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.trino.plugin.truera; | ||
|
||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.stream.IntStream; | ||
|
||
public class ROCAUCFunction { | ||
public static double computeRocAuc(boolean[] labels, double[] scores) { | ||
int[] sortedIndices = IntStream.range(0, scores.length).boxed().sorted( | ||
Comparator.comparing(i -> scores[i]) | ||
).sorted(Collections.reverseOrder()).mapToInt(i->i).toArray(); | ||
|
||
int currTruePositives = 0, currFalsePositives = 0, prevTruePositives =0, prevFalsePositives = 0; | ||
double auc = 0.; | ||
|
||
for (int i : sortedIndices) { | ||
if (labels[i]) { currTruePositives++; } else { currFalsePositives++; }; | ||
prevTruePositives = currTruePositives; | ||
prevFalsePositives = currFalsePositives; | ||
auc += trapezoidIntegrate(prevFalsePositives, currFalsePositives, prevTruePositives, currTruePositives); | ||
} | ||
|
||
// If labels only contain one class, AUC is undefined | ||
if (currTruePositives == 0 || currFalsePositives == 0) { | ||
return Double.POSITIVE_INFINITY; | ||
} | ||
|
||
return auc / (currTruePositives * currFalsePositives); | ||
} | ||
|
||
private static double trapezoidIntegrate(double x1, double x2, double y1, double y2) { | ||
return (y1 + y2) * Math.abs(x2 - x1) / 2; // (base1 + base2) * height / 2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
plugin/trino-truera/src/test/java/io/trino/plugin/truera/TestROCAUCFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.trino.plugin.truera; | ||
|
||
import java.util.Arrays; | ||
import org.testng.annotations.Test; | ||
import static org.testng.Assert.assertEquals; | ||
public class TestROCAUCFunction { | ||
@Test | ||
public void testComputeAucRocWhenUndefined() { | ||
|
||
boolean[] testLabels = new boolean[10]; | ||
double[] testProbabilities = new double[10]; | ||
assertEquals(ROCAUCFunction.computeRocAuc(testLabels, testProbabilities), Double.POSITIVE_INFINITY); | ||
} | ||
|
||
@Test | ||
public void testComputeAucRoc() { | ||
|
||
boolean[] testLabels = new boolean[10]; | ||
testLabels[9] = true; | ||
double[] testProbabilities = new double[]{0.21206135, 0.97905249, 0.6460657 , 0.83698787, 0.40314617, | ||
0.62190361, 0.34917899, 0.88604834, 0.09936481, 0.65903197}; | ||
// Arrays.fill(testProbabilities, 1.0); | ||
for (boolean element: testLabels) { | ||
System.out.println(element); | ||
} | ||
for (double element: testProbabilities) { | ||
System.out.println(element); | ||
} | ||
System.out.println("score"); | ||
System.out.println(ROCAUCFunction.computeRocAuc(testLabels, testProbabilities)); | ||
} | ||
|
||
} |