From dbb2dc6f3acadca6d1744a595fe0cd755367c62c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 16:30:12 +0100 Subject: Switch to wilson interval --- src/main/evaluation/binomial_ci.cpp | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/evaluation/binomial_ci.cpp (limited to 'src/main/evaluation/binomial_ci.cpp') diff --git a/src/main/evaluation/binomial_ci.cpp b/src/main/evaluation/binomial_ci.cpp new file mode 100644 index 0000000..c381c05 --- /dev/null +++ b/src/main/evaluation/binomial_ci.cpp @@ -0,0 +1,45 @@ +// +// Created by thecharlesblake on 20/06/18. +// + +#include +#include +#include "binomial_ci.h" + +using std::pow; +using std::min; +using std::max; +using std::pair; + +pair binomial_ci::wilson(int s_, int f_) { + auto s = static_cast(s_); + auto f = static_cast(f_); + double z = 1.96; + double z2 = pow(z, 2); + double n = s + f; + double p = (s + (z2 / 2.0)) / (n + z2); + double half_ci = (z / (n + z2)) * sqrt(((s * f) / n) + (z2 / 4.0)); + return { p - half_ci + , p + half_ci }; +} + +pair binomial_ci::wilson(int s, int f, int i) { + pair a = wilson(s, f + i); + pair b = wilson(s + i, f); + return {a.first, b.second}; +} + +pair binomial_ci::agresti_coull(int s, int f) { + double z = 1.96; + double n_ = s + f + pow(z, 2); + double p = (s + pow(z, 2)/2)/n_; + double half_ci = z * sqrt((p - pow(p, 2))/n_); + return { max(0.0, p - half_ci) + , min(1.0, p + half_ci) }; +} + +pair binomial_ci::agresti_coull(int s, int f, int i) { + pair a = agresti_coull(s, f + i); + pair b = agresti_coull(s + i, f); + return {a.first, b.second}; +} -- cgit v1.2.1