Skip to content

Instantly share code, notes, and snippets.

@mhl
Created July 1, 2011 17:10
Show Gist options
  • Select an option

  • Save mhl/1058979 to your computer and use it in GitHub Desktop.

Select an option

Save mhl/1058979 to your computer and use it in GitHub Desktop.
Naive pairwise comparisons power
#!/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