|
| 1 | +#pragma once |
| 2 | +// |
| 3 | +// FILE: GST.h |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// PURPOSE: Arduino library for Gold Standard Test metrics |
| 6 | +// URL: https://github.com/RobTillaart/GST |
| 7 | +// https://en.wikipedia.org/wiki/Sensitivity_and_specificity |
| 8 | +// https://en.wikipedia.org/wiki/Confusion_matrix |
| 9 | +// |
| 10 | +// formula's based upon wikipedia. |
| 11 | + |
| 12 | + |
| 13 | +#define GST_LIB_VERSION (F("0.1.0")) |
| 14 | + |
| 15 | + |
| 16 | +class GST |
| 17 | +{ |
| 18 | +public: |
| 19 | + GST() {}; |
| 20 | + |
| 21 | + // These 4 need to be filled in. |
| 22 | + void setTruePositive(float v) { TP = v; P = TP + FN; }; |
| 23 | + void setTrueNegative(float v) { TN = v; N = TN + FP; }; |
| 24 | + void setFalsePositive(float v) { FP = v; N = TN + FP; }; |
| 25 | + void setFalseNegative(float v) { FN = v; P = TP + FN; }; |
| 26 | + |
| 27 | + float getTruePositive() { return TP; }; |
| 28 | + float getTrueNegative() { return TN; }; |
| 29 | + float getFalsePositive() { return FP; }; |
| 30 | + float getFalseNegative() { return FN; }; |
| 31 | + |
| 32 | + float getTotal() { return P + N; }; |
| 33 | + float getActualPositive() { return P; }; |
| 34 | + float getActualNegative() { return N; }; |
| 35 | + float getTestedPositive() { return TP + FP; }; |
| 36 | + float getTestedNegative() { return TN + FN; }; |
| 37 | + |
| 38 | + float sensitivity() { return TPR(); }; |
| 39 | + float specificity() { return TNR(); }; |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + // true positive rate |
| 44 | + float TPR() { return TP / P; }; |
| 45 | + // true negative rate |
| 46 | + float TNR() { return TN / N; }; |
| 47 | + |
| 48 | + // false negative rate |
| 49 | + float FNR() { return FN / (FN + TP); }; |
| 50 | + // false positive rate |
| 51 | + float FPR() { return FP / (FP + TN); }; |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + // positive predictive value |
| 56 | + float PPV() { return TP / (TP + FP); }; |
| 57 | + // negative predictive value |
| 58 | + float NPV() { return TN / (TN + FN); }; |
| 59 | + |
| 60 | + // false discovery rate |
| 61 | + float FDR() { return FP / (FP + TP); }; |
| 62 | + // false omission rate |
| 63 | + float FOR() { return FN / (FN + TN); }; |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + // positive likelihood ratio |
| 68 | + float LRplus() { return TPR() / FPR(); }; |
| 69 | + // negative likelihood ratio |
| 70 | + float LRminus() { return FNR() / TNR(); }; |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + float prevalenceThreshold() { return sqrt(FPR()) / (sqrt(TPR()) + sqrt(FPR())); }; |
| 75 | + float threatScore() { return TP / (TP + FN + FP); }; |
| 76 | + float criticalSuccessIndex() { return threatScore(); }; |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + float prevalence() { return P / (P + N); }; |
| 81 | + float accuracy() { return (TP + TN) / (P + N); }; |
| 82 | + float balancedAccuracy() { return (TPR() + TNR()) / 2; }; |
| 83 | + float F1Score() { return (2 * TP)/(2 * TP + FP + FN); }; |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + // Matthews correlation coefficient |
| 88 | + float MCC() { return (TP*TN-FP*FN)/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN)); }; |
| 89 | + float phi() { return MCC(); }; |
| 90 | + // Fowlkes–Mallows index |
| 91 | + float FM() { return sqrt(PPV()*TPR()); }; |
| 92 | + // Bookmaker informedness |
| 93 | + float BM() { return TPR() + TNR() - 1; }; |
| 94 | + // markedness |
| 95 | + float MK() { return PPV() + NPV() - 1; }; |
| 96 | + float deltaP() { return MK(); }; |
| 97 | + // diagnostic odds ratio |
| 98 | + float DOR() { return LRplus() / LRminus(); }; |
| 99 | + |
| 100 | + |
| 101 | +private: |
| 102 | + float P = 0; |
| 103 | + float N = 0; |
| 104 | + float TP = 0; |
| 105 | + float TN = 0; |
| 106 | + float FP = 0; |
| 107 | + float FN = 0; |
| 108 | +}; |
| 109 | + |
| 110 | + |
| 111 | +// -- END OF FILE -- |
| 112 | + |
0 commit comments