bernoulli 0.1.0 → 0.2.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
@@ -1,4 +1,3 @@
1
-
2
1
  # Bernoulli : A Ruby library for the binomial distribution
3
2
 
4
3
  In probability theory and statistics, the **binomial distribution** is the discrete probability distribution of the number of successes in a sequence of *n* independent yes/no experiments, each of which yields success with probability *p*.
@@ -11,23 +10,23 @@ Use RubyGems to install `bernoulli`
11
10
 
12
11
  $ gem install bernoulli
13
12
 
14
- ## Uses
13
+ ## Tutorial
15
14
 
16
15
  Using `bernoulli`, one can calculate the probabilities of bernoulli experiments.
17
16
 
18
- ### Example Usage
17
+ Let's start by looking at an example.
19
18
 
20
19
  **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
20
 
22
21
  Let's use `bernoulli` to solve this:
23
22
 
24
- First we load up `irb` from the terminal including `bernoulli` as a library
23
+ First we load up `irb` from the terminal including `bernoulli` as a library.
25
24
 
26
25
  $ irb -r bernoulli
27
26
 
28
- Or we could just include `require 'bernoulli'` in any Ruby script
27
+ Or we could just `require 'bernoulli'` in any Ruby script.
29
28
 
30
- Then we create a new instance of class Bernoulli and calculate our desired probability and properties
29
+ Then we create a new instance of class Bernoulli and calculate our desired probability and properties like so
31
30
 
32
31
  x = Bernoulli.new(180, 1.0/6)
33
32
  # => #<Bernoulli:0x007fba721a1bf0 @n=180, @p=0.16666666666666666>
@@ -38,3 +37,44 @@ Then we create a new instance of class Bernoulli and calculate our desired proba
38
37
  x.expected_value # => 30.0
39
38
  x.variance # => 25.0
40
39
 
40
+ We could also calculate the standard deviation, excess or skewness:
41
+
42
+ x.standard_deviation # => 5.0
43
+ x.excess # => 0.006666666666666665
44
+ x.skewness => 0.13333333333333336
45
+
46
+ `bernoulli` can also do empirical tests. Let's look at a smaller example We can simulate the tossing of 4 fair coins
47
+
48
+ y = y = Bernoulli.new(4, 0.5)
49
+ # => #<Bernoulli:0x007ff894293198 @n=4, @p=0.5>
50
+
51
+ The method `sample` gives as a random array of length `n`, where each entry is `1` with a probability of `p`. The methods `sample_value` gives us the number of wins in a random expriment, we could than compare it to `expected_value`.
52
+
53
+ y.sample # => [1, 0, 1, 0]
54
+ y.sample # => [0, 0, 1, 0]
55
+ y.sample # => [1, 1, 1, 1]
56
+
57
+ y.sample_value # => 2
58
+ y.sample_value # => 1
59
+ y.sample_value # => 0
60
+ y.sample_value # => 2
61
+
62
+ y.expected_value # => 2.0
63
+
64
+ Last, but not least, we can create a full table of all possible values and their probabilitys like so
65
+
66
+ puts y.table
67
+
68
+ will produce
69
+
70
+ 0,0.0625
71
+ 1,0.25
72
+ 2,0.375
73
+ 3,0.25
74
+ 4,0.0625
75
+
76
+ ## Contributing
77
+
78
+ `bernoulli` is a really small project. After writing the same code for some project and then losing it two or three times I decided to do it one time and well, so I can just call in the code from here next time.
79
+
80
+ Feel free to cantact me about anything I could/should add or to contribute in any way to this simple library.
data/Rakefile CHANGED
@@ -1,10 +1,13 @@
1
-
2
1
  require 'rake/testtask'
3
2
 
3
+ desc 'Run tests'
4
4
  Rake::TestTask.new do |test|
5
- test.libs << 'test'
6
5
  end
7
6
 
8
- desc "Run tests"
9
7
  task :default => :test
10
8
 
9
+ desc 'Build gem'
10
+ task :gem do
11
+ sh 'gem build bernoulli.gemspec'
12
+ end
13
+
data/bernoulli.gemspec CHANGED
@@ -1,13 +1,12 @@
1
-
2
1
  Gem::Specification.new do |spec|
3
2
 
4
3
  spec.name = 'bernoulli'
5
- spec.version = '0.1.0'
4
+ spec.version = '0.2.0'
6
5
  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'
6
+ spec.description = 'A Library that allows calculation of probibilities and properties of binomial experiments like coin tossing'
7
+ spec.homepage = 'http://mkonutgan.github.com/bernoulli'
9
8
 
10
- spec.date = '2012-05-25'
9
+ spec.date = '2012-05-28'
11
10
 
12
11
  spec.author = 'Mikael Konutgan'
13
12
  spec.email = 'mkonutgan@shortmail.com'
@@ -20,10 +19,12 @@ Gem::Specification.new do |spec|
20
19
  'bernoulli.gemspec',
21
20
  'lib/bernoulli.rb',
22
21
  'lib/bernoulli/math.rb',
23
- 'test/test_bernoulli.rb'
22
+ 'test/test_bernoulli.rb',
23
+ 'test/test_math.rb'
24
24
  ]
25
25
  spec.test_files = [
26
- 'test/test_bernoulli.rb'
26
+ 'test/test_bernoulli.rb',
27
+ 'test/test_math.rb'
27
28
  ]
28
29
 
29
30
  end
@@ -1,4 +1,3 @@
1
-
2
1
  module Math
3
2
 
4
3
  def self.factorial(n)
data/lib/bernoulli.rb CHANGED
@@ -1,5 +1,5 @@
1
-
2
1
  require 'bernoulli/math'
2
+ require 'csv'
3
3
 
4
4
  class Bernoulli
5
5
 
@@ -18,12 +18,12 @@ class Bernoulli
18
18
  def [](k)
19
19
  if k.is_a? Integer
20
20
  raise Math::DomainError if not (k <= @n and k >= 0)
21
- self.probability(k).to_f
21
+ probability(k).to_f
22
22
  elsif k.is_a? Range
23
23
  raise Math::DomainError if not (k.begin >= 0 and k.end <= @n)
24
24
  sum = 0
25
25
  k.each do |i|
26
- sum += self.probability(i)
26
+ sum += probability(i)
27
27
  end
28
28
  sum.to_f
29
29
  else
@@ -31,6 +31,19 @@ class Bernoulli
31
31
  end
32
32
  end
33
33
 
34
+ def sample
35
+ s = []
36
+ @n.times do
37
+ s << (rand < @p ? 1 : 0)
38
+ end
39
+ s
40
+ end
41
+
42
+ def sample_value
43
+ sample.inject(:+)
44
+ end
45
+ alias :sv :sample_value
46
+
34
47
  def expected_value
35
48
  @n * @p
36
49
  end
@@ -42,7 +55,7 @@ class Bernoulli
42
55
  alias :v :variance
43
56
 
44
57
  def standard_deviation
45
- Math.sqrt(self.variance)
58
+ Math.sqrt(variance)
46
59
  end
47
60
  alias :sd :standard_deviation
48
61
 
@@ -53,6 +66,14 @@ class Bernoulli
53
66
  def excess
54
67
  (1 - 6 * @p * (1 - @p)) / (@n * @p * (1 - @p))
55
68
  end
69
+
70
+ def table
71
+ CSV.generate do |csv|
72
+ (0..@n).each do |n|
73
+ csv << [n, probability(n)]
74
+ end
75
+ end
76
+ end
56
77
 
57
78
  end
58
79
 
@@ -1,26 +1,8 @@
1
-
2
- require 'test/unit'
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
3
  require 'bernoulli'
4
4
 
5
- class BernoulliTest < Test::Unit::TestCase
6
-
7
- def test_factorial
8
- assert_equal 1, Math.factorial(0)
9
- assert_equal 1, Math.factorial(1)
10
- assert_equal 120, Math.factorial(5)
11
- assert_equal 263130836933693530167218012160000000, Math.factorial(32)
12
- end
13
-
14
- def test_binomial
15
- assert_equal 1, Math.binomial(0, 0)
16
- (1..10).each do |k|
17
- assert_equal 1, Math.binomial(k, 0)
18
- assert_equal 1, Math.binomial(k, k)
19
- assert_equal k, Math.binomial(k, 1)
20
- assert_equal k, Math.binomial(k, k - 1)
21
- end
22
- assert_equal 100947, Math.binomial(23, 17)
23
- end
5
+ class BernoulliTest < MiniTest::Unit::TestCase
24
6
 
25
7
  def test_new_experiment
26
8
  assert_raises RuntimeError do
data/test/test_math.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'bernoulli'
4
+
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
+
38
+ end
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.1.0
4
+ version: 0.2.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-25 00:00:00.000000000 Z
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Library that allows calculation of probibilities and properties of
15
- binomial experiments
15
+ binomial experiments like coin tossing
16
16
  email: mkonutgan@shortmail.com
17
17
  executables: []
18
18
  extensions: []
@@ -24,7 +24,8 @@ files:
24
24
  - lib/bernoulli.rb
25
25
  - lib/bernoulli/math.rb
26
26
  - test/test_bernoulli.rb
27
- homepage: https://github.com/mkonutgan/bernoulli
27
+ - test/test_math.rb
28
+ homepage: http://mkonutgan.github.com/bernoulli
28
29
  licenses: []
29
30
  post_install_message:
30
31
  rdoc_options: []
@@ -50,3 +51,4 @@ specification_version: 3
50
51
  summary: Binomial experiments library
51
52
  test_files:
52
53
  - test/test_bernoulli.rb
54
+ - test/test_math.rb