Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Math::DomainError with continuity correction #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/wilson_score.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ def self.interval(k, n, *args)
if correction # continuity correction
a = 2 * (n + z2)
b = 2*n*phat + z2
c = z * Math.sqrt(z2 - 1.0/n + 4*n*phat*(1 - phat) + (4*phat - 2)) + 1
d = z * Math.sqrt(z2 - 1.0/n + 4*n*phat*(1 - phat) - (4*phat - 2)) + 1
lower = phat == 0 ? 0 : [0, (b - c) / a].max
upper = phat == 1 ? 1 : [1, (b + d) / a].min
lower = phat == 0 ? 0 : [0, (b - (z * Math.sqrt(z2 - 1.0/n + 4*n*phat*(1 - phat) + (4*phat - 2)) + 1)) / a].max
upper = phat == 1 ? 1 : [1, (b + (z * Math.sqrt(z2 - 1.0/n + 4*n*phat*(1 - phat) - (4*phat - 2)) + 1)) / a].min
lower..upper
else
a = 1 + z2 / n
Expand Down
10 changes: 10 additions & 0 deletions test/wilson_score_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ def test_continuity_correction_one_three
assert_in_delta 0.8747, interval.last
end

def test_continuity_correction_p_zero
assert_equal 0, WilsonScore.interval(0, 25, confidence: 0.8).first
assert_equal 1, WilsonScore.rating_interval(1, 25, 1..5, confidence: 0.8).first
end

def test_continuity_correction_p_one
assert_equal 1, WilsonScore.interval(25, 25, confidence: 0.8).last
assert_equal 5, WilsonScore.rating_interval(5, 25, 1..5, confidence: 0.8).last
end

def test_rating
interval = WilsonScore.rating_interval(5, 1, 1..5, correction: false)
assert_in_delta 1.8262, interval.first
Expand Down
2 changes: 1 addition & 1 deletion wilson_score.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
end