wilson_score 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -3
- data/lib/wilson_score.rb +14 -9
- data/lib/wilson_score/version.rb +1 -1
- data/test/wilson_score_test.rb +24 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be14e070c2299eb4ca4bfb98fff17bc037778702
|
4
|
+
data.tar.gz: 811d3aed20e01c0f4498c5ef40ac7129f8322eff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 640f472abf05bd6f9acc79ee4644136387346a15d409c60d4c0dd5f739da5aaef2e4178bcf79d954822c5e4477676c577eaccde796b8b76f37ea5ade87440925
|
7
|
+
data.tar.gz: ee62a52fc1f1049a4c3c2659afa4621fd651ed2b923c16b4c4d26fea556f753b286e2466f3446d2a7b02c8e459284a8ac6e5f7c1b6c58ec25132d3d3fb2be74a
|
data/README.md
CHANGED
@@ -20,9 +20,9 @@ WilsonScore.interval(3, 5, 0.95, true)
|
|
20
20
|
|
21
21
|
## Star Ratings
|
22
22
|
|
23
|
-
|
23
|
+
You have a rating system where users can rate products from 1 to 5 stars.
|
24
24
|
|
25
|
-
|
25
|
+
A product has two ratings - one 4 star and one 5 star.
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
average_rating = 4.5
|
@@ -30,9 +30,12 @@ total_ratings = 2
|
|
30
30
|
rating_range = 1..5 # 1 to 5 stars
|
31
31
|
confidence = 0.95 # 95%
|
32
32
|
|
33
|
-
WilsonScore.rating_interval(average_rating, total_ratings, rating_range, confidence)
|
33
|
+
interval = WilsonScore.rating_interval(average_rating, total_ratings, rating_range, confidence)
|
34
|
+
lower_bound = interval.first
|
34
35
|
```
|
35
36
|
|
37
|
+
Use the lower bound of the interval to sort items.
|
38
|
+
|
36
39
|
Again, you can set the last parameter to `true` for continuity correction.
|
37
40
|
|
38
41
|
## Installation
|
data/lib/wilson_score.rb
CHANGED
@@ -4,18 +4,23 @@ module WilsonScore
|
|
4
4
|
|
5
5
|
# http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
|
6
6
|
def self.interval(k, n, confidence, correction = false)
|
7
|
-
z = pnorm(1 - (1 - confidence) / 2)
|
7
|
+
z = pnorm(1 - (1 - confidence) / 2.0)
|
8
8
|
phat = k / n.to_f
|
9
|
+
z2 = z**2
|
9
10
|
if correction # continuity correction
|
10
|
-
a =
|
11
|
-
b = 2*n*phat +
|
12
|
-
c = z * Math.sqrt(
|
13
|
-
|
11
|
+
a = 2 * (n + z2)
|
12
|
+
b = 2*n*phat + z2
|
13
|
+
c = z * Math.sqrt(z2 - 1.0/n + 4*n*phat*(1 - phat) + (4*phat - 2)) + 1
|
14
|
+
d = z * Math.sqrt(z2 - 1.0/n + 4*n*phat*(1 - phat) - (4*phat - 2)) + 1
|
15
|
+
# hack for phat = 0 or 1
|
16
|
+
lower = phat == 0 ? 0 : (b - c) / a
|
17
|
+
upper = phat == 1 ? 1 : (b + d) / a
|
18
|
+
([0, lower].max)..([1, upper].min)
|
14
19
|
else
|
15
|
-
a = 1
|
16
|
-
b = phat +
|
17
|
-
c = z * Math.sqrt((phat * (1 - phat) +
|
18
|
-
(
|
20
|
+
a = 1 + z2 / n
|
21
|
+
b = phat + z2 / (2 * n)
|
22
|
+
c = z * Math.sqrt((phat * (1 - phat) + z2 / (4 * n)) / n)
|
23
|
+
((b - c) / a)..((b + c) / a)
|
19
24
|
end
|
20
25
|
end
|
21
26
|
|
data/lib/wilson_score/version.rb
CHANGED
data/test/wilson_score_test.rb
CHANGED
@@ -14,6 +14,30 @@ class TestWilsonScore < Minitest::Test
|
|
14
14
|
assert_in_delta 0.9733, interval.last
|
15
15
|
end
|
16
16
|
|
17
|
+
def test_continuity_correction_zero_one
|
18
|
+
interval = WilsonScore.interval(0, 1, 0.95, true)
|
19
|
+
assert_in_delta 0, interval.first
|
20
|
+
assert_in_delta 0.9454, interval.last
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_continuity_correction_one_fifty
|
24
|
+
interval = WilsonScore.interval(1, 50, 0.95, true)
|
25
|
+
assert_in_delta 0.0010, interval.first
|
26
|
+
assert_in_delta 0.1201, interval.last
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_continuity_correction_one_one
|
30
|
+
interval = WilsonScore.interval(1, 1, 0.95, true)
|
31
|
+
assert_in_delta 0.0546, interval.first
|
32
|
+
assert_in_delta 1, interval.last
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_continuity_correction_one_three
|
36
|
+
interval = WilsonScore.interval(1, 3, 0.95, true)
|
37
|
+
assert_in_delta 0.0176, interval.first
|
38
|
+
assert_in_delta 0.8747, interval.last
|
39
|
+
end
|
40
|
+
|
17
41
|
def test_rating
|
18
42
|
interval = WilsonScore.rating_interval(5, 1, 1..5, 0.95)
|
19
43
|
assert_in_delta 1.8262, interval.first
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wilson_score
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|