libqe-stats 0.1.3-dev.9005
QE C++ library
Loading...
Searching...
No Matches
metrics.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <stdexcept>
5#include <string>
6
7// Functions that must reproduce R's arithmetic (rhoR parity) start their body
8// with QESTATS_R_ARITHMETIC and carry QESTATS_R_ARITHMETIC_FN: R never fuses
9// a * b + c, so these keep the compiler from contracting it into an FMA.
10#if defined(__clang__)
11# define QESTATS_R_ARITHMETIC _Pragma("STDC FP_CONTRACT OFF")
12# define QESTATS_R_ARITHMETIC_FN
13#elif defined(__GNUC__)
14# define QESTATS_R_ARITHMETIC
15# define QESTATS_R_ARITHMETIC_FN __attribute__((optimize("fp-contract=off")))
16#else
17# define QESTATS_R_ARITHMETIC
18# define QESTATS_R_ARITHMETIC_FN
19#endif
20
21namespace qestats {
22
23// Confusion matrix proportions (must sum to 1 after normalization).
25 double tp = 0, fp = 0, fn = 0, tn = 0;
26};
27
28// All derived classification metrics.
29struct Metrics {
30 // confusion matrix proportions
31 double tp = 0, fp = 0, fn = 0, tn = 0;
32 // derived
33 double b1 = 0; // classifier positive rate (tp + fp)
34 double b2 = 0; // true positive rate (tp + fn)
35 double precision = 0; // tp / (tp + fp)
36 double recall = 0; // tp / (tp + fn)
37 double accuracy = 0; // tp + tn
38 double fdr = 0; // fp / (tp + fp) — False Discovery Rate
39 double for_ = 0; // fn / (fn + tn) — False Omission Rate
40 double kappa = 0;
41};
42
43// Range result — min/max of a metric over the key variable interval.
45 double min = 0;
46 double max = 0;
47};
48
49// Confidence interval from a hypergeometric estimate.
50struct Interval {
51 double lower = 0;
52 double mean = 0;
53 double upper = 0;
54};
55
56// One row in the metrics_range output table.
58 std::string metric;
59 double lower = 0, mean = 0, upper = 0;
60};
61
62// ── Core arithmetic ───────────────────────────────────────────────────────────
63
64// Cohen's kappa from confusion-matrix counts (or proportions), computed in
65// the same order as rhoR's calcKappa() so the two agree to the bit: perfect
66// agreement on one class (all TP or all TN) is 1.
68inline double kappa_from_counts(double tp, double fp, double fn, double tn) {
70 const double s = tp + fp + fn + tn;
71 if (tp == s || tn == s) return 1.0;
72 const double agreement = (tp + tn) / s;
73 const double rate1 = (tp + fn) / s; // first rater's positive rate
74 const double rate2 = (tp + fp) / s; // second rater's positive rate
75 const double pe = rate1 * rate2 + (1 - rate1) * (1 - rate2);
76 return (pe < 1.0) ? (agreement - pe) / (1 - pe) : 0.0;
77}
78
79// Rates are computed from the counts, not the normalized cells, so precision
80// and recall match rhoR's TP / (TP + FP) and TP / (TP + FN) exactly.
82 const double s = cm.tp + cm.fp + cm.fn + cm.tn;
83 if (s <= 0) throw std::domain_error("metrics_from_cm: confusion matrix is empty");
84 Metrics m;
85 m.tp = cm.tp / s; m.fp = cm.fp / s;
86 m.fn = cm.fn / s; m.tn = cm.tn / s;
87 const double pos1 = cm.tp + cm.fp, pos2 = cm.tp + cm.fn, neg1 = cm.fn + cm.tn;
88 m.b1 = pos1 / s;
89 m.b2 = pos2 / s;
90 m.precision = (pos1 > 0) ? cm.tp / pos1 : 0.0;
91 m.recall = (pos2 > 0) ? cm.tp / pos2 : 0.0;
92 m.accuracy = (cm.tp + cm.tn) / s;
93 m.fdr = (pos1 > 0) ? cm.fp / pos1 : 0.0;
94 m.for_ = (neg1 > 0) ? cm.fn / neg1 : 0.0;
95 m.kappa = kappa_from_counts(cm.tp, cm.fp, cm.fn, cm.tn);
96 return m;
97}
98
99// Convenience: from raw counts (negative values clamped to 0).
100inline Metrics calculate_metrics(double tp, double fp, double fn, double tn) {
101 if (tp < 0) tp = 0; if (fp < 0) fp = 0;
102 if (fn < 0) fn = 0; if (tn < 0) tn = 0;
103 return metrics_from_cm({tp, fp, fn, tn});
104}
105
106// Retrieve a single metric value by name (supports alias names).
107inline double get_metric(const Metrics& m, const std::string& name) {
108 if (name == "b1" || name == "baserate") return m.b1;
109 if (name == "b2" || name == "baserate2") return m.b2;
110 if (name == "precision") return m.precision;
111 if (name == "recall") return m.recall;
112 if (name == "accuracy") return m.accuracy;
113 if (name == "FDR") return m.fdr;
114 if (name == "FOR") return m.for_;
115 if (name == "kappa") return m.kappa;
116 if (name == "tp") return m.tp;
117 if (name == "fp") return m.fp;
118 if (name == "fn") return m.fn;
119 if (name == "tn") return m.tn;
120 throw std::invalid_argument("get_metric: unknown metric '" + name + "'");
121}
122
123} // namespace qestats
#define QESTATS_R_ARITHMETIC_FN
Definition metrics.hpp:18
#define QESTATS_R_ARITHMETIC
Definition metrics.hpp:17
Definition comparison.hpp:12
Metrics metrics_from_cm(const ConfusionMatrix &cm)
Definition metrics.hpp:81
Metrics calculate_metrics(double tp, double fp, double fn, double tn)
Definition metrics.hpp:100
double get_metric(const Metrics &m, const std::string &name)
Definition metrics.hpp:107
QESTATS_R_ARITHMETIC_FN double kappa_from_counts(double tp, double fp, double fn, double tn)
Definition metrics.hpp:68
Definition metrics.hpp:24
double fp
Definition metrics.hpp:25
double fn
Definition metrics.hpp:25
double tp
Definition metrics.hpp:25
double tn
Definition metrics.hpp:25
Definition metrics.hpp:50
double mean
Definition metrics.hpp:52
double upper
Definition metrics.hpp:53
double lower
Definition metrics.hpp:51
Definition metrics.hpp:57
std::string metric
Definition metrics.hpp:58
double mean
Definition metrics.hpp:59
double upper
Definition metrics.hpp:59
double lower
Definition metrics.hpp:59
Definition metrics.hpp:44
double max
Definition metrics.hpp:46
double min
Definition metrics.hpp:45
Definition metrics.hpp:29
double tn
Definition metrics.hpp:31
double accuracy
Definition metrics.hpp:37
double kappa
Definition metrics.hpp:40
double b2
Definition metrics.hpp:34
double precision
Definition metrics.hpp:35
double fdr
Definition metrics.hpp:38
double fp
Definition metrics.hpp:31
double recall
Definition metrics.hpp:36
double tp
Definition metrics.hpp:31
double fn
Definition metrics.hpp:31
double for_
Definition metrics.hpp:39
double b1
Definition metrics.hpp:33