bernoulli 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -8,13 +8,13 @@ end
8
8
 
9
9
  desc 'Remove pkg directory'
10
10
  task :clean do
11
- rm_rf 'pkg/'
11
+ rm_rf 'pkg/'
12
12
  end
13
13
 
14
14
  spec = eval File.read('bernoulli.gemspec')
15
15
 
16
16
  desc "Remove and uninstall #{spec.name}-#{spec.version}.gem from system gems"
17
17
  task :uninstall => :clean do
18
- sh 'gem uninstall bernoulli'
18
+ sh 'gem uninstall bernoulli'
19
19
  end
20
20
 
data/bernoulli.gemspec CHANGED
@@ -1,24 +1,25 @@
1
1
  require File.expand_path('../lib/bernoulli/version', __FILE__)
2
2
 
3
3
  Gem::Specification.new do |spec|
4
-
5
- spec.name = 'bernoulli'
6
- spec.version = Bernoulli::VERSION
7
- spec.summary = 'Binomial experiments library'
8
- spec.description = 'A library that allows calculation of probibilities and properties of binomial experiments like coin tossing'
9
- spec.homepage = 'http://mkonutgan.github.com/bernoulli'
10
-
11
- spec.author = 'Mikael Konutgan'
12
- spec.email = 'mkonutgan@shortmail.com'
13
-
14
- spec.required_ruby_version = '>= 1.9.2'
15
-
16
- spec.files = `git ls-files`.split($\)
17
- spec.test_files = spec.files.grep(/test/)
18
-
19
- spec.add_development_dependency 'minitest', '~> 3.0.1'
20
- spec.add_development_dependency 'rake', '~> 0.9.2.2'
21
- spec.add_development_dependency 'bundler', '~> 1.1.4'
22
-
4
+
5
+ spec.name = 'bernoulli'
6
+ spec.version = Bernoulli::VERSION
7
+ spec.summary = 'Binomial experiments library'
8
+ spec.description = 'A library that allows calculation of probibilities and properties of binomial experiments like coin tossing'
9
+ spec.homepage = 'http://mkonutgan.github.com/bernoulli'
10
+
11
+ spec.author = 'Mikael Konutgan'
12
+ spec.email = 'mkonutgan@shortmail.com'
13
+
14
+ spec.required_ruby_version = '>= 1.8.7'
15
+ spec.required_rubygems_version = '>= 1.3.6'
16
+
17
+ spec.files = `git ls-files`.split($\)
18
+ spec.test_files = spec.files.grep(/test/)
19
+
20
+ spec.add_development_dependency 'minitest', '~> 3.0.1'
21
+ spec.add_development_dependency 'rake', '~> 0.9.2.2'
22
+ spec.add_development_dependency 'bundler', '~> 1.1.4'
23
+
23
24
  end
24
25
 
@@ -1,23 +1,23 @@
1
1
  module Math
2
2
 
3
- def self.factorial(n)
4
-
5
- if n < 0 or not n.is_a? Integer
6
- raise Math::DomainError, 'Numerical argument is out of domain - "factorial"'
7
- end
8
- if n == 0
9
- 1
10
- else
11
- (1..n).inject(:*)
12
- end
13
- end
14
-
15
- def self.binomial(n, k)
16
- if n < 0 or n < k or not (n.is_a? Integer and k.is_a? Integer)
17
- raise Math::DomainError, 'Numerical argument is out of domain - "binomial"'
18
- end
19
- factorial(n)/(factorial(k) * factorial(n - k))
20
- end
3
+ def self.factorial(n)
4
+
5
+ if n < 0 or not n.is_a? Integer
6
+ raise Math::DomainError, 'Numerical argument is out of domain - "factorial"'
7
+ end
8
+ if n == 0
9
+ 1
10
+ else
11
+ (1..n).inject(:*)
12
+ end
13
+ end
14
+
15
+ def self.binomial(n, k)
16
+ if n < 0 or n < k or not (n.is_a? Integer and k.is_a? Integer)
17
+ raise Math::DomainError, 'Numerical argument is out of domain - "binomial"'
18
+ end
19
+ factorial(n)/(factorial(k) * factorial(n - k))
20
+ end
21
21
 
22
22
  end
23
23
 
@@ -1,4 +1,4 @@
1
1
  module Bernoulli
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
4
4
 
data/lib/bernoulli.rb CHANGED
@@ -1,86 +1,85 @@
1
1
  require 'bernoulli/math'
2
2
  require 'bernoulli/version'
3
- require 'csv'
4
3
 
5
4
  module Bernoulli
6
- class Distribution
7
-
8
- def initialize(n, p)
9
- if n < 0 or p > 1.0 or p < 0.0 or not n.is_a? Integer
10
- raise 'Could not initialize Bernoulli experiment'
11
- end
12
- @n = n
13
- @p = p
14
- end
15
-
16
- def to_s
17
- "#<Bernoulli::Distribution @n=#{@n}, @p=#{@p}>"
18
- end
19
-
20
- def probability(k)
21
- Math.binomial(@n, k) * @p**k * (1 - @p)**(@n - k)
22
- end
23
-
24
- def [](k)
25
- if k.is_a? Integer
26
- raise Math::DomainError if not (k <= @n and k >= 0)
27
- probability(k).to_f
28
- elsif k.is_a? Range
29
- raise Math::DomainError if not (k.begin >= 0 and k.end <= @n)
30
- sum = 0
31
- k.each do |i|
32
- sum += probability(i)
33
- end
34
- sum.to_f
35
- else
36
- raise TypeError
37
- end
38
- end
39
-
40
- def sample
41
- s = []
42
- @n.times do
43
- s << (rand < @p ? 1 : 0)
44
- end
45
- s
46
- end
47
-
48
- def sample_value
49
- sample.count(1)
50
- end
51
- alias :sv :sample_value
52
-
53
- def expected_value
54
- @n * @p
55
- end
56
- alias :ev :expected_value
57
-
58
- def variance
59
- @n * @p * (1 - @p)
60
- end
61
- alias :v :variance
62
-
63
- def standard_deviation
64
- Math.sqrt(variance)
65
- end
66
- alias :sd :standard_deviation
67
-
68
- def skewness
69
- (1 - 2 * @p) / (Math.sqrt(@n * @p * (1 - @p)))
70
- end
71
-
72
- def excess
73
- (1 - 6 * @p * (1 - @p)) / (@n * @p * (1 - @p))
74
- end
75
-
76
- def table
77
- CSV.generate do |csv|
78
- (0..@n).each do |n|
79
- csv << [n, probability(n)]
80
- end
81
- end
82
- end
83
-
84
- end
5
+ class Distribution
6
+
7
+ def initialize(n, p)
8
+ if n < 0 or p > 1.0 or p < 0.0 or not n.is_a? Integer
9
+ raise 'Could not initialize Bernoulli experiment'
10
+ end
11
+ @n = n
12
+ @p = p
13
+ end
14
+
15
+ def to_s
16
+ "#<Bernoulli::Distribution @n=#@n, @p=#@p>"
17
+ end
18
+
19
+ def probability(k)
20
+ Math.binomial(@n, k) * @p**k * (1 - @p)**(@n - k)
21
+ end
22
+
23
+ def [](k)
24
+ if k.is_a? Integer
25
+ raise Math::DomainError if not (k <= @n and k >= 0)
26
+ probability(k).to_f
27
+ elsif k.is_a? Range
28
+ raise Math::DomainError if not (k.begin >= 0 and k.end <= @n)
29
+ sum = 0
30
+ k.each do |i|
31
+ sum += probability(i)
32
+ end
33
+ sum.to_f
34
+ else
35
+ raise TypeError
36
+ end
37
+ end
38
+
39
+ def sample
40
+ s = []
41
+ @n.times do
42
+ s << (rand < @p ? 1 : 0)
43
+ end
44
+ s
45
+ end
46
+
47
+ def sample_value
48
+ sample.count(1)
49
+ end
50
+ alias :sv :sample_value
51
+
52
+ def expected_value
53
+ @n * @p
54
+ end
55
+ alias :ev :expected_value
56
+
57
+ def variance
58
+ @n * @p * (1 - @p)
59
+ end
60
+ alias :v :variance
61
+
62
+ def standard_deviation
63
+ Math.sqrt(variance)
64
+ end
65
+ alias :sd :standard_deviation
66
+
67
+ def skewness
68
+ (1 - 2 * @p) / (Math.sqrt(@n * @p * (1 - @p)))
69
+ end
70
+
71
+ def excess
72
+ (1 - 6 * @p * (1 - @p)) / (@n * @p * (1 - @p))
73
+ end
74
+
75
+ def table
76
+ csv = ""
77
+ (0..@n).each do |n|
78
+ csv << n.to_s << "," << probability(n).to_s << "\n"
79
+ end
80
+ csv
81
+ end
82
+
83
+ end
85
84
  end
86
85
 
@@ -3,59 +3,59 @@ require 'minitest/pride'
3
3
  require 'bernoulli'
4
4
 
5
5
  class DistributionTest < MiniTest::Unit::TestCase
6
-
7
- def test_new_experiment
8
- assert_raises RuntimeError do
9
- Bernoulli::Distribution.new(10, 1.1)
10
- end
11
- assert_raises RuntimeError do
12
- Bernoulli::Distribution.new(-10, 0.7)
13
- end
14
- assert_raises RuntimeError do
15
- Bernoulli::Distribution.new(4.5, 0.2)
16
- end
17
- end
18
-
19
- def setup
20
- @x = Bernoulli::Distribution.new(180, 1.0/6)
21
- @y = Bernoulli::Distribution.new(2, 0.5)
22
- @z = Bernoulli::Distribution.new(4, 0.5)
23
- end
24
-
25
- def test_to_s
26
- assert_equal "#<Bernoulli::Distribution @n=2, @p=0.5>", @y.to_s
27
- end
28
-
29
- def test_coin_toss
30
- assert_equal 0.5, @y[1]
31
- assert_equal 0.25, @y[2]
32
- assert_equal 1.0, @y[0..2]
33
- end
34
-
35
- def test_expected_value
36
- assert_equal 30, @x.expected_value
37
- end
38
-
39
- def test_variance
40
- assert_equal 25, @x.variance
41
- end
42
-
43
- def test_standard_deviation
44
- assert_equal 5, @x.standard_deviation
45
- end
46
-
47
- def test_skewness
48
- assert_in_delta 0.1333333333333333, @x.skewness, 1.0e-7
49
- end
50
-
51
- def test_excess
52
- assert_in_delta 0.0066666666666667, @x.excess, 1.0e-7
53
- end
54
-
55
- def test_table
56
- str = "0,0.0625\n1,0.25\n2,0.375\n3,0.25\n4,0.0625\n"
57
- assert_equal str, @z.table
58
- end
59
-
6
+
7
+ def test_new_experiment
8
+ assert_raises RuntimeError do
9
+ Bernoulli::Distribution.new(10, 1.1)
10
+ end
11
+ assert_raises RuntimeError do
12
+ Bernoulli::Distribution.new(-10, 0.7)
13
+ end
14
+ assert_raises RuntimeError do
15
+ Bernoulli::Distribution.new(4.5, 0.2)
16
+ end
17
+ end
18
+
19
+ def setup
20
+ @x = Bernoulli::Distribution.new(180, 1.0/6)
21
+ @y = Bernoulli::Distribution.new(2, 0.5)
22
+ @z = Bernoulli::Distribution.new(4, 0.5)
23
+ end
24
+
25
+ def test_to_s
26
+ assert_equal "#<Bernoulli::Distribution @n=2, @p=0.5>", @y.to_s
27
+ end
28
+
29
+ def test_coin_toss
30
+ assert_equal 0.5, @y[1]
31
+ assert_equal 0.25, @y[2]
32
+ assert_equal 1.0, @y[0..2]
33
+ end
34
+
35
+ def test_expected_value
36
+ assert_equal 30, @x.expected_value
37
+ end
38
+
39
+ def test_variance
40
+ assert_equal 25, @x.variance
41
+ end
42
+
43
+ def test_standard_deviation
44
+ assert_equal 5, @x.standard_deviation
45
+ end
46
+
47
+ def test_skewness
48
+ assert_in_delta 0.1333333333333333, @x.skewness, 1.0e-7
49
+ end
50
+
51
+ def test_excess
52
+ assert_in_delta 0.0066666666666667, @x.excess, 1.0e-7
53
+ end
54
+
55
+ def test_table
56
+ str = "0,0.0625\n1,0.25\n2,0.375\n3,0.25\n4,0.0625\n"
57
+ assert_equal str, @z.table
58
+ end
59
+
60
60
  end
61
61
 
data/test/test_math.rb CHANGED
@@ -3,37 +3,37 @@ require 'minitest/pride'
3
3
  require 'bernoulli'
4
4
 
5
5
  class MathTest < MiniTest::Unit::TestCase
6
-
7
- def test_factorial
8
- assert_raises(Math::DomainError) { Math.factorial(-1) }
9
- assert_raises(Math::DomainError) { Math.factorial(2.4) }
10
- assert_equal 1, Math.factorial(0)
11
- assert_equal 1, Math.factorial(1)
12
- assert_equal 120, Math.factorial(5)
13
- assert_equal 263130836933693530167218012160000000, Math.factorial(32)
14
- end
15
-
16
- def test_binomial
17
- pairs = [[-1, 0], [2, 4], [-2, -1], [1.2, 3.4], [12, 50]]
18
- pairs.each do |pair|
19
- assert_raises Math::DomainError do
20
- Math.binomial *pair
21
- end
22
- end
23
- assert_equal 1, Math.binomial(0, 0)
24
- (1..20).each do |k|
25
- assert_equal 1, Math.binomial(k, 0)
26
- assert_equal 1, Math.binomial(k, k)
27
- assert_equal k, Math.binomial(k, 1)
28
- assert_equal k, Math.binomial(k, k - 1)
29
- end
30
- (15..20).each do |n|
31
- (5..15).each do |k|
32
- assert_equal Math.binomial(n, k), Math.binomial(n, n - k)
33
- end
34
- end
35
- assert_equal 100947, Math.binomial(23, 17)
36
- end
37
-
6
+
7
+ def test_factorial
8
+ assert_raises(Math::DomainError) { Math.factorial(-1) }
9
+ assert_raises(Math::DomainError) { Math.factorial(2.4) }
10
+ assert_equal 1, Math.factorial(0)
11
+ assert_equal 1, Math.factorial(1)
12
+ assert_equal 120, Math.factorial(5)
13
+ assert_equal 263130836933693530167218012160000000, Math.factorial(32)
14
+ end
15
+
16
+ def test_binomial
17
+ pairs = [[-1, 0], [2, 4], [-2, -1], [1.2, 3.4], [12, 50]]
18
+ pairs.each do |pair|
19
+ assert_raises Math::DomainError do
20
+ Math.binomial *pair
21
+ end
22
+ end
23
+ assert_equal 1, Math.binomial(0, 0)
24
+ (1..20).each do |k|
25
+ assert_equal 1, Math.binomial(k, 0)
26
+ assert_equal 1, Math.binomial(k, k)
27
+ assert_equal k, Math.binomial(k, 1)
28
+ assert_equal k, Math.binomial(k, k - 1)
29
+ end
30
+ (15..20).each do |n|
31
+ (5..15).each do |k|
32
+ assert_equal Math.binomial(n, k), Math.binomial(n, n - k)
33
+ end
34
+ end
35
+ assert_equal 100947, Math.binomial(23, 17)
36
+ end
37
+
38
38
  end
39
39
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bernoulli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.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-06-02 00:00:00.000000000 Z
12
+ date: 2012-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -86,13 +86,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ! '>='
88
88
  - !ruby/object:Gem::Version
89
- version: 1.9.2
89
+ version: 1.8.7
90
90
  required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  none: false
92
92
  requirements:
93
93
  - - ! '>='
94
94
  - !ruby/object:Gem::Version
95
- version: '0'
95
+ version: 1.3.6
96
96
  requirements: []
97
97
  rubyforge_project:
98
98
  rubygems_version: 1.8.24