binomial 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -1,5 +1,6 @@
1
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).
2
+ Binomial is a ruby gem for quickly calculating probabilities that are modeled by the [Binomial Theorem](http://en.wikipedia.org/wiki/Binomial_theorem).
3
+ The master branch of this repo may be rather buggy at times. For a stable version, use `gem install binomial` or download a tagged version.
3
4
  ## Usage
4
5
 
5
6
  ```ruby
@@ -13,6 +14,5 @@ calc.calculate
13
14
  ```
14
15
 
15
16
  ## Todo
16
- - Add support for cumulative binomial probabilities (X > 5, X 7, etc)
17
- - TESTING!
18
- - Documentation
17
+ Check the [Issue Tracker](https://github.com/loddy1234/binomial/issues) for an idea of what I think needs doing.
18
+ Feel free to add your own improvements! :smile:
data/Rakefile CHANGED
@@ -69,12 +69,7 @@ end
69
69
 
70
70
  desc "Open an irb session preloaded with this library"
71
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"
72
+ sh "irb -Ilib -r ./lib/#{name}.rb"
78
73
  end
79
74
 
80
75
  desc "Open an pry session preloaded with this library"
data/binomial.gemspec CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'binomial'
16
- s.version = '0.1.0'
17
- s.date = '2012-11-12'
16
+ s.version = '0.1.1'
17
+ s.date = '2012-12-01'
18
18
  s.rubyforge_project = 'binomial'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -39,7 +39,7 @@ Gem::Specification.new do |s|
39
39
  ## Specify any RDoc options here. You'll want to add your README and
40
40
  ## LICENSE files to the extra_rdoc_files list.
41
41
  s.rdoc_options = ["--charset=UTF-8"]
42
- s.extra_rdoc_files = %w[README.md]
42
+ s.extra_rdoc_files = %w[README.md LICENSE]
43
43
 
44
44
  ## List your runtime dependencies here. Runtime dependencies are those
45
45
  ## that are needed for an end user to actually USE your code.
@@ -60,7 +60,9 @@ Gem::Specification.new do |s|
60
60
  binomial.gemspec
61
61
  lib/binomial.rb
62
62
  lib/binomial/calculator.rb
63
+ lib/binomial/cumulative_calculator.rb
63
64
  lib/binomial/integer.rb
65
+ spec/calculator_spec.rb
64
66
  ]
65
67
  # = MANIFEST =
66
68
 
@@ -1,17 +1,35 @@
1
+ require 'binomial/integer'
1
2
  module Binomial
2
3
  # Params:
3
4
  # - trials: Total number of trials conducted
4
5
  # - probability: The probability of one success
5
6
  # - target: The desired amount of successes
6
7
  class Calculator
7
- ATTRS = [:trials, :probability, :target]
8
- ATTRS.each do |attribute|
9
- self.send(:attr_accessor, attribute)
10
- end
8
+ attr_accessor :trials, :probability, :target
11
9
 
12
10
  def initialize(params = {})
13
- ATTRS.each do |attribute|
14
- instance_eval "@#{attribute.to_s} = params[:#{attribute.to_s}]"
11
+ @trials = params[:trials]
12
+ @probability = params[:probability]
13
+ @target = params[:target]
14
+
15
+ if @trials <= 0
16
+ raise "Error: trials[#{@trials}] must be > 0"
17
+ end
18
+
19
+ if @target <= 0
20
+ raise "Error: target[#{@target}] must be > 0"
21
+ end
22
+
23
+ if @probability <= 0
24
+ raise "Error: probability[#{@probability}] must be > 0"
25
+ end
26
+
27
+ if @probability > 1
28
+ raise "Error: probability[#{@probability}] must be <= 1"
29
+ end
30
+
31
+ if @trials < @target
32
+ raise "Error: target[#{@target}] must be < trials[#{@trials}]"
15
33
  end
16
34
  end
17
35
 
@@ -0,0 +1,43 @@
1
+ require 'binomial/calculator'
2
+
3
+ module Binomial
4
+ class CumulativeCalculator < Calculator
5
+ attr_accessor :sign
6
+
7
+ # sign: P(X {{sign}} {{target}})
8
+ # The probability X is [equal to, less than, greater than, etc] the
9
+ # target when X = number of successes
10
+ def initialize(params = {})
11
+ super
12
+ @sign = params[:sign]
13
+ end
14
+
15
+ def calculate
16
+ total = 0
17
+ case @sign
18
+ when :equals
19
+ super
20
+ when :less_than
21
+ calc_cumulative @target - 1
22
+ when :less_than_or_equal_to
23
+ calc_cumulative @target
24
+ when :greater_than
25
+ 1 - calc_cumulative(@target)
26
+ when :greater_than_or_equal_to
27
+ 1 - calc_cumulative(@target - 1)
28
+ else
29
+ p "Case not handled?!"
30
+ end
31
+ end
32
+
33
+ def calc_cumulative(up_to)
34
+ total = 0.0
35
+ while up_to >= 0 do
36
+ calc = Calculator.new trials: @trials, probability: @probability, target: up_to
37
+ total += calc.calculate
38
+ up_to -= 1
39
+ end
40
+ total
41
+ end
42
+ end
43
+ end
data/lib/binomial.rb CHANGED
@@ -1,6 +1,5 @@
1
- require 'binomial/integer'
2
- require 'binomial/calculator'
1
+ require 'binomial/cumulative_calculator'
3
2
 
4
3
  module Binomial
5
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
6
5
  end
@@ -0,0 +1,75 @@
1
+ require 'binomial'
2
+
3
+ describe "Binomial::Calculator" do
4
+ describe "calculate" do
5
+ it "should validate probability is > 0" do
6
+ expect {
7
+ Binomial::Calculator.new(
8
+ :trials => 10,
9
+ :probability => 0,
10
+ :target => 3
11
+ ).calculate
12
+ }.to raise_error
13
+ end
14
+
15
+ it "should validate probability is < 1" do
16
+ expect {
17
+ Binomial::Calculator.new(
18
+ :trials => 10,
19
+ :probability => 1.1,
20
+ :target => 3
21
+ ).calculate
22
+ }.to raise_error
23
+ end
24
+
25
+ it "should succeed if probability is >0 and <1" do
26
+ expect {
27
+ Binomial::Calculator.new(
28
+ :trials => 10,
29
+ :probability => 0.2,
30
+ :target => 3
31
+ ).calculate
32
+ }.to_not raise_error
33
+ end
34
+
35
+ it "should validate target < trials" do
36
+ expect {
37
+ Binomial::Calculator.new(
38
+ :trials => 10,
39
+ :probability => 0.2,
40
+ :target => 11
41
+ ).calculate
42
+ }.to raise_error
43
+ end
44
+
45
+ it "should succeed if target < trials" do
46
+ expect {
47
+ Binomial::Calculator.new(
48
+ :trials => 10,
49
+ :probability => 0.2,
50
+ :target => 3
51
+ ).calculate
52
+ }.to_not raise_error
53
+ end
54
+
55
+ it "should validate target > 0" do
56
+ expect {
57
+ Binomial::Calculator.new(
58
+ :trials => 10,
59
+ :probability => 0.2,
60
+ :target => 0
61
+ ).calculate
62
+ }.to raise_error
63
+ end
64
+
65
+ it "should succeed if target > 0" do
66
+ expect {
67
+ Binomial::Calculator.new(
68
+ :trials => 10,
69
+ :probability => 0.2,
70
+ :target => 3
71
+ ).calculate
72
+ }.to_not raise_error
73
+ end
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binomial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-12 00:00:00.000000000 Z
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A gem for calculating probabilities modelled by the binomial theorem.
15
15
  email: lewis.odriscoll@gmail.com
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files:
19
19
  - README.md
20
+ - LICENSE
20
21
  files:
21
22
  - LICENSE
22
23
  - README.md
@@ -24,7 +25,9 @@ files:
24
25
  - binomial.gemspec
25
26
  - lib/binomial.rb
26
27
  - lib/binomial/calculator.rb
28
+ - lib/binomial/cumulative_calculator.rb
27
29
  - lib/binomial/integer.rb
30
+ - spec/calculator_spec.rb
28
31
  homepage: ''
29
32
  licenses: []
30
33
  post_install_message: