aboutsummaryrefslogtreecommitdiffstats
path: root/lib/confidence_intervals.rb
blob: 9fe38045ac1bef7635f14e0d12385289f18dbaa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Calculate the confidence interval for a samples from a binonial
# distribution using Wilson's score interval.  For more theoretical
# details, please see:
#
#  http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson%20score%20interval
#
# This is a variant of the function suggested here:
#
#  http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
#
# total: the total number of observations
# successes: the subset of those observations that were "successes"
# power: for a 95% confidence interval, this should be 0.05
#
# The naive proportion is (successes / total).  This returns an array
# with the proportions that represent the lower and higher confidence
# intervals around that.

require 'statistics2'

def ci_bounds(successes, total, power)
    if total == 0
        raise RuntimeError, "Can't calculate the CI for 0 observations"
    end
    z = Statistics2.pnormaldist(1 - power/2)
    phat = successes.to_f/total
    offset = z*Math.sqrt((phat*(1 - phat) + z*z/(4*total))/total)
    denominator = 1 + z*z/total
    return [(phat + z*z/(2*total) - offset)/denominator,
            (phat + z*z/(2*total) + offset)/denominator]
end