wilson_score 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +9 -0
- data/lib/wilson_score/version.rb +3 -0
- data/lib/wilson_score.rb +59 -0
- data/test/test_helper.rb +4 -0
- data/test/wilson_score_test.rb +29 -0
- data/wilson_score.gemspec +24 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a66381f037f35b8c6af86cd5c654f243378f620
|
4
|
+
data.tar.gz: 8d56f3a99766730ee9baecbb0c63b39be703fd0b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50d652c3613f6bba4dd2c685ad8c60943eb8d974276be0cee64c6fb2fab9aee514efb6b64471d9d862d527853d7d6f3cfdcc61a99aa84b376e654e3c5c6fc20f
|
7
|
+
data.tar.gz: 1734379e9da77cef40f164ba86525f98f35c991f0c3ff954e93ec82b459865a42eb5384b92289e7d8db56e618e3e6d974560f7952515f51cc29cfa0567d07199
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Wilson Score
|
2
|
+
|
3
|
+
Simple, dependency-free [Wilson score](http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval)
|
4
|
+
|
5
|
+
Inspired by [How Not To Sort By Average Rating](http://www.evanmiller.org/how-not-to-sort-by-average-rating.html) by Evan Miller
|
6
|
+
|
7
|
+
## How to Use
|
8
|
+
|
9
|
+
3 positive ratings out of 5 with 95% confidence
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
WilsonScore.interval(3, 5, 0.95)
|
13
|
+
```
|
14
|
+
|
15
|
+
[Continuity correction](http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval_with_continuity_correction) can improve the score, especially for a small number of samples (n < 30). Set the last paramter to true to use it.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
WilsonScore.interval(3, 5, 0.95, true)
|
19
|
+
```
|
20
|
+
|
21
|
+
## Star Ratings
|
22
|
+
|
23
|
+
Imagine you have a rating system with 1 to 5 stars.
|
24
|
+
|
25
|
+
There are two ratings, a 4 star and a 5 star.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
average_rating = 4.5
|
29
|
+
total_ratings = 2
|
30
|
+
rating_range = 1..5 # 1 to 5 stars
|
31
|
+
confidence = 0.95 # 95%
|
32
|
+
|
33
|
+
WilsonScore.rating_interval(average_rating, total_ratings, rating_range, confidence)
|
34
|
+
```
|
35
|
+
|
36
|
+
Again, you can set the last parameter to `true` for continuity correction.
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'wilson_score'
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
```sh
|
49
|
+
bundle
|
50
|
+
```
|
51
|
+
|
52
|
+
## Additional Resources
|
53
|
+
|
54
|
+
- http://www.vassarstats.net/prop1.html
|
55
|
+
- http://www.goproblems.com/test/wilson/wilson.php
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
60
|
+
|
61
|
+
- [Report bugs](https://github.com/instacart/wilson_score/issues)
|
62
|
+
- Fix bugs and [submit pull requests](https://github.com/instacart/wilson_score/pulls)
|
63
|
+
- Write, clarify, or fix documentation
|
64
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
data/lib/wilson_score.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "wilson_score/version"
|
2
|
+
|
3
|
+
module WilsonScore
|
4
|
+
|
5
|
+
# http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
|
6
|
+
def self.interval(k, n, confidence, correction = false)
|
7
|
+
z = pnorm(1 - (1 - confidence) / 2)
|
8
|
+
phat = k / n.to_f
|
9
|
+
if correction # continuity correction
|
10
|
+
a = 1.0 / (2 * (n + z**2))
|
11
|
+
b = 2*n*phat + z**2
|
12
|
+
c = z * Math.sqrt(z**2 - 1.0/n + 4*n*phat*(1 - phat) + (4*phat - 2)) + 1
|
13
|
+
([0, a * (b - c)].max)..([1, a * (b + c)].min)
|
14
|
+
else
|
15
|
+
a = 1.0 / (1 + z ** 2 / n)
|
16
|
+
b = phat + z ** 2 / (2 * n)
|
17
|
+
c = z * Math.sqrt((phat * (1 - phat) + z ** 2 / (4 * n)) / n)
|
18
|
+
(a * (b - c))..(a * (b + c))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.rating_interval(avg, n, score_range, confidence, correction = false)
|
23
|
+
min = score_range.first
|
24
|
+
max = score_range.last
|
25
|
+
range = max - min
|
26
|
+
interval = interval(n * (avg - min) / range, n, confidence, correction)
|
27
|
+
(min + range * interval.first)..(min + range * interval.last)
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
# from the statistics2 gem
|
33
|
+
# https://github.com/abscondment/statistics2/blob/master/lib/statistics2/base.rb
|
34
|
+
# inverse of normal distribution ([2])
|
35
|
+
# Pr( (-\infty, x] ) = qn -> x
|
36
|
+
def self.pnorm(qn)
|
37
|
+
b = [1.570796288, 0.03706987906, -0.8364353589e-3,
|
38
|
+
-0.2250947176e-3, 0.6841218299e-5, 0.5824238515e-5,
|
39
|
+
-0.104527497e-5, 0.8360937017e-7, -0.3231081277e-8,
|
40
|
+
0.3657763036e-10, 0.6936233982e-12]
|
41
|
+
|
42
|
+
if(qn < 0.0 || 1.0 < qn)
|
43
|
+
$stderr.printf("Error : qn <= 0 or qn >= 1 in pnorm()!\n")
|
44
|
+
return 0.0;
|
45
|
+
end
|
46
|
+
qn == 0.5 and return 0.0
|
47
|
+
|
48
|
+
w1 = qn
|
49
|
+
qn > 0.5 and w1 = 1.0 - w1
|
50
|
+
w3 = -Math.log(4.0 * w1 * (1.0 - w1))
|
51
|
+
w1 = b[0]
|
52
|
+
1.upto 10 do |i|
|
53
|
+
w1 += b[i] * w3**i;
|
54
|
+
end
|
55
|
+
qn > 0.5 and return Math.sqrt(w1 * w3)
|
56
|
+
-Math.sqrt(w1 * w3)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class TestWilsonScore < Minitest::Test
|
4
|
+
|
5
|
+
def test_wilson_score
|
6
|
+
interval = WilsonScore.interval(1, 2, 0.95)
|
7
|
+
assert_in_delta 0.0945, interval.first
|
8
|
+
assert_in_delta 0.9055, interval.last
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_continuity_correction
|
12
|
+
interval = WilsonScore.interval(1, 2, 0.95, true)
|
13
|
+
assert_in_delta 0.0267, interval.first
|
14
|
+
assert_in_delta 0.9733, interval.last
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_rating
|
18
|
+
interval = WilsonScore.rating_interval(5, 1, 1..5, 0.95)
|
19
|
+
assert_in_delta 1.8262, interval.first
|
20
|
+
assert_in_delta 5, interval.last
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_rating_advanced
|
24
|
+
interval = WilsonScore.rating_interval(3.7, 10, 1..5, 0.95)
|
25
|
+
assert_in_delta 2.4998, interval.first
|
26
|
+
assert_in_delta 4.5117, interval.last
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wilson_score/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wilson_score"
|
8
|
+
spec.version = WilsonScore::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
spec.description = %q{Simple, dependency-free Wilson score}
|
12
|
+
spec.summary = %q{Simple, dependency-free Wilson score}
|
13
|
+
spec.homepage = "https://github.com/instacart/wilson_score"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wilson_score
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Simple, dependency-free Wilson score
|
56
|
+
email:
|
57
|
+
- andrew@chartkick.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/wilson_score.rb
|
68
|
+
- lib/wilson_score/version.rb
|
69
|
+
- test/test_helper.rb
|
70
|
+
- test/wilson_score_test.rb
|
71
|
+
- wilson_score.gemspec
|
72
|
+
homepage: https://github.com/instacart/wilson_score
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.0
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Simple, dependency-free Wilson score
|
96
|
+
test_files:
|
97
|
+
- test/test_helper.rb
|
98
|
+
- test/wilson_score_test.rb
|