Created
July 1, 2011 17:10
-
-
Save mhl/1058979 to your computer and use it in GitHub Desktop.
Naive pairwise comparisons power
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby -w | |
| require 'statistics2' | |
| class Numeric | |
| def to_percent | |
| "#{(self * 100).round}%" | |
| end | |
| end | |
| def puts_interval(wins, n, certainty) | |
| z = Statistics2.pnormaldist(certainty) | |
| phat = 1.0 * wins / n.to_f | |
| denominator = 1 + z*z / n.to_f | |
| estimate = (phat + z*z / (2.0 * n)) / denominator | |
| plusminus = z * Math.sqrt( (phat * (1-phat) + (z*z) / (4.0*n)) / n ) / denominator | |
| lower = estimate - plusminus | |
| higher = estimate + plusminus | |
| puts <<EOM | |
| With #{wins} wins out of #{n} battles, you can be #{certainty.to_percent} | |
| sure that the "true" value is between: #{lower.to_percent} and #{higher.to_percent} | |
| EOM | |
| end | |
| puts_interval 75, 100, 0.95 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment