ECF 1.5
measures.cpp
1#include "measures.h"
2#include <vector>
3#include <iostream>
4#include <cmath>
5
6namespace utility
7{
8 double accuracy(int TP, int TN, int FP, int FN, int numberOfClasses, int alpha)
9 {
10 double numerator = TP + TN + alpha;
11 double nominator = TP + TN + FP + FN + numberOfClasses;
12 return numerator/nominator;
13 }
14
15 double error(int TP, int TN, int FP, int FN, int numberOfClasses, int alpha)
16 {
17 double numerator = FP + FN + alpha;
18 double nominator = TP + TN + FP + FN + numberOfClasses;
19 return numerator/nominator;
20
21 }
22
23 double precision(int TP, int FP, int numberOfClasses, int alpha)
24 {
25 return (static_cast<double>(TP) + alpha) / (TP + FP + numberOfClasses);
26 }
27
28 double sensitivity(int TP, int FN, int numberOfClasses, int alpha)
29 {
30 return (static_cast<double>(TP) + alpha) / (TP + FN + numberOfClasses);
31 }
32
33 double fallout(int FP, int TN, int numberOfClasses, int alpha)
34 {
35 return (static_cast<double>(FP) + alpha) / (FP + TN + numberOfClasses);
36 }
37
38 double specificity(int TN, int FP, int numberOfClasses, int alpha)
39 {
40 return (static_cast<double>(TN) + alpha)/ (TN + FP + numberOfClasses);
41 }
42
43 double F1(int TP, int FP, int FN, int numberOfClasses, int alpha)
44 {
45 double P = precision(TP,FP,numberOfClasses,alpha);
46 double R = sensitivity(TP,FN,numberOfClasses,alpha);
47 return (2 * P * R) / (P + R);
48 }
49
50 double FBeta(int TP, int FP, int FN, double beta, int numberOfClasses, int alpha)
51 {
52 double P = precision(TP,FP,numberOfClasses,alpha);
53 double R = sensitivity(TP,FN,numberOfClasses,alpha);
54 return ((1 + beta * beta) * P * R) / (beta * beta * P + R);
55 }
56 double returnConfusionMatrixResult(const std::vector<std::vector<uint> >& confusionMatrix, uint size,
57 const std::string& measureUsed, int alpha)
58 {
59 std::vector<double> calculatedMeasures;
60 uint numberOfClasses = confusionMatrix.size();
61 uint TP=0,TN=0,FP=0,FN=0;
62 for(uint i = 0; i < numberOfClasses; i++) {
63 TP += confusionMatrix[i][i];
64 for(uint j = 0; j < confusionMatrix[i].size(); j++) {
65 if(i != j) {
66 FP += confusionMatrix[i][j];
67 }
68 }
69 for(uint j = 0; j < confusionMatrix.size(); j++) {
70 if(i != j) {
71 FN += confusionMatrix[j][i];
72 }
73 }
74 TN = size - TP - FP - FN;
75 if(measureUsed == "f1" || measureUsed == "f1score" ) {
76 calculatedMeasures.push_back(F1(TP,FP,FN,numberOfClasses,alpha));
77 }
78 else if(measureUsed == "precision") {
79 calculatedMeasures.push_back(precision(TP,FP,numberOfClasses, alpha));
80 }
81 else if(measureUsed == "specificity") {
82 calculatedMeasures.push_back(specificity(TN,FP,numberOfClasses,alpha));
83 }
84 else if(measureUsed == "sensitivity" || measureUsed == "recall" || measureUsed == "true positive rate") {
85 calculatedMeasures.push_back(sensitivity(TP,FN,numberOfClasses,alpha));
86 }
87 else if(measureUsed == "fallout" || measureUsed == "false positive rate") {
88 calculatedMeasures.push_back(fallout(FP,TN,numberOfClasses,alpha));
89 }
90 else if(measureUsed == "error") {
91 calculatedMeasures.push_back(error(TP,TN,FP,FN,numberOfClasses,alpha));
92 }
93 else {
94 calculatedMeasures.push_back(accuracy(TP,TN,FP,FN,numberOfClasses,alpha));
95 }
96 }
97 double measure = 0.0;
98 for(uint i = 0; i < calculatedMeasures.size(); i++) {
99 measure += calculatedMeasures[i];
100 }
101 return measure / calculatedMeasures.size();
102 }
103
104 double meanSquareError(const std::vector<double>& trueOutput, const std::vector<double>& calculatedOutput)
105 {
106 double sum = 0.0;
107 if(trueOutput.size() != calculatedOutput.size()) {
108 std::cerr << "Mean square error encountered an error. Calculated output and true output are not of same size.\n";
109 }
110 for(uint i = 0; i < trueOutput.size(); i++) {
111 sum += (trueOutput[i] - calculatedOutput[i]) * (trueOutput[i] - calculatedOutput[i]);
112 }
113 return sum / trueOutput.size();
114 }
115
116 double rootMeanSquareError(const std::vector<double>& trueOutput, const std::vector<double>& calculatedOutput)
117 {
118 double sum = 0.0;
119 if(trueOutput.size() != calculatedOutput.size()) {
120 std::cerr << "Root mean square error encountered an error. Calculated output and true output are not of same size.\n";
121 }
122 for(uint i = 0; i < trueOutput.size(); i++) {
123 sum += (trueOutput[i] - calculatedOutput[i]) * (trueOutput[i] - calculatedOutput[i]);
124 }
125 return std::sqrt(sum / trueOutput.size());
126 }
127}