binomial 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Tom Preston-Werner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Binomial
2
+ Binomial is a ruby gem for quickly calculating probabilities that are modelled by the [Binomial Theorem](http://en.wikipedia.org/wiki/Binomial_theorem).
3
+ ## Usage
4
+
5
+ ```ruby
6
+ require 'binomial'
7
+
8
+ calc = Binomial::Calculator.new trials: 10, probability: 0.2, target: 3
9
+
10
+ # Returns the probability of 3 successes occuring during 10 trials where the
11
+ # probability of a success is 0.2
12
+ calc.calculate
13
+ ```
14
+
15
+ ## Todo
16
+ - Add support for cumulative binomial probabilities (X > 5, X ≤ 7, etc)
17
+ - TESTING!
18
+ - Documentation
data/Rakefile ADDED
@@ -0,0 +1,159 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :spec
47
+
48
+ require "rspec/core/rake_task"
49
+
50
+ RSpec::Core::RakeTask.new(:spec) do |spec|
51
+ spec.pattern = 'spec/**/*_spec.rb'
52
+ spec.rspec_opts = ['--backtrace']
53
+ end
54
+
55
+ desc "Generate SimpleCov test coverage and open in your browser"
56
+ task :coverage do
57
+ sh "rm -fr coverage"
58
+ sh "ruby -e \"require 'simplecov'\; SimpleCov.start\" "
59
+ sh "open coverage/index.html"
60
+ end
61
+
62
+ require 'rdoc/task'
63
+ Rake::RDocTask.new do |rdoc|
64
+ rdoc.rdoc_dir = 'rdoc'
65
+ rdoc.title = "#{name} #{version}"
66
+ rdoc.rdoc_files.include('README*')
67
+ rdoc.rdoc_files.include('lib/**/*.rb')
68
+ end
69
+
70
+ desc "Open an irb session preloaded with this library"
71
+ task :console do
72
+ sh "irb -rubygems -r ./lib/#{name}.rb"
73
+ end
74
+
75
+ desc "Open an irb session preloaded with this library"
76
+ task :irb do
77
+ sh "irb -rubygems -r ./lib/#{name}.rb"
78
+ end
79
+
80
+ desc "Open an pry session preloaded with this library"
81
+ task :pry do
82
+ sh "pry -r ./lib/#{name}.rb"
83
+ end
84
+
85
+ #############################################################################
86
+ #
87
+ # Custom tasks (add your own tasks here)
88
+ #
89
+ #############################################################################
90
+
91
+
92
+
93
+ #############################################################################
94
+ #
95
+ # Packaging tasks
96
+ #
97
+ #############################################################################
98
+
99
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
100
+ task :release => :build do
101
+ unless `git branch` =~ /^\* master$/
102
+ puts "You must be on the master branch to release!"
103
+ exit!
104
+ end
105
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
106
+ sh "git tag v#{version}"
107
+ sh "git push origin master"
108
+ sh "git push origin v#{version}"
109
+ sh "gem push pkg/#{name}-#{version}.gem"
110
+ end
111
+
112
+ desc "Build #{gem_file} into the pkg directory"
113
+ task :build => :gemspec do
114
+ sh "mkdir -p pkg"
115
+ sh "gem build #{gemspec_file}"
116
+ sh "mv #{gem_file} pkg"
117
+ end
118
+
119
+ desc "Generate #{gemspec_file}"
120
+ task :gemspec => :validate do
121
+ # read spec file and split out manifest section
122
+ spec = File.read(gemspec_file)
123
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
124
+
125
+ # replace name version and date
126
+ replace_header(head, :name)
127
+ replace_header(head, :version)
128
+ replace_header(head, :date)
129
+ #comment this out if your rubyforge_project has a different name
130
+ replace_header(head, :rubyforge_project)
131
+
132
+ # determine file list from git ls-files
133
+ files = `git ls-files`.
134
+ split("\n").
135
+ sort.
136
+ reject { |file| file =~ /^\./ }.
137
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
138
+ map { |file| " #{file}" }.
139
+ join("\n")
140
+
141
+ # piece file back together and write
142
+ manifest = " s.files = %w[\n#{files}\n ]\n"
143
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
144
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
145
+ puts "Updated #{gemspec_file}"
146
+ end
147
+
148
+ desc "Validate #{gemspec_file}"
149
+ task :validate do
150
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
151
+ unless libfiles.empty?
152
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
153
+ exit!
154
+ end
155
+ unless Dir['VERSION*'].empty?
156
+ puts "A `VERSION` file at root level violates Gem best practices."
157
+ exit!
158
+ end
159
+ end
data/binomial.gemspec ADDED
@@ -0,0 +1,70 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'binomial'
16
+ s.version = '0.1.0'
17
+ s.date = '2012-11-12'
18
+ s.rubyforge_project = 'binomial'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "A gem for calculating binomial probabilities."
23
+ s.description = "A gem for calculating probabilities modelled by the binomial theorem."
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Lewis O'Driscoll"]
29
+ s.email = 'lewis.odriscoll@gmail.com'
30
+ s.homepage = ''
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## If your gem includes any executables, list them here.
37
+ # s.executables = ["name"]
38
+
39
+ ## Specify any RDoc options here. You'll want to add your README and
40
+ ## LICENSE files to the extra_rdoc_files list.
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.extra_rdoc_files = %w[README.md]
43
+
44
+ ## List your runtime dependencies here. Runtime dependencies are those
45
+ ## that are needed for an end user to actually USE your code.
46
+ # s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"])
47
+
48
+ ## List your development dependencies here. Development dependencies are
49
+ ## those that are only needed during development
50
+ # s.add_development_dependency('DEVDEPNAME', [ ">= 1.1.0", "< 2.0.0"])
51
+
52
+ ## Leave this section as-is. It will be automatically generated from the
53
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
54
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
55
+ # = MANIFEST =
56
+ s.files = %w[
57
+ LICENSE
58
+ README.md
59
+ Rakefile
60
+ binomial.gemspec
61
+ lib/binomial.rb
62
+ lib/binomial/calculator.rb
63
+ lib/binomial/integer.rb
64
+ ]
65
+ # = MANIFEST =
66
+
67
+ ## Test files will be grabbed from the file list. Make sure the path glob
68
+ ## matches what you actually use.
69
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
70
+ end
@@ -0,0 +1,35 @@
1
+ module Binomial
2
+ # Params:
3
+ # - trials: Total number of trials conducted
4
+ # - probability: The probability of one success
5
+ # - target: The desired amount of successes
6
+ class Calculator
7
+ ATTRS = [:trials, :probability, :target]
8
+ ATTRS.each do |attribute|
9
+ self.send(:attr_accessor, attribute)
10
+ end
11
+
12
+ def initialize(params = {})
13
+ ATTRS.each do |attribute|
14
+ instance_eval "@#{attribute.to_s} = params[:#{attribute.to_s}]"
15
+ end
16
+ end
17
+
18
+ def model
19
+ "X~B(#{@trials}, #{@probability})"
20
+ end
21
+
22
+ # A representation of the equation performed
23
+ def equation
24
+ "P(X=#{@target}) = #{@trials}C#{@target} * #{@probability}^#{@target} *"
25
+ + " #{1 - @probability}^#{@trials - @target} = #{calculate().to_s}"
26
+ end
27
+
28
+ # Calculates the result
29
+ def calculate
30
+ @trials.choose(@target) *
31
+ (@probability ** @target) *
32
+ ((1 - @probability) ** (@trials - @target))
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ # Integer extension to add choose function
2
+ class Integer
3
+ # binomial coefficient: n C k
4
+ def choose(k)
5
+ # n!/(n-k)!
6
+ top = (self-k+1..self).inject(1, &:*)
7
+ # k!
8
+ bottom = (2..k).inject(1, &:*)
9
+ top / bottom
10
+ end
11
+ end
data/lib/binomial.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'binomial/integer'
2
+ require 'binomial/calculator'
3
+
4
+ module Binomial
5
+ VERSION = '0.1.0'
6
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: binomial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lewis O'Driscoll
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem for calculating probabilities modelled by the binomial theorem.
15
+ email: lewis.odriscoll@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - binomial.gemspec
25
+ - lib/binomial.rb
26
+ - lib/binomial/calculator.rb
27
+ - lib/binomial/integer.rb
28
+ homepage: ''
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --charset=UTF-8
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: binomial
49
+ rubygems_version: 1.8.22
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: A gem for calculating binomial probabilities.
53
+ test_files: []