bernoulli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+
2
+ # Bernoulli : A Ruby library for the binomial distribution
3
+
4
+ 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*.
5
+
6
+ You can read more about **Bernoulli trials** and the **binomial distribution** [on Wikipedia](http://en.wikipedia.org/wiki/Binomial_distribution).
7
+
8
+ ## Installation
9
+
10
+ Use RubyGems to install `bernoulli`
11
+
12
+ $ gem install bernoulli
13
+
14
+ ## Uses
15
+
16
+ Using `bernoulli`, one can calculate the probabilities of bernoulli experiments.
17
+
18
+ ### Example Usage
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?
21
+
22
+ Let's use `bernoulli` to solve this:
23
+
24
+ First we load up `irb` from the terminal including `bernoulli` as a library
25
+
26
+ $ irb -r bernoulli
27
+
28
+ Or we could just include `require 'bernoulli'` in any Ruby script
29
+
30
+ Then we create a new instance of class Bernoulli and calculate our desired probability and properties
31
+
32
+ b = Bernoulli.new(180, 1.0/6)
33
+ # => #<Bernoulli:0x007fba721a1bf0 @n=180, @p=0.16666666666666666>
34
+
35
+ # The probability of getting between 25 and 36 sixes is
36
+ b[25..36] # => 0.7665588897840108
37
+
38
+ b.expected_value # => 30.0
39
+ b.variance # => 25.0
40
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |test|
5
+ test.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
10
+
data/bernoulli.gemspec ADDED
@@ -0,0 +1,24 @@
1
+
2
+ Gem::Specification.new do |spec|
3
+
4
+ spec.files = [
5
+ 'README.md',
6
+ 'Rakefile',
7
+ 'bernoulli.gemspec',
8
+ 'lib/bernoulli.rb',
9
+ 'lib/bernoulli/math.rb',
10
+ 'test/test_bernoulli.rb'
11
+ ]
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'
22
+
23
+ end
24
+
@@ -0,0 +1,17 @@
1
+
2
+ module Math
3
+
4
+ def self.factorial(n)
5
+ if n == 0
6
+ 1
7
+ else
8
+ (1..n).inject(:*)
9
+ end
10
+ end
11
+
12
+ def self.binomial(n, k)
13
+ factorial(n)/(factorial(k) * factorial(n - k))
14
+ end
15
+
16
+ end
17
+
data/lib/bernoulli.rb ADDED
@@ -0,0 +1,36 @@
1
+
2
+ require 'bernoulli/math'
3
+
4
+ class Bernoulli
5
+
6
+ def initialize(n, p)
7
+ @n = n
8
+ @p = p
9
+ end
10
+
11
+ def probability(k)
12
+ Math.binomial(@n, k) * @p**k * (1 - @p)**(@n - k)
13
+ end
14
+
15
+ def [](k)
16
+ if k.is_a? Integer
17
+ self.probability(k).to_f
18
+ elsif k.is_a? Range
19
+ sum = 0
20
+ k.each do |i|
21
+ sum += self.probability(i)
22
+ end
23
+ sum.to_f
24
+ end
25
+ end
26
+
27
+ def expected_value
28
+ @n * @p
29
+ end
30
+
31
+ def variance
32
+ @n * @p * (1 - @p)
33
+ end
34
+
35
+ end
36
+
@@ -0,0 +1,43 @@
1
+
2
+ require 'test/unit'
3
+ require 'bernoulli'
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
24
+
25
+ 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]
30
+ end
31
+
32
+ def test_expected_value
33
+ p = Bernoulli.new(180, 1.0/6)
34
+ assert_equal 30, p.expected_value
35
+ end
36
+
37
+ def test_variance
38
+ p = Bernoulli.new(180, 1.0/6)
39
+ assert_equal 25, p.variance
40
+ end
41
+
42
+ end
43
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bernoulli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mikael Konutgan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Library that allows calculation of probibilities and properties of
15
+ Bernoulli processes like coin tossing
16
+ email: mkonutgan@shortmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - Rakefile
23
+ - bernoulli.gemspec
24
+ - lib/bernoulli.rb
25
+ - lib/bernoulli/math.rb
26
+ - test/test_bernoulli.rb
27
+ homepage: https://github.com/mkonutgan
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Bernoulli process library
51
+ test_files: []