libqe-stats 0.1.2
QE C++ library
Loading...
Searching...
No Matches
rho.hpp
Go to the documentation of this file.
1#pragma once
2#include "metrics.hpp"
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <cstdlib>
7#include <random>
8#include <stdexcept>
9#include <string>
10#include <type_traits>
11#include <vector>
12
13// Rho (Shaffer's rho): how likely a test set's kappa this high would be if the
14// coding process's population kappa were below a threshold. A port of rhoR:
15// given an adapter over R's RNG, a seeded rho() returns exactly what rhoR does.
16//
17// rhoR computes rho two ways, and both are kept, quirks included:
18//
19// RhoMethod::Standard rhoR's default path, written in R (calcRho): grids
20// from seq(), draws from sample.int(), the hand set
21// sampled as rows of the full set. qestats::rho_standard.
22// RhoMethod::Fast rhoR's C++ path (calcRho_c, any other `method`):
23// Armadillo regspace() grids, RcppArmadillo sample() /
24// randi() draws, the hand set sampled from the
25// contingency table. qestats::rho_fast.
26//
27// Random numbers come from an Rng: any type with
28//
29// double unif_rand(); // uniform on (0, 1) — R's unif_rand()
30// double unif_index(double n); // uniform integer in [0, n) — R's R_unif_index()
31//
32// The Standard method draws only through unif_index, the Fast method only
33// through unif_rand. StdRng is a standalone default.
34
35namespace qestats {
36
37// mt19937_64 with the draws derived by hand (the std distributions differ
38// between standard libraries), so a seed gives the same rho everywhere.
39struct StdRng {
40 std::mt19937_64 gen;
41 explicit StdRng(std::uint64_t seed = 5489u) : gen(seed) {}
42 double unif_rand() {
43 double u;
44 do { u = static_cast<double>(gen() >> 11) * 0x1.0p-53; } while (u <= 0.0);
45 return u;
46 }
47 // Rejection on the top bits, like R_unif_index.
48 double unif_index(double n) {
49 const auto m = static_cast<std::uint64_t>(n);
50 if (m <= 1) return 0;
51 int bits = 0;
52 while (bits < 64 && (std::uint64_t(1) << bits) < m) ++bits;
53 std::uint64_t v;
54 do { v = gen() >> (64 - bits); } while (v >= m);
55 return static_cast<double>(v);
56 }
57};
58
59enum class RhoMethod { Standard, Fast };
60
61// rhoR's rhoK() arguments after the observed kappa, base rate and test set
62// length, with the same defaults.
63struct RhoOptions {
65 int ocs_length = 10000; // OcSLength
66 int replicates = 800;
67 double scs_kappa_threshold = 0.9; // ScSKappaThreshold
68 double scs_kappa_min = 0.40; // ScSKappaMin
69 double scs_precision_min = 0.6; // ScSPrecisionMin
70 double scs_precision_max = 1; // ScSPrecisionMax
72};
73
75 double kappa = 0, precision = 0;
76};
77
78// Both rhoR paths recurse until a valid (kappa, precision) pair turns up and
79// give up with this message; here the retries are capped instead.
80inline constexpr int RHO_MAX_ATTEMPTS = 10000;
81inline const char* const RHO_NO_VALID_SET =
82 "Could not generate a valid set given ranges of kappa and precision";
83
84// ── Standard method: rhoR's R implementation ─────────────────────────────────
85
86namespace rho_standard {
87
88// getR(): the recall that gives `kappa` at this base rate and precision.
90inline double recall(double kappa, double baserate, double precision) {
92 const double top = kappa * precision;
93 return top / (2 * precision - 2 * baserate - kappa + 2 * baserate * kappa);
94}
95
96// checkBRPKcombo(): whether (base rate, precision, kappa) can form a set.
98inline bool valid_combo(double baserate, double precision, double kappa) {
100 const double right = (2 * baserate * kappa - 2 * baserate - kappa) / (kappa - 2);
101 return precision > right;
102}
103
104// seq(from, to, length.out = n).
106inline std::vector<double> seq(double from, double to, int n) {
108 if (n <= 0) return {};
109 if (n <= 2) {
110 std::vector<double> v = {from, to};
111 v.resize(n);
112 return v;
113 }
114 if (from == to) return std::vector<double>(n, from);
115 const double by = (to - from) / (n - 1);
116 std::vector<double> v(n);
117 v[0] = from;
118 for (int i = 1; i < n - 1; ++i) v[i] = from + static_cast<double>(i) * by;
119 v[n - 1] = to;
120 return v;
121}
122
123// sample.int(n, k) without replacement, 0-based.
124template <class Rng>
125std::vector<std::size_t> sample_int(std::size_t n, std::size_t k, Rng& rng) {
126 if (k > n)
127 throw std::invalid_argument(
128 "cannot take a sample larger than the population when 'replace = FALSE'");
129 std::vector<std::size_t> out(k);
130 if (k < 2) {
131 for (std::size_t i = 0; i < k; ++i)
132 out[i] = static_cast<std::size_t>(rng.unif_index(static_cast<double>(n)));
133 return out;
134 }
135 std::vector<std::size_t> x(n);
136 for (std::size_t i = 0; i < n; ++i) x[i] = i;
137 for (std::size_t i = 0; i < k; ++i) {
138 const auto j = static_cast<std::size_t>(rng.unif_index(static_cast<double>(n)));
139 out[i] = x[j];
140 x[j] = x[--n];
141 }
142 return out;
143}
144
145// genPKcombo(): one (kappa, precision) draw from the two grids. When the pair
146// is invalid, precision is redrawn from the grid values above the minimum
147// valid precision; when none are, kappa is redrawn too.
148template <class Rng>
150KappaPrecision kappa_precision(const std::vector<double>& kappas,
151 const std::vector<double>& precisions,
152 double baserate, Rng& rng) {
154 for (int attempt = 0; attempt < RHO_MAX_ATTEMPTS; ++attempt) {
155 const double kappa = kappas[sample_int(kappas.size(), 1, rng)[0]];
156 double precision = precisions[sample_int(precisions.size(), 1, rng)[0]];
157 if (valid_combo(baserate, precision, kappa)) return {kappa, precision};
158 const double precision_min =
159 (2 * baserate * kappa - 2 * baserate - kappa) / (kappa - 2);
160 std::vector<double> above;
161 for (double p : precisions) if (p > precision_min) above.push_back(p);
162 if (above.empty()) continue;
163 precision = above[sample_int(above.size(), 1, rng)[0]];
164 return {kappa, precision};
165 }
166 throw std::runtime_error(RHO_NO_VALID_SET);
167}
168
169// generateKPs() with the flat distribution: kappa from a 100,000-point grid,
170// precision from a 10,000-point grid.
171template <class Rng>
172std::vector<KappaPrecision>
173generate_kps(int n, double baserate, double kappa_min, double kappa_max,
174 double precision_min, double precision_max, Rng& rng) {
175 const auto kappas = seq(kappa_min, kappa_max, 100000);
176 const auto precisions = seq(precision_min, precision_max, 10000);
177 std::vector<KappaPrecision> kps;
178 kps.reserve(n > 0 ? n : 0);
179 for (int i = 0; i < n; ++i) kps.push_back(kappa_precision(kappas, precisions, baserate, rng));
180 return kps;
181}
182
183// prset() / pr.ct(): the full set's contingency table (counts). R's round()
184// rounds halves to even.
186inline ConfusionMatrix contingency_table(double precision, double recall,
187 int length, double baserate) {
189 const double gold1s = std::max(std::nearbyint(baserate * length), 1.0);
190 const double gold0s = length - gold1s;
191 const double tp = std::max(std::nearbyint(gold1s * recall), 1.0);
192 const double fp = std::min(gold0s, std::max(std::nearbyint(tp * (1 - precision) / precision), 1.0));
193 return {tp, fp, gold1s - tp, gold0s - fp};
194}
195
196// getHandSet(): kappa of a test set of `length` rows sampled from the full
197// set laid out as prset() does (TP rows, FN rows, FP rows, TN rows). With an
198// inflated base rate, ceiling(length * inflation) rows are first sampled from
199// the first rater's positives.
200template <class Rng>
202double hand_kappa(const ConfusionMatrix& full, int length, double inflation, Rng& rng) {
204 const auto tp = static_cast<std::size_t>(full.tp), fn = static_cast<std::size_t>(full.fn);
205 const auto fp = static_cast<std::size_t>(full.fp), tn = static_cast<std::size_t>(full.tn);
206 const std::size_t gold1s = tp + fn, rows = gold1s + fp + tn;
207 double counts[4] = {0, 0, 0, 0}; // tp, fp, fn, tn
208 auto add = [&](std::size_t row) {
209 if (row < tp) counts[0] += 1;
210 else if (row < gold1s) counts[2] += 1;
211 else if (row < gold1s + fp) counts[1] += 1;
212 else counts[3] += 1;
213 };
214 const double positives = std::ceil(length * inflation);
215 if (positives > static_cast<double>(gold1s))
216 throw std::invalid_argument("Not enough positives in first rater to inflate to this level");
217 if (positives > 0) {
218 const auto k = static_cast<std::size_t>(positives);
219 std::vector<bool> taken(rows, false);
220 for (std::size_t row : sample_int(gold1s, k, rng)) { taken[row] = true; add(row); }
221 std::vector<std::size_t> others;
222 others.reserve(rows - k);
223 for (std::size_t row = 0; row < rows; ++row) if (!taken[row]) others.push_back(row);
224 for (std::size_t i : sample_int(others.size(), static_cast<std::size_t>(length) - k, rng))
225 add(others[i]);
226 } else {
227 for (std::size_t row : sample_int(rows, static_cast<std::size_t>(length), rng)) add(row);
228 }
229 return kappa_from_counts(counts[0], counts[1], counts[2], counts[3]);
230}
231
232// mean(x, na.rm = TRUE): R sums in long double and adds a correction pass.
233inline double r_mean(const std::vector<double>& x) {
234 long double s = 0;
235 std::size_t n = 0;
236 for (double v : x) if (!std::isnan(v)) { s += v; ++n; }
237 s /= static_cast<long double>(n);
238 if (std::isfinite(static_cast<double>(s))) {
239 long double t = 0;
240 for (double v : x) if (!std::isnan(v)) t += (v - s);
241 s += t / static_cast<long double>(n);
242 }
243 return static_cast<double>(s);
244}
245
246// getBootPvalue(): 1 below the distribution's mean, else the share of the
247// distribution at or above `observed`.
248inline double boot_pvalue(const std::vector<double>& distribution, double observed) {
249 if (observed < r_mean(distribution)) return 1;
250 std::size_t at_or_above = 0;
251 for (double v : distribution) if (v >= observed) ++at_or_above;
252 return static_cast<double>(at_or_above) / static_cast<double>(distribution.size());
253}
254
255// calcRho() with given (kappa, precision) pairs.
256template <class Rng>
257double rho(double observed_kappa, double baserate, int test_set_length,
258 const std::vector<KappaPrecision>& kps, const RhoOptions& opts, Rng& rng) {
259 const std::size_t replicates =
260 std::min(kps.size(), static_cast<std::size_t>(std::max(opts.replicates, 0)));
261 std::vector<double> kappas(replicates, 0.0);
262 for (std::size_t i = 0; i < replicates; ++i) {
263 const double r = recall(kps[i].kappa, baserate, kps[i].precision);
264 const auto full = contingency_table(kps[i].precision, r, opts.ocs_length, baserate);
265 kappas[i] = hand_kappa(full, test_set_length, opts.test_set_baserate_inflation, rng);
266 }
267 return boot_pvalue(kappas, observed_kappa);
268}
269
270} // namespace rho_standard
271
272// ── Fast method: rhoR's C++ implementation ───────────────────────────────────
273//
274// Compiled as plain C++, like rhoR's src/rhor.cpp, so the compiler treats its
275// arithmetic the way it treated rhoR's.
276
277namespace rho_fast {
278
279// recall(): the same formula as rho_standard::recall.
280inline double recall(double kappa, double baserate, double precision) {
281 const double top = kappa * precision;
282 return top / (2 * precision - 2 * baserate - kappa + 2 * baserate * kappa);
283}
284
285// check_BRK_combo().
286inline bool valid_combo(double baserate, double precision, double kappa) {
287 const double right = ((2 * baserate * kappa) - (2 * baserate) - kappa) / (kappa - 2);
288 return precision > right;
289}
290
291// Armadillo's regspace(start, delta, end).
292inline std::vector<double> regspace(double start, double delta, double end) {
293 if (((start < end) && (delta < 0)) || ((start > end) && (delta > 0)) || (delta == 0)) return {};
294 const bool ascend = (start <= end);
295 const double inc = (delta < 0) ? -delta : delta;
296 const double m = (ascend ? (end - start) : (start - end)) / inc;
297 const auto n = static_cast<std::size_t>(1 + static_cast<std::size_t>(std::floor(m)));
298 // Two loops, as Armadillo writes them: a compiler that fuses
299 // start + i * inc into an FMA then does it the same way here.
300 std::vector<double> v(n);
301 double* mem = v.data();
302 if (ascend) {
303 for (std::size_t i = 0; i < n; ++i) { mem[i] = double(start + double(i * inc)); }
304 } else {
305 for (std::size_t i = 0; i < n; ++i) { mem[i] = double(start - double(i * inc)); }
306 }
307 return v;
308}
309
310// One index from RcppArmadillo's sample(x, 1, FALSE): int(n * unif_rand()).
311template <class Rng>
312std::size_t sample_one(std::size_t n, Rng& rng) {
313 const int n_orig = static_cast<int>(n);
314 const int j = static_cast<int>(n_orig * rng.unif_rand());
315 return static_cast<std::size_t>(j);
316}
317
318// Armadillo's randi<int>(distr_param(a, b)) on R's RNG (RcppArmadillo's
319// Alt_R_RNG.h): runif(0, RAND_MAX) scaled to b - a + 1 values.
320template <class Rng>
321int randi(int a, int b, Rng& rng) {
322 if (!(a <= b))
323 throw std::logic_error("randi(): incorrect distribution parameters; a must be less than b");
324 const double rand_max = static_cast<double>(RAND_MAX);
325 const double draw = 0.0 + (rand_max - 0.0) * rng.unif_rand();
326 if (a == 0 && b == RAND_MAX) return static_cast<int>(draw);
327 const auto length = static_cast<unsigned long long>(b - a + 1);
328 const double scale = double(length) / rand_max;
329 return (std::min)(b, int(draw * scale) + a);
330}
331
332// find_valid_pk().
333template <class Rng>
334KappaPrecision kappa_precision(const std::vector<double>& kappas,
335 const std::vector<double>& precisions,
336 double baserate, Rng& rng) {
337 for (int attempt = 0; attempt < RHO_MAX_ATTEMPTS; ++attempt) {
338 const double kappa = kappas[sample_one(kappas.size(), rng)];
339 double precision = precisions[sample_one(precisions.size(), rng)];
340 if (!valid_combo(baserate, precision, kappa)) {
341 const double precision_min =
342 (2 * baserate * kappa - 2 * baserate - kappa) / (kappa - 2);
343 std::vector<double> above;
344 for (double p : precisions) if (p > precision_min) above.push_back(p);
345 if (above.empty()) continue;
346 precision = above[sample_one(above.size(), rng)];
347 }
348 return {kappa, precision};
349 }
350 throw std::runtime_error(RHO_NO_VALID_SET);
351}
352
353// generate_kp_list() with the flat distribution (distributionType 0); rhoR's
354// bell-shaped option (distributionType 1) is not ported.
355template <class Rng>
356std::vector<KappaPrecision>
357generate_kps(int n, double baserate, double kappa_min, double kappa_max,
358 double precision_min, double precision_max, Rng& rng,
359 long distribution_length = 10000) {
360 const double kappa_step = (kappa_max - kappa_min) / (distribution_length - 1);
361 const auto kappas = regspace(kappa_min, kappa_step, kappa_max);
362 const double precision_step = (precision_max - precision_min) / (10000 - 1);
363 const auto precisions = regspace(precision_min, precision_step, precision_max);
364 if (kappas.empty() || precisions.empty())
365 throw std::invalid_argument("generate_kps: empty kappa or precision range");
366 std::vector<KappaPrecision> kps;
367 kps.reserve(n > 0 ? n : 0);
368 for (int i = 0; i < n; ++i) kps.push_back(kappa_precision(kappas, precisions, baserate, rng));
369 return kps;
370}
371
372// contingency_table(): the full set's table (counts). C's round() rounds
373// halves away from zero.
374inline ConfusionMatrix contingency_table(double precision, double recall,
375 int length, double baserate) {
376 const int gold1s = static_cast<int>(std::max(std::round(baserate * length), 1.0));
377 const int gold0s = length - gold1s;
378 const int tp = static_cast<int>(std::max(std::round(gold1s * recall), 1.0));
379 const int fp = static_cast<int>(std::min(static_cast<double>(gold0s),
380 std::max(std::round(tp * (1 - precision) / precision), 1.0)));
381 return {double(tp), double(fp), double(gold1s - tp), double(gold0s - fp)};
382}
383
384// sample_contingency_table(): draw n items without replacement from cells
385// (a 1x2 row or a 2x2 table in column order: TP, FP, FN, TN), returning each
386// draw's 0-based cell. Kept as rhoR has it: the draw spans sum + 1 values and
387// the first cell takes the extra one, so an empty first cell can be drawn.
388// The draw spans every cell given; draws past the third cell land in the
389// fourth.
390template <class Rng>
391std::vector<int> sample_contingency_table(std::vector<int> cells, int n, Rng& rng) {
392 if (cells.size() < 4) cells.resize(4, 0);
393 std::vector<int> out(n > 0 ? n : 0);
394 for (int w = 0; w < n; ++w) {
395 int max_int = 0;
396 for (int c : cells) max_int += c;
397 const int s = randi(0, max_int, rng);
398 const int cell = (s <= cells[0]) ? 0
399 : (s <= cells[0] + cells[1]) ? 1
400 : (s <= cells[0] + cells[1] + cells[2]) ? 2 : 3;
401 out[w] = cell;
402 cells[cell]--;
403 }
404 return out;
405}
406
407// getHand_ct(): the test set's table, drawn from the full set's table.
408template <class Rng>
410 double inflation, Rng& rng) {
411 const int positives = static_cast<int>(std::ceil(length * inflation));
412 int tp = static_cast<int>(full.tp), fp = static_cast<int>(full.fp);
413 int fn = static_cast<int>(full.fn), tn = static_cast<int>(full.tn);
414 int counts[4] = {0, 0, 0, 0}; // column order: tp, fp, fn, tn
415 if (positives > 0) {
416 int pos_tp = 0, pos_fn = 0;
417 for (int cell : sample_contingency_table({tp, fn}, positives, rng))
418 (cell == 0 ? pos_tp : pos_fn)++;
419 tp -= pos_tp;
420 fn -= pos_fn;
421 counts[0] += pos_tp;
422 counts[2] += pos_fn;
423 }
424 for (int cell : sample_contingency_table({tp, fp, fn, tn}, length - positives, rng))
425 counts[cell]++;
426 return {double(counts[0]), double(counts[1]), double(counts[2]), double(counts[3])};
427}
428
429// kappa_ct(): kappa as rhoR's C++ computes it. Within an ulp of
430// kappa_from_counts, but NaN (not 1) when one cell holds every item.
431inline double kappa_ct(const ConfusionMatrix& ct) {
432 const double a = ct.tp, c = ct.fn, b = ct.fp, d = ct.tn;
433 const double size = a + b + c + d;
434 const double p_zero = (a + d) / size;
435 const double p_yes = ((a + b) / size) * ((a + c) / size);
436 const double p_no = ((c + d) / size) * ((b + d) / size);
437 const double p_e = (p_yes + p_no);
438 return (p_zero - p_e) / (1 - p_e);
439}
440
441// Armadillo's mean(): two interleaved accumulators.
442inline double arma_mean(const std::vector<double>& x) {
443 const std::size_t n = x.size();
444 if (n == 0) throw std::logic_error("mean(): object has no elements");
445 double acc1 = 0, acc2 = 0;
446 std::size_t j;
447 for (j = 1; j < n; j += 2) { acc1 += x[j - 1]; acc2 += x[j]; }
448 if ((j - 1) < n) acc1 += x[j - 1];
449 const double mean = (acc1 + acc2) / double(n);
450 if (std::isfinite(mean)) return mean;
451 for (double v : x) if (!std::isfinite(v)) return mean;
452 double r_mean = 0;
453 for (std::size_t i = 0; i < n; ++i) r_mean = r_mean + (x[i] - r_mean) / double(i + 1);
454 return r_mean;
455}
456
457// getBootPvalue_c().
458inline double boot_pvalue(const std::vector<double>& distribution, double observed) {
459 if (observed < arma_mean(distribution)) return 1.0;
460 std::size_t at_or_above = 0;
461 for (double v : distribution) if (v >= observed) ++at_or_above;
462 return (at_or_above * 1.0) / distribution.size();
463}
464
465// calcRho_c() with given (kappa, precision) pairs.
466template <class Rng>
467double rho(double observed_kappa, double baserate, int test_set_length,
468 const std::vector<KappaPrecision>& kps, const RhoOptions& opts, Rng& rng) {
469 const std::size_t replicates =
470 std::min(kps.size(), static_cast<std::size_t>(std::max(opts.replicates, 0)));
471 std::vector<double> kappas(replicates);
472 for (std::size_t i = 0; i < replicates; ++i) {
473 const double r = recall(kps[i].kappa, baserate, kps[i].precision);
474 const auto full = contingency_table(kps[i].precision, r, opts.ocs_length, baserate);
475 kappas[i] = kappa_ct(hand_contingency_table(full, test_set_length,
476 opts.test_set_baserate_inflation, rng));
477 }
478 return boot_pvalue(kappas, observed_kappa);
479}
480
481} // namespace rho_fast
482
483// ── rho ──────────────────────────────────────────────────────────────────────
484
485// rhoK()'s argument checks, with its messages.
486inline void check_rho_args(double observed_kappa, double baserate, const RhoOptions& o) {
487 if ((observed_kappa > 1 || observed_kappa < 0) || (baserate > 1 || baserate < 0))
488 throw std::invalid_argument("x and OcSBaserate must be between 0 and 1.");
489 if (o.scs_kappa_threshold < 0 || o.scs_kappa_min < 0)
490 throw std::invalid_argument("OcSBaserate, ScSKappaThreshold, and ScSKappaMin must be positive.");
492 throw std::invalid_argument("ScSKappaThreshold must be greater than ScSKappaMin.");
493 if ((o.scs_precision_min < 0 || o.scs_precision_min > 1) ||
494 (o.scs_precision_max < 0 || o.scs_precision_max > 1))
495 throw std::invalid_argument("ScSPrecisionMin and ScSPrecisionMax must be between 0 and 1.");
497 throw std::invalid_argument("ScSPrecisionMax must be greater than ScSPrecisionMin.");
498}
499
500// The (kappa, precision) pairs rho() simulates: opts.replicates draws with
501// kappa in [scs_kappa_min, scs_kappa_threshold] and precision in
502// [scs_precision_min, scs_precision_max].
503template <class Rng>
504std::vector<KappaPrecision> rho_kps(double baserate, const RhoOptions& opts, Rng& rng) {
505 if (opts.method == RhoMethod::Fast)
506 return rho_fast::generate_kps(opts.replicates, baserate, opts.scs_kappa_min,
508 opts.scs_precision_max, rng);
509 return rho_standard::generate_kps(opts.replicates, baserate, opts.scs_kappa_min,
511 opts.scs_precision_max, rng);
512}
513
514// rhoK(): rho for an observed kappa on a test set of `test_set_length` items
515// from a population with this base rate. Only a class type is taken as an Rng,
516// so an integer seed of any type picks the overload below.
517template <class Rng, std::enable_if_t<std::is_class_v<Rng>, int> = 0>
518double rho(double observed_kappa, double baserate, int test_set_length,
519 const RhoOptions& opts, Rng& rng) {
520 check_rho_args(observed_kappa, baserate, opts);
521 const auto kps = rho_kps(baserate, opts, rng);
522 if (opts.method == RhoMethod::Fast)
523 return rho_fast::rho(observed_kappa, baserate, test_set_length, kps, opts, rng);
524 return rho_standard::rho(observed_kappa, baserate, test_set_length, kps, opts, rng);
525}
526
527inline double rho(double observed_kappa, double baserate, int test_set_length,
528 const RhoOptions& opts = {}, std::uint64_t seed = 5489u) {
529 StdRng rng(seed);
530 return rho(observed_kappa, baserate, test_set_length, opts, rng);
531}
532
533} // namespace qestats
#define QESTATS_R_ARITHMETIC_FN
Definition metrics.hpp:18
#define QESTATS_R_ARITHMETIC
Definition metrics.hpp:17
std::size_t sample_one(std::size_t n, Rng &rng)
Definition rho.hpp:312
std::vector< KappaPrecision > generate_kps(int n, double baserate, double kappa_min, double kappa_max, double precision_min, double precision_max, Rng &rng, long distribution_length=10000)
Definition rho.hpp:357
double kappa_ct(const ConfusionMatrix &ct)
Definition rho.hpp:431
bool valid_combo(double baserate, double precision, double kappa)
Definition rho.hpp:286
int randi(int a, int b, Rng &rng)
Definition rho.hpp:321
std::vector< int > sample_contingency_table(std::vector< int > cells, int n, Rng &rng)
Definition rho.hpp:391
ConfusionMatrix contingency_table(double precision, double recall, int length, double baserate)
Definition rho.hpp:374
std::vector< double > regspace(double start, double delta, double end)
Definition rho.hpp:292
double recall(double kappa, double baserate, double precision)
Definition rho.hpp:280
KappaPrecision kappa_precision(const std::vector< double > &kappas, const std::vector< double > &precisions, double baserate, Rng &rng)
Definition rho.hpp:334
double boot_pvalue(const std::vector< double > &distribution, double observed)
Definition rho.hpp:458
ConfusionMatrix hand_contingency_table(const ConfusionMatrix &full, int length, double inflation, Rng &rng)
Definition rho.hpp:409
double arma_mean(const std::vector< double > &x)
Definition rho.hpp:442
double rho(double observed_kappa, double baserate, int test_set_length, const std::vector< KappaPrecision > &kps, const RhoOptions &opts, Rng &rng)
Definition rho.hpp:467
double boot_pvalue(const std::vector< double > &distribution, double observed)
Definition rho.hpp:248
double r_mean(const std::vector< double > &x)
Definition rho.hpp:233
QESTATS_R_ARITHMETIC_FN double recall(double kappa, double baserate, double precision)
Definition rho.hpp:90
QESTATS_R_ARITHMETIC_FN double hand_kappa(const ConfusionMatrix &full, int length, double inflation, Rng &rng)
Definition rho.hpp:202
std::vector< KappaPrecision > generate_kps(int n, double baserate, double kappa_min, double kappa_max, double precision_min, double precision_max, Rng &rng)
Definition rho.hpp:173
QESTATS_R_ARITHMETIC_FN bool valid_combo(double baserate, double precision, double kappa)
Definition rho.hpp:98
std::vector< std::size_t > sample_int(std::size_t n, std::size_t k, Rng &rng)
Definition rho.hpp:125
QESTATS_R_ARITHMETIC_FN KappaPrecision kappa_precision(const std::vector< double > &kappas, const std::vector< double > &precisions, double baserate, Rng &rng)
Definition rho.hpp:150
QESTATS_R_ARITHMETIC_FN std::vector< double > seq(double from, double to, int n)
Definition rho.hpp:106
QESTATS_R_ARITHMETIC_FN ConfusionMatrix contingency_table(double precision, double recall, int length, double baserate)
Definition rho.hpp:186
double rho(double observed_kappa, double baserate, int test_set_length, const std::vector< KappaPrecision > &kps, const RhoOptions &opts, Rng &rng)
Definition rho.hpp:257
Definition comparison.hpp:12
double rho(double observed_kappa, double baserate, int test_set_length, const RhoOptions &opts, Rng &rng)
Definition rho.hpp:518
constexpr int RHO_MAX_ATTEMPTS
Definition rho.hpp:80
void check_rho_args(double observed_kappa, double baserate, const RhoOptions &o)
Definition rho.hpp:486
std::vector< KappaPrecision > rho_kps(double baserate, const RhoOptions &opts, Rng &rng)
Definition rho.hpp:504
const char *const RHO_NO_VALID_SET
Definition rho.hpp:81
RhoMethod
Definition rho.hpp:59
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 rho.hpp:74
double precision
Definition rho.hpp:75
double kappa
Definition rho.hpp:75
Definition rho.hpp:63
double scs_precision_min
Definition rho.hpp:69
double scs_precision_max
Definition rho.hpp:70
int replicates
Definition rho.hpp:66
double test_set_baserate_inflation
Definition rho.hpp:64
double scs_kappa_threshold
Definition rho.hpp:67
RhoMethod method
Definition rho.hpp:71
int ocs_length
Definition rho.hpp:65
double scs_kappa_min
Definition rho.hpp:68
Definition rho.hpp:39
double unif_rand()
Definition rho.hpp:42
StdRng(std::uint64_t seed=5489u)
Definition rho.hpp:41
double unif_index(double n)
Definition rho.hpp:48
std::mt19937_64 gen
Definition rho.hpp:40