Skip to content

Instantly share code, notes, and snippets.

@zkan
Created July 16, 2017 14:55
Show Gist options
  • Save zkan/92e4d79432017a603f2a0c7d2822fc4d to your computer and use it in GitHub Desktop.
Save zkan/92e4d79432017a603f2a0c7d2822fc4d to your computer and use it in GitHub Desktop.
Lower bound of Wilson score confidence interval (3 rating scale)
"""
How Not To Sort By Average Rating:
http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
Note: Lower bound of a 95% confidence interval
"""
import math
import unittest
def get_wilson_score(positive, negative):
return ((positive + 1.9208) / (positive + negative) - 1.96 * math.sqrt(
(positive * negative) / (positive + negative) + 0.9604) /
(positive + negative)) / (1 + 3.8416 / (positive + negative))
def get_three_rating_scale_score(one, two, three):
positives = two * 0.5 + three
negatives = one + two * 0.5
return get_wilson_score(positives, negatives)
class WilsonScoreTest(unittest.TestCase):
def test_wilson_score_with_9_positives_and_1_negative_should_get_0_5958(
self
):
result = get_wilson_score(9, 1)
self.assertEqual(result, 0.5958436145024278)
def test_wilson_score_with_1_positive_and_0_negative_should_get_0_2065(
self
):
result = get_wilson_score(1, 0)
self.assertEqual(result, 0.20654329147389294)
def test_three_rating_scale_score_of_1_5_and_9_should_get_0_5137(
self
):
result = get_three_rating_scale_score(1, 5, 9)
self.assertEqual(result, 0.5137271335506134)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment