20 #ifndef _BOUNDS_BINOMIAL_PROPORTIONS_HPP_
21 #define _BOUNDS_BINOMIAL_PROPORTIONS_HPP_
115 if (n == 0) {
return 0.0; }
116 else if (k == 0) {
return 0.0; }
117 else if (k == 1) {
return (exact_lower_bound_on_p_k_eq_1(n, delta_of_num_stdevs(num_std_devs))); }
118 else if (k == n) {
return (exact_lower_bound_on_p_k_eq_n(n, delta_of_num_stdevs(num_std_devs))); }
120 double x = abramowitz_stegun_formula_26p5p22((n - k) + 1.0,
static_cast<double>(k), (-1.0 * num_std_devs));
150 if (n == 0) {
return 1.0; }
151 else if (k == n) {
return 1.0; }
152 else if (k == (n - 1)) {
153 return (exact_upper_bound_on_p_k_eq_minusone(n, delta_of_num_stdevs(num_std_devs)));
156 return (exact_upper_bound_on_p_k_eq_zero(n, delta_of_num_stdevs(num_std_devs)));
159 double x = abramowitz_stegun_formula_26p5p22(
static_cast<double>(n - k), k + 1.0, num_std_devs);
172 if (n == 0) {
return 0.5; }
173 else {
return ((
double) k / (
double) n); }
181 static inline double erf(
double x) {
182 if (x < 0.0) {
return (-1.0 * (erf_of_nonneg(-1.0 * x))); }
183 else {
return (erf_of_nonneg(x)); }
192 return (0.5 * (1.0 + (
erf(x / (sqrt(2.0))))));
196 static inline void check_inputs(uint64_t n, uint64_t k) {
197 if (k > n) {
throw std::invalid_argument(
"K cannot exceed N"); }
202 static inline double erf_of_nonneg(
double x) {
210 static const double a1 = 0.0705230784;
211 static const double a3 = 0.0092705272;
212 static const double a5 = 0.0002765672;
213 static const double a2 = 0.0422820123;
214 static const double a4 = 0.0001520143;
215 static const double a6 = 0.0000430638;
216 const double x2 = x * x;
217 const double x3 = x2 * x;
218 const double x4 = x2 * x2;
219 const double x5 = x2 * x3;
220 const double x6 = x3 * x3;
221 const double sum = ( 1.0
228 const double sum2 = sum * sum;
229 const double sum4 = sum2 * sum2;
230 const double sum8 = sum4 * sum4;
231 const double sum16 = sum8 * sum8;
232 return (1.0 - (1.0 / sum16));
235 static inline double delta_of_num_stdevs(
double kappa) {
252 static inline double abramowitz_stegun_formula_26p5p22(
double a,
double b,
double yp) {
253 const double b2m1 = (2.0 * b) - 1.0;
254 const double a2m1 = (2.0 * a) - 1.0;
255 const double lambda = ((yp * yp) - 3.0) / 6.0;
256 const double htmp = (1.0 / a2m1) + (1.0 / b2m1);
257 const double h = 2.0 / htmp;
258 const double term1 = (yp * (sqrt(h + lambda))) / h;
259 const double term2 = (1.0 / b2m1) - (1.0 / a2m1);
260 const double term3 = (lambda + (5.0 / 6.0)) - (2.0 / (3.0 * h));
261 const double w = term1 - (term2 * term3);
262 const double xp = a / (a + (b * (exp(2.0 * w))));
268 static inline double exact_upper_bound_on_p_k_eq_zero(uint64_t n,
double delta) {
269 return (1.0 - pow(delta, (1.0 / n)));
272 static inline double exact_lower_bound_on_p_k_eq_n(uint64_t n,
double delta) {
273 return (pow(delta, (1.0 / n)));
276 static inline double exact_lower_bound_on_p_k_eq_1(uint64_t n,
double delta) {
277 return (1.0 - pow((1.0 - delta), (1.0 / n)));
280 static inline double exact_upper_bound_on_p_k_eq_minusone(uint64_t n,
double delta) {
281 return (pow((1.0 - delta), (1.0 / n)));
Confidence intervals for binomial proportions.
Definition: bounds_binomial_proportions.hpp:81
static double estimate_unknown_p(uint64_t n, uint64_t k)
Computes an estimate of an unknown binomial proportion.
Definition: bounds_binomial_proportions.hpp:170
static double approximate_upper_bound_on_p(uint64_t n, uint64_t k, double num_std_devs)
Computes upper bound of approximate Clopper-Pearson confidence interval for a binomial proportion.
Definition: bounds_binomial_proportions.hpp:148
static double normal_cdf(double x)
Computes an approximation to normal_cdf(x).
Definition: bounds_binomial_proportions.hpp:191
static double erf(double x)
Computes an approximation to the erf() function.
Definition: bounds_binomial_proportions.hpp:181
static double approximate_lower_bound_on_p(uint64_t n, uint64_t k, double num_std_devs)
Computes lower bound of approximate Clopper-Pearson confidence interval for a binomial proportion.
Definition: bounds_binomial_proportions.hpp:113
DataSketches namespace.
Definition: binomial_bounds.hpp:38