[9119] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9119] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Problems.DataAnalysis.OnlineCalculators {
|
---|
| 26 | public class MatthewsCorrelationCoefficientCalculator {
|
---|
| 27 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
|
---|
| 28 | var confusionMatrix = ConfusionMatrixCalculator.Calculate(originalValues, estimatedValues, out errorState);
|
---|
| 29 | if (!errorState.Equals(OnlineCalculatorError.None)) {
|
---|
| 30 | return double.NaN;
|
---|
| 31 | }
|
---|
| 32 | return CalculateMCC(confusionMatrix);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | private static double CalculateMCC(double[,] confusionMatrix) {
|
---|
| 36 | if (confusionMatrix.GetLength(0) != confusionMatrix.GetLength(1)) {
|
---|
| 37 | throw new ArgumentException("Confusion matrix is not a square matrix.");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | int classes = confusionMatrix.GetLength(0);
|
---|
| 41 | double numerator = 0;
|
---|
| 42 | for (int k = 0; k < classes; k++) {
|
---|
| 43 | for (int l = 0; l < classes; l++) {
|
---|
| 44 | for (int m = 0; m < classes; m++) {
|
---|
| 45 | numerator += confusionMatrix[k, k] * confusionMatrix[m, l] - confusionMatrix[l, k] * confusionMatrix[k, m];
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | double denominator1 = 0;
|
---|
| 51 | double denominator2 = 0;
|
---|
| 52 | for (int k = 0; k < classes; k++) {
|
---|
| 53 | double clk = 0;
|
---|
| 54 | double cgf = 0;
|
---|
| 55 | double ckl = 0;
|
---|
| 56 | double cfg = 0;
|
---|
| 57 | for (int l = 0; l < classes; l++) {
|
---|
| 58 | clk += confusionMatrix[l, k];
|
---|
| 59 | ckl += confusionMatrix[k, l];
|
---|
| 60 | }
|
---|
| 61 | for (int f = 0; f < classes; f++) {
|
---|
| 62 | if (f == k) {
|
---|
| 63 | continue;
|
---|
| 64 | }
|
---|
| 65 | for (int g = 0; g < classes; g++) {
|
---|
| 66 | cgf += confusionMatrix[g, f];
|
---|
| 67 | cfg += confusionMatrix[f, g];
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | denominator1 += clk * cgf;
|
---|
| 71 | denominator2 += ckl * cfg;
|
---|
| 72 | }
|
---|
| 73 | denominator1 = Math.Sqrt(denominator1);
|
---|
| 74 | denominator2 = Math.Sqrt(denominator2);
|
---|
| 75 |
|
---|
| 76 | return numerator / (denominator1 * denominator2);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|