@Test public void test0(){ System.out.println("numerator = 0"); int[] truth = {0, 0, 0, 0, 1, 1, 1, 1}; int[] prediction = {0, 1, 0, 1, 0, 1, 0, 1}; double expResult = 0; double result = MCC.of(truth, prediction); assertEquals(expResult, result, 1E-5); }
public static double of(int[] truth, int[] prediction) { if (truth.length != prediction.length) { throw new IllegalArgumentException(String.format("The vector sizes don't match: %d != %d.", truth.length, prediction.length)); } ConfusionMatrix confusion = ConfusionMatrix.of(truth, prediction); int[][] matrix = confusion.matrix; if (matrix.length != 2 || matrix[0].length != 2) { throw new IllegalArgumentException("MCC can only be applied to binary classification: " + confusion.toString()); } int tp = matrix[0][0]; int tn = matrix[1][1]; int fp = matrix[0][1]; int fn = matrix[1][0]; int numerator = (tp * tn - fp * fn); double denominator = Math.sqrt(tp + fp) * Math.sqrt(tp + fn) * Math.sqrt(tn + fp) * Math.sqrt(tn + fn); return numerator / denominator; }
MCC implements ClassificationMeasure { public static double of(int[] truth, int[] prediction) { if (truth.length != prediction.length) { throw new IllegalArgumentException(String.format("The vector sizes don't match: %d != %d.", truth.length, prediction.length)); } ConfusionMatrix confusion = ConfusionMatrix.of(truth, prediction); int[][] matrix = confusion.matrix; if (matrix.length != 2 || matrix[0].length != 2) { throw new IllegalArgumentException("MCC can only be applied to binary classification: " + confusion.toString()); } int tp = matrix[0][0]; int tn = matrix[1][1]; int fp = matrix[0][1]; int fn = matrix[1][0]; int numerator = (tp * tn - fp * fn); double denominator = Math.sqrt(tp + fp) * Math.sqrt(tp + fn) * Math.sqrt(tn + fp) * Math.sqrt(tn + fn); return numerator / denominator; } }
MCC implements ClassificationMeasure { public static double of(int[] truth, int[] prediction) { if (truth.length != prediction.length) { throw new IllegalArgumentException(String.format("The vector sizes don't match: %d != %d.", truth.length, prediction.length)); } ConfusionMatrix confusion = ConfusionMatrix.of(truth, prediction); int[][] matrix = confusion.matrix; if (matrix.length != 2 || matrix[0].length != 2) { throw new IllegalArgumentException("MCC can only be applied to binary classification: " + confusion.toString()); } int tp = matrix[0][0]; int tn = matrix[1][1]; int fp = matrix[0][1]; int fn = matrix[1][0]; int numerator = (tp * tn - fp * fn); double denominator = Math.sqrt(tp + fp) * Math.sqrt(tp + fn) * Math.sqrt(tn + fp) * Math.sqrt(tn + fn); return numerator / denominator; }  }
MCC implements ClassificationMeasure { public static double of(int[] truth, int[] prediction) { if (truth.length != prediction.length) { throw new IllegalArgumentException(String.format("The vector sizes don't match: %d != %d.", truth.length, prediction.length)); } ConfusionMatrix confusion = ConfusionMatrix.of(truth, prediction); int[][] matrix = confusion.matrix; if (matrix.length != 2 || matrix[0].length != 2) { throw new IllegalArgumentException("MCC can only be applied to binary classification: " + confusion.toString()); } int tp = matrix[0][0]; int tn = matrix[1][1]; int fp = matrix[0][1]; int fn = matrix[1][0]; int numerator = (tp * tn - fp * fn); double denominator = Math.sqrt(tp + fp) * Math.sqrt(tp + fn) * Math.sqrt(tn + fp) * Math.sqrt(tn + fn); return numerator / denominator; }  @Override double measure(int[] truth, int[] prediction); static double of(int[] truth, int[] prediction); @Override String toString(); }
MCC implements ClassificationMeasure { public static double of(int[] truth, int[] prediction) { if (truth.length != prediction.length) { throw new IllegalArgumentException(String.format("The vector sizes don't match: %d != %d.", truth.length, prediction.length)); } ConfusionMatrix confusion = ConfusionMatrix.of(truth, prediction); int[][] matrix = confusion.matrix; if (matrix.length != 2 || matrix[0].length != 2) { throw new IllegalArgumentException("MCC can only be applied to binary classification: " + confusion.toString()); } int tp = matrix[0][0]; int tn = matrix[1][1]; int fp = matrix[0][1]; int fn = matrix[1][0]; int numerator = (tp * tn - fp * fn); double denominator = Math.sqrt(tp + fp) * Math.sqrt(tp + fn) * Math.sqrt(tn + fp) * Math.sqrt(tn + fn); return numerator / denominator; }  @Override double measure(int[] truth, int[] prediction); static double of(int[] truth, int[] prediction); @Override String toString(); final static MCC instance; }