17inline double phyper(
int q,
int K,
int N_minus_K,
int n) {
18 if (q < 0)
return 0.0;
19 const int N = K + N_minus_K;
20 if (n > N || K < 0 || N_minus_K < 0)
return 0.0;
23 auto lnchoose = [](
int a,
int b) ->
double {
24 if (b < 0 || b > a)
return -1e300;
25 if (b == 0 || b == a)
return 0.0;
26 return std::lgamma(a + 1) - std::lgamma(b + 1) - std::lgamma(a - b + 1);
29 const double log_denom = lnchoose(N, n);
31 const int hi = std::min(q, std::min(K, n));
32 for (
int k = std::max(0, n - N_minus_K); k <= hi; ++k) {
33 double log_num = lnchoose(K, k) + lnchoose(N_minus_K, n - k);
34 sum += std::exp(log_num - log_denom);
36 return std::min(sum, 1.0);
48 if (n_pos < 0 || n_pos > n || n > N || alpha <= 0 || alpha >= 1)
49 throw std::domain_error(
"hyper_range: invalid input parameters");
51 const int n_neg = n - n_pos;
53 int M_upper = N - n_pos;
57 double p_lower =
phyper(n_pos, N - M_lower, M_lower, n);
58 if (p_lower < alpha / 2) {
59 int left = M_lower, right = M_upper;
60 while (right - left > 2) {
61 M_lower = (left + right + 1) / 2;
62 double p =
phyper(n_pos - 1, N - M_lower, M_lower, n);
63 if (p < alpha / 2) left = M_lower;
72 double p_upper = 1.0 -
phyper(n_pos, N - M_upper, M_upper, n);
73 if (p_upper < alpha / 2) {
74 int left = M_lower, right = M_upper;
75 while (right - left > 2) {
76 M_upper = (left + right + 1) / 2;
77 double p = 1.0 -
phyper(n_pos - 1, N - M_upper, M_upper, n);
78 if (p < alpha / 2) right = M_upper;
86 1.0 -
static_cast<double>(M_upper) / N,
87 (n > 0) ?
static_cast<double>(n_pos) / n : 0.0,
88 1.0 -
static_cast<double>(M_lower) / N
104 int N = 0,
int allowed_errors = 0,
105 double alpha = 0.05) {
106 if (alpha < 0.001 || alpha > 0.1)
return {};
107 if (x0 < 0.5 || x0 >= 1.0)
return {};
109 const int a = allowed_errors;
110 if (N <= 0) N = 1000000;
111 const int K =
static_cast<int>(std::ceil(x0 * N));
112 if (K == N)
return {N, 1.0, 1.0};
113 if (a >=
static_cast<int>(K * (1.0 - x0)))
return {};
117 int left = a, right = K;
119 while (left < right - 1) {
120 int n = (left + right + 1) / 2;
121 p = 1.0 -
phyper(n - 1 - a, K, N - K, n);
122 if (p > alpha) left = n;
125 const int n_out = right;
127 if (x1 <= x0 || x1 >= 1.0) x1 = (x0 + 1.0) / 2.0;
128 const double K1 = x1 * N;
129 const int K1i =
static_cast<int>(K1);
130 const double power = 1.0 -
phyper(n_out - 1 - a, K1i, N - K1i, n_out);
131 return {n_out, power,
static_cast<double>(n_out) / power};
145 double alpha = 0.05) {
146 if (alpha < 0.001 || alpha > 0.1)
return -1;
147 const int a = allowed_errors;
148 if (a >= n)
return -1;
149 if (N <= 0) N = 1000000;
150 if (n < 1 || n >= N)
return -1;
152 int left = 1, right = N;
154 while (right - left > 1) {
155 int K = (left + right + 1) / 2;
156 double p = 1.0 -
phyper(n - 1 - a, K, N - K, n);
157 x =
static_cast<double>(K) / N;
158 if (p < alpha) left = K;
175inline std::vector<MetricRangeRow>
177 int N,
double baserate,
double alpha = 0.05) {
179 auto metrics_from_bFF = [](
double b,
double FDR,
double FOR) ->
Metrics {
180 const double fp_ = FDR * b;
181 const double tp_ = b - fp_;
182 const double fn_ = FOR * (1.0 - b);
183 const double tn_ = 1.0 - fp_ - tp_ - fn_;
189 static_cast<int>(tp + fp), N, alpha);
194 static_cast<int>(fn + tn), N, alpha);
196 1.0 - one_minus_FOR.
mean,
197 1.0 - one_minus_FOR.
lower};
Definition comparison.hpp:12
std::vector< MetricRangeRow > metrics_range(double tp, double fp, double fn, double tn, int N, double baserate, double alpha=0.05)
Definition inference.hpp:176
PsResult ps_proportion(double x0, double x1=0, int N=0, int allowed_errors=0, double alpha=0.05)
Definition inference.hpp:103
double ps_est_proportion(int n, int N=0, int allowed_errors=0, double alpha=0.05)
Definition inference.hpp:144
Metrics metrics_from_cm(const ConfusionMatrix &cm)
Definition metrics.hpp:81
Interval hyper_range(int n_pos, int n, int N, double alpha=0.05)
Definition inference.hpp:47
double phyper(int q, int K, int N_minus_K, int n)
Definition inference.hpp:17
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:29
double accuracy
Definition metrics.hpp:37
double kappa
Definition metrics.hpp:40
double recall
Definition metrics.hpp:36
Definition inference.hpp:97
double power
Definition inference.hpp:99
double cost
Definition inference.hpp:100
int n
Definition inference.hpp:98