41 explicit StdRng(std::uint64_t seed = 5489u) :
gen(seed) {}
44 do { u =
static_cast<double>(
gen() >> 11) * 0x1.0p-53; }
while (u <= 0.0);
49 const auto m =
static_cast<std::uint64_t
>(n);
52 while (bits < 64 && (std::uint64_t(1) << bits) < m) ++bits;
54 do { v =
gen() >> (64 - bits); }
while (v >= m);
55 return static_cast<double>(v);
82 "Could not generate a valid set given ranges of kappa and precision";
86namespace rho_standard {
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);
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;
106inline std::vector<double>
seq(
double from,
double to,
int n) {
108 if (n <= 0)
return {};
110 std::vector<double> v = {from, to};
114 if (from == to)
return std::vector<double>(n, from);
115 const double by = (to - from) / (n - 1);
116 std::vector<double> v(n);
118 for (
int i = 1; i < n - 1; ++i) v[i] = from +
static_cast<double>(i) * by;
125std::vector<std::size_t>
sample_int(std::size_t n, std::size_t k, Rng& rng) {
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);
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)));
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)));
151 const std::vector<double>& precisions,
152 double baserate, Rng& rng) {
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};
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));
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};
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};
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;
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");
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))
227 for (std::size_t row :
sample_int(rows,
static_cast<std::size_t
>(length), rng)) add(row);
233inline double r_mean(
const std::vector<double>& x) {
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))) {
240 for (
double v : x)
if (!std::isnan(v)) t += (v - s);
241 s += t /
static_cast<long double>(n);
243 return static_cast<double>(s);
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());
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);
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);
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;
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)));
300 std::vector<double> v(n);
301 double* mem = v.data();
303 for (std::size_t i = 0; i < n; ++i) { mem[i] = double(start +
double(i * inc)); }
305 for (std::size_t i = 0; i < n; ++i) { mem[i] = double(start -
double(i * inc)); }
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);
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);
335 const std::vector<double>& precisions,
336 double baserate, Rng& rng) {
338 const double kappa = kappas[
sample_one(kappas.size(), rng)];
339 double precision = precisions[
sample_one(precisions.size(), rng)];
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)];
348 return {kappa, precision};
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));
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)};
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) {
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;
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};
416 int pos_tp = 0, pos_fn = 0;
418 (cell == 0 ? pos_tp : pos_fn)++;
426 return {double(counts[0]), double(counts[1]), double(counts[2]), double(counts[3])};
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);
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;
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;
453 for (std::size_t i = 0; i < n; ++i) r_mean = r_mean + (x[i] - r_mean) / double(i + 1);
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();
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);
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.");
490 throw std::invalid_argument(
"OcSBaserate, ScSKappaThreshold, and ScSKappaMin must be positive.");
492 throw std::invalid_argument(
"ScSKappaThreshold must be greater than ScSKappaMin.");
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.");
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,
521 const auto kps =
rho_kps(baserate, opts, rng);
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);
527inline double rho(
double observed_kappa,
double baserate,
int test_set_length,
528 const RhoOptions& opts = {}, std::uint64_t seed = 5489u) {
530 return rho(observed_kappa, baserate, test_set_length, opts, rng);
#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
double precision
Definition rho.hpp:75
double kappa
Definition rho.hpp:75
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
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