binomial 0.1.1 → 0.1.2
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/Gemfile +3 -0
- data/Rakefile +20 -2
- data/binomial.gemspec +9 -2
- data/lib/binomial.rb +1 -1
- data/lib/binomial/calculator.rb +13 -19
- data/lib/binomial/cumulative_calculator.rb +2 -2
- data/spec/calculator_spec.rb +47 -35
- data/spec/cumulative_calculator_spec.rb +107 -0
- data/spec/integer_spec.rb +27 -0
- data/spec/spec_helper.rb +2 -0
- metadata +39 -3
data/Gemfile
ADDED
data/Rakefile
CHANGED
@@ -54,8 +54,8 @@ end
|
|
54
54
|
|
55
55
|
desc "Generate SimpleCov test coverage and open in your browser"
|
56
56
|
task :coverage do
|
57
|
-
|
58
|
-
|
57
|
+
ENV['COVERAGE'] = 'true'
|
58
|
+
Rake::Task["spec"].execute
|
59
59
|
sh "open coverage/index.html"
|
60
60
|
end
|
61
61
|
|
@@ -83,7 +83,25 @@ end
|
|
83
83
|
#
|
84
84
|
#############################################################################
|
85
85
|
|
86
|
+
namespace :clean do
|
87
|
+
desc "Deletes the files generated by SimpleCov"
|
88
|
+
task :coverage do
|
89
|
+
sh "rm -rf coverage"
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Remove RDoc HTML files"
|
93
|
+
task :docs do
|
94
|
+
Rake::Task["clobber_rdoc"].invoke
|
95
|
+
end
|
96
|
+
|
97
|
+
desc "Delete all built versions of the gem"
|
98
|
+
task :built do
|
99
|
+
sh "rm -rf pkg"
|
100
|
+
end
|
101
|
+
end
|
86
102
|
|
103
|
+
desc "Delete SimpleCov reports, RDoc HTML and built versions"
|
104
|
+
task :clean => ["clean:coverage", "clean:docs", "clean:built"]
|
87
105
|
|
88
106
|
#############################################################################
|
89
107
|
#
|
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.
|
17
|
-
s.date = '2012-12-
|
16
|
+
s.version = '0.1.2'
|
17
|
+
s.date = '2012-12-04'
|
18
18
|
s.rubyforge_project = 'binomial'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
@@ -54,6 +54,7 @@ Gem::Specification.new do |s|
|
|
54
54
|
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
55
55
|
# = MANIFEST =
|
56
56
|
s.files = %w[
|
57
|
+
Gemfile
|
57
58
|
LICENSE
|
58
59
|
README.md
|
59
60
|
Rakefile
|
@@ -63,10 +64,16 @@ Gem::Specification.new do |s|
|
|
63
64
|
lib/binomial/cumulative_calculator.rb
|
64
65
|
lib/binomial/integer.rb
|
65
66
|
spec/calculator_spec.rb
|
67
|
+
spec/cumulative_calculator_spec.rb
|
68
|
+
spec/integer_spec.rb
|
69
|
+
spec/spec_helper.rb
|
66
70
|
]
|
67
71
|
# = MANIFEST =
|
68
72
|
|
69
73
|
## Test files will be grabbed from the file list. Make sure the path glob
|
70
74
|
## matches what you actually use.
|
71
75
|
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
76
|
+
|
77
|
+
s.add_development_dependency "rspec"
|
78
|
+
s.add_development_dependency "simplecov"
|
72
79
|
end
|
data/lib/binomial.rb
CHANGED
data/lib/binomial/calculator.rb
CHANGED
@@ -12,25 +12,20 @@ module Binomial
|
|
12
12
|
@probability = params[:probability]
|
13
13
|
@target = params[:target]
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
check_positive(:@trials)
|
16
|
+
check_positive(:@target)
|
17
|
+
check_positive(:@probability)
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
19
|
+
@probability <= 1 or raise "Error: probability[#{@probability}] must be <= 1"
|
20
|
+
@trials >= @target or raise "Error: target[#{@target}] must be < trials[#{@trials}]"
|
21
|
+
end
|
30
22
|
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
# param: name of instance variable WITH leading @
|
24
|
+
def check_positive(inst_var)
|
25
|
+
value = instance_variable_get(inst_var)
|
26
|
+
var_name = inst_var.to_s.delete('@')
|
27
|
+
value > 0 or
|
28
|
+
raise "Error: '#{var_name}' must be > 0, but was: #{value}"
|
34
29
|
end
|
35
30
|
|
36
31
|
def model
|
@@ -39,8 +34,7 @@ module Binomial
|
|
39
34
|
|
40
35
|
# A representation of the equation performed
|
41
36
|
def equation
|
42
|
-
"P(X=#{@target}) = #{@trials}C#{@target} * #{@probability}^#{@target} *"
|
43
|
-
+ " #{1 - @probability}^#{@trials - @target} = #{calculate().to_s}"
|
37
|
+
"P(X=#{@target}) = #{@trials}C#{@target} * #{@probability}^#{@target} * #{1 - @probability}^#{@trials - @target} = #{calculate}"
|
44
38
|
end
|
45
39
|
|
46
40
|
# Calculates the result
|
@@ -26,13 +26,13 @@ module Binomial
|
|
26
26
|
when :greater_than_or_equal_to
|
27
27
|
1 - calc_cumulative(@target - 1)
|
28
28
|
else
|
29
|
-
|
29
|
+
raise "Error: invalid sign '#{@sign.to_s}' supplied"
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
def calc_cumulative(up_to)
|
34
34
|
total = 0.0
|
35
|
-
while up_to
|
35
|
+
while up_to > 0 do
|
36
36
|
calc = Calculator.new trials: @trials, probability: @probability, target: up_to
|
37
37
|
total += calc.calculate
|
38
38
|
up_to -= 1
|
data/spec/calculator_spec.rb
CHANGED
@@ -1,48 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
1
2
|
require 'binomial'
|
2
3
|
|
3
4
|
describe "Binomial::Calculator" do
|
4
5
|
describe "calculate" do
|
6
|
+
def binom_calc(trials,prob,target)
|
7
|
+
Binomial::Calculator.new(
|
8
|
+
:trials => trials,
|
9
|
+
:probability => prob,
|
10
|
+
:target => target
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
5
14
|
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
|
15
|
+
expect { binom_calc(10,0,3).calculate }.to raise_error
|
13
16
|
end
|
14
17
|
|
15
18
|
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
|
19
|
+
expect { binom_calc(10,1.1,3).calculate }.to raise_error
|
23
20
|
end
|
24
21
|
|
25
22
|
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
|
23
|
+
expect { binom_calc(10, 0.2, 3).calculate }.to_not raise_error
|
33
24
|
end
|
34
25
|
|
35
|
-
it "should validate
|
26
|
+
it "should validate trial > 0" do
|
36
27
|
expect {
|
37
28
|
Binomial::Calculator.new(
|
38
|
-
:trials =>
|
29
|
+
:trials => 0,
|
39
30
|
:probability => 0.2,
|
40
|
-
:target =>
|
31
|
+
:target => 1
|
41
32
|
).calculate
|
42
33
|
}.to raise_error
|
43
34
|
end
|
44
35
|
|
45
|
-
it "should succeed if
|
36
|
+
it "should succeed if trial > 0" do
|
46
37
|
expect {
|
47
38
|
Binomial::Calculator.new(
|
48
39
|
:trials => 10,
|
@@ -52,24 +43,45 @@ describe "Binomial::Calculator" do
|
|
52
43
|
}.to_not raise_error
|
53
44
|
end
|
54
45
|
|
46
|
+
it "should validate target < trials" do
|
47
|
+
expect { binom_calc(10, 0.2, 11).calculate }.to raise_error
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should succeed if target < trials" do
|
51
|
+
expect { binom_calc(10, 0.2, 3).calculate }.to_not raise_error
|
52
|
+
end
|
53
|
+
|
55
54
|
it "should validate target > 0" do
|
55
|
+
expect { binom_calc(10, 0.2, 0).calculate }.to raise_error
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should succeed if target > 0" do
|
59
|
+
expect { binom_calc(10, 0.2, 3).calculate }.to_not raise_error
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should print out a representation of the equation performed" do
|
56
63
|
expect {
|
57
64
|
Binomial::Calculator.new(
|
58
|
-
:trials =>
|
59
|
-
:probability => 0.
|
60
|
-
:target =>
|
61
|
-
).
|
62
|
-
}.
|
65
|
+
:trials => 2,
|
66
|
+
:probability => 0.5,
|
67
|
+
:target => 2
|
68
|
+
).equation
|
69
|
+
}.to_not raise_error
|
63
70
|
end
|
64
71
|
|
65
|
-
it "should
|
72
|
+
it "should print out a representation of the model" do
|
66
73
|
expect {
|
67
74
|
Binomial::Calculator.new(
|
68
|
-
:trials =>
|
69
|
-
:probability => 0.
|
70
|
-
:target =>
|
71
|
-
).
|
75
|
+
:trials => 2,
|
76
|
+
:probability => 0.5,
|
77
|
+
:target => 2
|
78
|
+
).model
|
72
79
|
}.to_not raise_error
|
73
80
|
end
|
81
|
+
|
82
|
+
it "should produce the correct results" do
|
83
|
+
binom_calc(2,0.5,2).calculate.should == 0.25
|
84
|
+
binom_calc(2,0.5,1).calculate.should == 0.5
|
85
|
+
end
|
74
86
|
end
|
75
87
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'binomial'
|
3
|
+
|
4
|
+
describe "Binomial::CumulativeCalculator" do
|
5
|
+
describe "calculate" do
|
6
|
+
def binom_calc(sign, trials, prob, target)
|
7
|
+
Binomial::CumulativeCalculator.new(
|
8
|
+
:sign => sign,
|
9
|
+
:trials => trials,
|
10
|
+
:probability => prob,
|
11
|
+
:target => target
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should validate probability is > 0" do
|
16
|
+
expect {
|
17
|
+
binom_calc(:equals, 10, 0, 3).calculate
|
18
|
+
}.to raise_error
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should validate probability is < 1" do
|
22
|
+
expect {
|
23
|
+
binom_calc(:equals, 10, 1.1, 3).calculate
|
24
|
+
}.to raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should succeed if probability is >0 and <1" do
|
28
|
+
expect {
|
29
|
+
binom_calc(:equals, 10, 0.2, 3).calculate
|
30
|
+
}.to_not raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should validate trial > 0" do
|
34
|
+
expect {
|
35
|
+
binom_calc(:equals, 0, 0.2, 1).calculate
|
36
|
+
}.to raise_error
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should succeed if trial > 0" do
|
40
|
+
expect {
|
41
|
+
binom_calc(:equals, 10, 0.2, 3).calculate
|
42
|
+
}.to_not raise_error
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should validate target < trials" do
|
46
|
+
expect {
|
47
|
+
binom_calc(:equals, 10, 0.2, 11).calculate
|
48
|
+
}.to raise_error
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should succeed if target < trials" do
|
52
|
+
expect {
|
53
|
+
binom_calc(:equals, 10, 0.2, 3).calculate
|
54
|
+
}.to_not raise_error
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should validate target > 0" do
|
58
|
+
expect {
|
59
|
+
binom_calc(:equals, 10, 0.2, 0).calculate
|
60
|
+
}.to raise_error
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should succeed if target > 0" do
|
64
|
+
expect {
|
65
|
+
binom_calc(:equals, 10, 0.2, 3).calculate
|
66
|
+
}.to_not raise_error
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should print out a representation of the equation performed" do
|
70
|
+
expect {
|
71
|
+
binom_calc(:equals, 2, 0.5, 2).equation
|
72
|
+
}.to_not raise_error
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should print out a representation of the model" do
|
76
|
+
expect {
|
77
|
+
binom_calc(:equals, 2, 0.5, 2).model
|
78
|
+
}.to_not raise_error
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should raise an exception when sign not recognized" do
|
82
|
+
expect {
|
83
|
+
binom_calc('=', 2, 0.5, 2).calculate
|
84
|
+
}.to raise_error
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should produce the correct cumulative results" do
|
88
|
+
binom_calc(:equals, 2, 0.5, 2).calculate.should == 0.25
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should produce the correct cumulative results with less_than" do
|
92
|
+
binom_calc(:less_than, 5, 0.5, 5).calculate.should == 0.9375
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should produce the correct cumulative results with less_than_or_equal" do
|
96
|
+
binom_calc(:less_than_or_equal_to, 5, 0.5, 5).calculate.should == 0.96875
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should produce the correct cumulative results with greater_than" do
|
100
|
+
binom_calc(:greater_than, 5, 0.5, 5).calculate.should == 0.03125
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should produce the correct cumulative results with greater_than_or_equal" do
|
104
|
+
binom_calc(:greater_than_or_equal_to, 5, 0.5, 5).calculate.should == 0.0625
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'binomial'
|
3
|
+
|
4
|
+
describe "Integer" do
|
5
|
+
describe "#choose" do
|
6
|
+
|
7
|
+
it("should be correct when n is 0") { 0.choose(13).should == 0 }
|
8
|
+
it("should be correct when k is 0") { 5.choose(0).should == 1 }
|
9
|
+
it("should be correct when n and k are 0") { 0.choose(0).should == 1 }
|
10
|
+
|
11
|
+
it("should be correct when n is large") do
|
12
|
+
59922092947261214857303164514091691.choose(7).should ==
|
13
|
+
550399817117861542559553374183305880444453789686940825844792654274423774740226138320805430744153144877776025703863504009266563916236130288377323887215902973940458468633181977032752322590578526112480717050575426228710841090337272655324460690
|
14
|
+
end
|
15
|
+
|
16
|
+
it("should be correct when k is large") do
|
17
|
+
pending "algorithm is too slow"
|
18
|
+
11.choose(314159)
|
19
|
+
.should == 0
|
20
|
+
end
|
21
|
+
|
22
|
+
it("should be correct when n and k are equal") do
|
23
|
+
314.choose(314).should == 1
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
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.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,40 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
13
|
-
dependencies:
|
12
|
+
date: 2012-12-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: simplecov
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
14
46
|
description: A gem for calculating probabilities modelled by the binomial theorem.
|
15
47
|
email: lewis.odriscoll@gmail.com
|
16
48
|
executables: []
|
@@ -19,6 +51,7 @@ extra_rdoc_files:
|
|
19
51
|
- README.md
|
20
52
|
- LICENSE
|
21
53
|
files:
|
54
|
+
- Gemfile
|
22
55
|
- LICENSE
|
23
56
|
- README.md
|
24
57
|
- Rakefile
|
@@ -28,6 +61,9 @@ files:
|
|
28
61
|
- lib/binomial/cumulative_calculator.rb
|
29
62
|
- lib/binomial/integer.rb
|
30
63
|
- spec/calculator_spec.rb
|
64
|
+
- spec/cumulative_calculator_spec.rb
|
65
|
+
- spec/integer_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
31
67
|
homepage: ''
|
32
68
|
licenses: []
|
33
69
|
post_install_message:
|