wilson_interval 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb25f95060ae5c27f138c0bc4069a87b7c97b4b7
4
+ data.tar.gz: b08c3c8da284f9db7ea8640f12e0d33ba74535aa
5
+ SHA512:
6
+ metadata.gz: f54eb254fb3bcb8eba4732be69897bed10b0d923eec11d41b35dec8340dea150de5bfe4e7a9bb2c73bab2bb3d9e533b16f1b91e2c541d78dcdd9c8bef5b9f352
7
+ data.tar.gz: c510afe2f439306628709489ad4ab38f1fc56d3ddfc05db44ac8105e364363a05424f69c2d6ff441cfd7652eb562304f5cb0e907b0a566e3066c67a90d453525
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1 @@
1
+ wilson_interval
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'statistics2', '>= 0.54'
4
+
5
+ group :development do
6
+ gem 'jeweler', '>= 1.8.6'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rspec'
11
+ end
@@ -0,0 +1,61 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ diff-lcs (1.2.4)
7
+ faraday (0.8.8)
8
+ multipart-post (~> 1.2.0)
9
+ git (1.2.5)
10
+ github_api (0.10.1)
11
+ addressable
12
+ faraday (~> 0.8.1)
13
+ hashie (>= 1.2)
14
+ multi_json (~> 1.4)
15
+ nokogiri (~> 1.5.2)
16
+ oauth2
17
+ hashie (2.0.5)
18
+ highline (1.6.19)
19
+ httpauth (0.2.0)
20
+ jeweler (1.8.6)
21
+ builder
22
+ bundler (~> 1.0)
23
+ git (>= 1.2.5)
24
+ github_api (= 0.10.1)
25
+ highline (>= 1.6.15)
26
+ nokogiri (= 1.5.10)
27
+ rake
28
+ rdoc
29
+ jwt (0.1.8)
30
+ multi_json (>= 1.5)
31
+ multi_json (1.7.8)
32
+ multi_xml (0.5.5)
33
+ multipart-post (1.2.0)
34
+ nokogiri (1.5.10)
35
+ oauth2 (0.9.2)
36
+ faraday (~> 0.8)
37
+ httpauth (~> 0.2)
38
+ jwt (~> 0.1.4)
39
+ multi_json (~> 1.0)
40
+ multi_xml (~> 0.5)
41
+ rack (~> 1.2)
42
+ rack (1.5.2)
43
+ rake (10.0.4)
44
+ rdoc (4.0.0)
45
+ rspec (2.14.1)
46
+ rspec-core (~> 2.14.0)
47
+ rspec-expectations (~> 2.14.0)
48
+ rspec-mocks (~> 2.14.0)
49
+ rspec-core (2.14.4)
50
+ rspec-expectations (2.14.0)
51
+ diff-lcs (>= 1.1.3, < 2.0)
52
+ rspec-mocks (2.14.2)
53
+ statistics2 (0.54)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ jeweler (>= 1.8.6)
60
+ rspec
61
+ statistics2 (>= 0.54)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Magnus von Koeller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # wilson_interval
2
+
3
+ Calculate Wilson confidence intervals with minimum fuss.
4
+
5
+ ## Usage
6
+
7
+ You always need to specify ```:total``` and ```:confidence```. You also need to specify *either* ```:p_hat``` *or* ```:successes```. The ```:confidence``` option is optional and defaults to ```0.95```.
8
+
9
+ ```ruby
10
+ wilson = Wilson::Interval.new(p_hat: 0.5, total: 1000, confidence: 0.95)
11
+ wilson.lower_bound.round(4) => 0.4691
12
+ wilson.upper_bound.round(4) => 0.5309
13
+
14
+ wilson = Wilson::Interval.new(successes: 500, total: 1000, confidence: 0.95)
15
+ wilson.lower_bound.round(4) => 0.4691
16
+ wilson.upper_bound.round(4) => 0.5309
17
+ ```
18
+
19
+ ## Credits
20
+
21
+ Based on code found at [evanmiller.org](http://www.evanmiller.org/how-not-to-sort-by-average-rating.html). Adapted according to [Wikipedia](http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval).
22
+
23
+ ## Contributing to wilson_interval
24
+
25
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
26
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
27
+ * Fork the project.
28
+ * Start a feature/bugfix branch.
29
+ * Commit and push until you are happy with your contribution.
30
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
31
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
32
+
33
+ ## Copyright
34
+
35
+ Copyright (c) 2013 Magnus von Koeller. Licensed under the MIT license. See LICENSE.txt for further details.
36
+
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "wilson_interval"
18
+ gem.homepage = "http://github.com/magnusvk/wilson_interval"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Calculate Wilson confidence intervals with minimum fuss.}
21
+ gem.description = %Q{Makes it super easy to calculate Wilson confidence intervals.}
22
+ gem.email = "magnus@vonkoeller.de"
23
+ gem.authors = ["Magnus von Koeller"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,43 @@
1
+ require 'statistics2'
2
+
3
+ module Wilson
4
+ class Interval
5
+ def initialize(options)
6
+ @o = options
7
+ @o[:confidence] ||= 0.95
8
+
9
+ raise "Need to specify :n" unless @o[:total]
10
+ unless @o[:p_hat]
11
+ raise "Need to specify :p_hat or :successes" unless @o[:successes]
12
+ @o[:p_hat] = @o[:successes].to_f / @o[:total] unless @o[:total] == 0
13
+ end
14
+ end
15
+
16
+ def lower_bound
17
+ return nil if @o[:total] == 0
18
+ (center - plus_minus) / denominator
19
+ end
20
+
21
+ def upper_bound
22
+ return nil if @o[:total] == 0
23
+ (center + plus_minus) / denominator
24
+ end
25
+
26
+ private
27
+ def z
28
+ Statistics2.pnormaldist(1-(1-@o[:confidence])/2)
29
+ end
30
+
31
+ def center
32
+ @o[:p_hat] + z*z/(2*@o[:total])
33
+ end
34
+
35
+ def plus_minus
36
+ z * Math.sqrt((@o[:p_hat]*(1-@o[:p_hat])+z*z/(4*@o[:total]))/@o[:total])
37
+ end
38
+
39
+ def denominator
40
+ 1 + z*z / @o[:total]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,57 @@
1
+ require 'wilson_interval'
2
+
3
+ describe "Wilson::Interval" do
4
+ it "returns correct z" do
5
+ wilson = Wilson::Interval.new(p_hat: 0.5, total: 1000, confidence: 0.99)
6
+ wilson.send(:z).round(8).should == 2.57582933
7
+
8
+ wilson = Wilson::Interval.new(p_hat: 0.5, total: 1000, confidence: 0.95)
9
+ wilson.send(:z).round(8).should == 1.95996397
10
+
11
+ wilson = Wilson::Interval.new(p_hat: 0.5, total: 1000, confidence: 0.50)
12
+ wilson.send(:z).round(8).should == 0.67448976
13
+
14
+ wilson = Wilson::Interval.new(p_hat: 0.5, total: 1000, confidence: 0.25)
15
+ wilson.send(:z).round(8).should == 0.31863936
16
+ end
17
+
18
+ it "correctly calculates p_hat" do
19
+ wilson = Wilson::Interval.new(p_hat: 0.5, total: 1000, confidence: 0.95)
20
+ wilson.lower_bound.round(4).should == 0.4691
21
+ wilson.upper_bound.round(4).should == 0.5309
22
+
23
+ wilson = Wilson::Interval.new(successes: 500, total: 1000, confidence: 0.95)
24
+ wilson.lower_bound.round(4).should == 0.4691
25
+ wilson.upper_bound.round(4).should == 0.5309
26
+ end
27
+
28
+ it "correctly calculates lower and upper bounds" do
29
+ wilson = Wilson::Interval.new(successes: 500, total: 1000, confidence: 0.99)
30
+ wilson.lower_bound.round(4).should == 0.4594
31
+ wilson.upper_bound.round(4).should == 0.5406
32
+
33
+ wilson = Wilson::Interval.new(successes: 500, total: 1000, confidence: 0.95)
34
+ wilson.lower_bound.round(4).should == 0.4691
35
+ wilson.upper_bound.round(4).should == 0.5309
36
+
37
+ wilson = Wilson::Interval.new(successes: 500, total: 1000, confidence: 0.50)
38
+ wilson.lower_bound.round(4).should == 0.4893
39
+ wilson.upper_bound.round(4).should == 0.5107
40
+
41
+ wilson = Wilson::Interval.new(successes: 1000, total: 1000, confidence: 0.95)
42
+ wilson.lower_bound.round(4).should == 0.9962
43
+ wilson.upper_bound.round(4).should == 1
44
+
45
+ wilson = Wilson::Interval.new(successes: 0, total: 1000, confidence: 0.95)
46
+ wilson.lower_bound.round(4).should == 0
47
+ wilson.upper_bound.round(4).should == 0.0038
48
+
49
+ wilson = Wilson::Interval.new(successes: 250, total: 1000, confidence: 0.95)
50
+ wilson.lower_bound.round(4).should == 0.2242
51
+ wilson.upper_bound.round(4).should == 0.2778
52
+
53
+ wilson = Wilson::Interval.new(successes: 750, total: 1000, confidence: 0.95)
54
+ wilson.lower_bound.round(4).should == 0.7222
55
+ wilson.upper_bound.round(4).should == 0.7758
56
+ end
57
+ end
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "wilson_interval"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Magnus von Koeller"]
12
+ s.date = "2013-08-06"
13
+ s.description = "Makes it super easy to calculate Wilson confidence intervals."
14
+ s.email = "magnus@vonkoeller.de"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".ruby-gemset",
22
+ ".ruby-version",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/wilson_interval.rb",
30
+ "spec/wilson_interval_spec.rb",
31
+ "wilson_interval.gemspec"
32
+ ]
33
+ s.homepage = "http://github.com/magnusvk/wilson_interval"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "2.0.3"
37
+ s.summary = "Calculate Wilson confidence intervals with minimum fuss."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 4
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<statistics2>, [">= 0.54"])
44
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.6"])
45
+ else
46
+ s.add_dependency(%q<statistics2>, [">= 0.54"])
47
+ s.add_dependency(%q<jeweler>, [">= 1.8.6"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<statistics2>, [">= 0.54"])
51
+ s.add_dependency(%q<jeweler>, [">= 1.8.6"])
52
+ end
53
+ end
54
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wilson_interval
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Magnus von Koeller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: statistics2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0.54'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0.54'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.6
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.6
41
+ description: Makes it super easy to calculate Wilson confidence intervals.
42
+ email: magnus@vonkoeller.de
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files:
46
+ - LICENSE.txt
47
+ - README.md
48
+ files:
49
+ - .document
50
+ - .ruby-gemset
51
+ - .ruby-version
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - VERSION
58
+ - lib/wilson_interval.rb
59
+ - spec/wilson_interval_spec.rb
60
+ - wilson_interval.gemspec
61
+ homepage: http://github.com/magnusvk/wilson_interval
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Calculate Wilson confidence intervals with minimum fuss.
85
+ test_files: []