diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-09-24 11:29:31 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2013-09-24 11:29:31 +0100 |
commit | d8deb8418b4cd26c68eb1301959e156c19b111e2 (patch) | |
tree | 99c346db95d17be9c5105ce47d5f3ac8e943e952 /lib/confidence_intervals.rb | |
parent | 8459314b691f5b02277035219cd58f510d100a77 (diff) | |
parent | 75542416a1cc36b353ade557b1bc4f729b02423a (diff) |
Merge branch 'release/0.14'0.14
Conflicts:
locale/bg/app.po
locale/fr/app.po
locale/fr_CA/app.po
locale/he_IL/app.po
locale/hr/app.po
locale/it/app.po
locale/nb_NO/app.po
locale/pl/app.po
locale/sv/app.po
locale/vi/app.po
Diffstat (limited to 'lib/confidence_intervals.rb')
-rw-r--r-- | lib/confidence_intervals.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/confidence_intervals.rb b/lib/confidence_intervals.rb new file mode 100644 index 000000000..9fe38045a --- /dev/null +++ b/lib/confidence_intervals.rb @@ -0,0 +1,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 |