bernoulli 0.0.1 → 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/README.md CHANGED
@@ -17,7 +17,7 @@ Using `bernoulli`, one can calculate the probabilities of bernoulli experiments.
17
17
 
18
18
  ### Example Usage
19
19
 
20
- **Question:** What is the chance of getting between 25 and 36 6's when one rolls a die 180 times? What is the expected value and variance of this trial?
20
+ **Question:** What is the chance of getting between 25 and 36 sixes when one rolls a die 180 times? What is the expected value and variance of this trial?
21
21
 
22
22
  Let's use `bernoulli` to solve this:
23
23
 
@@ -29,12 +29,12 @@ Or we could just include `require 'bernoulli'` in any Ruby script
29
29
 
30
30
  Then we create a new instance of class Bernoulli and calculate our desired probability and properties
31
31
 
32
- b = Bernoulli.new(180, 1.0/6)
32
+ x = Bernoulli.new(180, 1.0/6)
33
33
  # => #<Bernoulli:0x007fba721a1bf0 @n=180, @p=0.16666666666666666>
34
34
 
35
35
  # The probability of getting between 25 and 36 sixes is
36
- b[25..36] # => 0.7665588897840108
36
+ x[25..36] # => 0.7665588897840108
37
37
 
38
- b.expected_value # => 30.0
39
- b.variance # => 25.0
38
+ x.expected_value # => 30.0
39
+ x.variance # => 25.0
40
40
 
data/bernoulli.gemspec CHANGED
@@ -1,6 +1,19 @@
1
1
 
2
2
  Gem::Specification.new do |spec|
3
3
 
4
+ spec.name = 'bernoulli'
5
+ spec.version = '0.1.0'
6
+ spec.summary = 'Binomial experiments library'
7
+ spec.description = 'A Library that allows calculation of probibilities and properties of binomial experiments'
8
+ spec.homepage = 'https://github.com/mkonutgan/bernoulli'
9
+
10
+ spec.date = '2012-05-25'
11
+
12
+ spec.author = 'Mikael Konutgan'
13
+ spec.email = 'mkonutgan@shortmail.com'
14
+
15
+ spec.required_ruby_version = '>= 1.9.2'
16
+
4
17
  spec.files = [
5
18
  'README.md',
6
19
  'Rakefile',
@@ -9,16 +22,9 @@ Gem::Specification.new do |spec|
9
22
  'lib/bernoulli/math.rb',
10
23
  'test/test_bernoulli.rb'
11
24
  ]
12
- spec.name = 'bernoulli'
13
- spec.summary = 'Bernoulli process library'
14
- spec.version = '0.0.1'
15
-
16
- spec.date = '2012-05-24'
17
- spec.description = "A Library that allows calculation of probibilities and properties of Bernoulli processes like coin tossing"
18
-
19
- spec.author = 'Mikael Konutgan'
20
- spec.homepage = 'https://github.com/mkonutgan'
21
- spec.email = 'mkonutgan@shortmail.com'
25
+ spec.test_files = [
26
+ 'test/test_bernoulli.rb'
27
+ ]
22
28
 
23
29
  end
24
30
 
@@ -2,6 +2,10 @@
2
2
  module Math
3
3
 
4
4
  def self.factorial(n)
5
+
6
+ if n < 0 or not n.is_a? Integer
7
+ raise Math::DomainError, 'Numerical argument is out of domain - "factorial"'
8
+ end
5
9
  if n == 0
6
10
  1
7
11
  else
@@ -10,6 +14,9 @@ module Math
10
14
  end
11
15
 
12
16
  def self.binomial(n, k)
17
+ if n < 0 or n < k or not (n.is_a? Integer and k.is_a? Integer)
18
+ raise Math::DomainError, 'Numerical argument is out of domain - "binomial"'
19
+ end
13
20
  factorial(n)/(factorial(k) * factorial(n - k))
14
21
  end
15
22
 
data/lib/bernoulli.rb CHANGED
@@ -4,6 +4,9 @@ require 'bernoulli/math'
4
4
  class Bernoulli
5
5
 
6
6
  def initialize(n, p)
7
+ if n < 0 or p > 1.0 or p < 0.0 or not n.is_a? Integer
8
+ raise 'Could not initialize Bernoulli experiment'
9
+ end
7
10
  @n = n
8
11
  @p = p
9
12
  end
@@ -14,23 +17,42 @@ class Bernoulli
14
17
 
15
18
  def [](k)
16
19
  if k.is_a? Integer
20
+ raise Math::DomainError if not (k <= @n and k >= 0)
17
21
  self.probability(k).to_f
18
22
  elsif k.is_a? Range
23
+ raise Math::DomainError if not (k.begin >= 0 and k.end <= @n)
19
24
  sum = 0
20
25
  k.each do |i|
21
26
  sum += self.probability(i)
22
27
  end
23
28
  sum.to_f
29
+ else
30
+ raise TypeError
24
31
  end
25
32
  end
26
33
 
27
34
  def expected_value
28
35
  @n * @p
29
36
  end
37
+ alias :ev :expected_value
30
38
 
31
39
  def variance
32
40
  @n * @p * (1 - @p)
33
41
  end
42
+ alias :v :variance
43
+
44
+ def standard_deviation
45
+ Math.sqrt(self.variance)
46
+ end
47
+ alias :sd :standard_deviation
48
+
49
+ def skewness
50
+ (1 - 2 * @p) / (Math.sqrt(@n * @p * (1 - @p)))
51
+ end
52
+
53
+ def excess
54
+ (1 - 6 * @p * (1 - @p)) / (@n * @p * (1 - @p))
55
+ end
34
56
 
35
57
  end
36
58
 
@@ -21,22 +21,48 @@ class BernoulliTest < Test::Unit::TestCase
21
21
  end
22
22
  assert_equal 100947, Math.binomial(23, 17)
23
23
  end
24
+
25
+ def test_new_experiment
26
+ assert_raises RuntimeError do
27
+ Bernoulli.new(10, 1.1)
28
+ end
29
+ assert_raises RuntimeError do
30
+ Bernoulli.new(-10, 0.7)
31
+ end
32
+ assert_raises RuntimeError do
33
+ Bernoulli.new(4.5, 0.2)
34
+ end
35
+ end
36
+
37
+ def setup
38
+ @x = Bernoulli.new(180, 1.0/6)
39
+ @y = Bernoulli.new(2, 0.5)
40
+ end
24
41
 
25
42
  def test_coin_toss
26
- p = Bernoulli.new(2, 0.5)
27
- assert_equal 0.5, p[1]
28
- assert_equal 0.25, p[2]
29
- assert_equal 1.0, p[0..2]
43
+ assert_equal 0.5, @y[1]
44
+ assert_equal 0.25, @y[2]
45
+ assert_equal 1.0, @y[0..2]
30
46
  end
31
47
 
32
48
  def test_expected_value
33
- p = Bernoulli.new(180, 1.0/6)
34
- assert_equal 30, p.expected_value
49
+ assert_equal 30, @x.expected_value
35
50
  end
36
51
 
37
52
  def test_variance
38
- p = Bernoulli.new(180, 1.0/6)
39
- assert_equal 25, p.variance
53
+ assert_equal 25, @x.variance
54
+ end
55
+
56
+ def test_standard_deviation
57
+ assert_equal 5, @x.standard_deviation
58
+ end
59
+
60
+ def test_skewness
61
+ assert_in_delta 0.1333333333333333, @x.skewness, 1.0e-7
62
+ end
63
+
64
+ def test_excess
65
+ assert_in_delta 0.0066666666666667, @x.excess, 1.0e-7
40
66
  end
41
67
 
42
68
  end
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.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-24 00:00:00.000000000 Z
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Library that allows calculation of probibilities and properties of
15
- Bernoulli processes like coin tossing
15
+ binomial experiments
16
16
  email: mkonutgan@shortmail.com
17
17
  executables: []
18
18
  extensions: []
@@ -24,7 +24,7 @@ files:
24
24
  - lib/bernoulli.rb
25
25
  - lib/bernoulli/math.rb
26
26
  - test/test_bernoulli.rb
27
- homepage: https://github.com/mkonutgan
27
+ homepage: https://github.com/mkonutgan/bernoulli
28
28
  licenses: []
29
29
  post_install_message:
30
30
  rdoc_options: []
@@ -35,7 +35,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
36
  - - ! '>='
37
37
  - !ruby/object:Gem::Version
38
- version: '0'
38
+ version: 1.9.2
39
39
  required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
@@ -47,5 +47,6 @@ rubyforge_project:
47
47
  rubygems_version: 1.8.24
48
48
  signing_key:
49
49
  specification_version: 3
50
- summary: Bernoulli process library
51
- test_files: []
50
+ summary: Binomial experiments library
51
+ test_files:
52
+ - test/test_bernoulli.rb