binomial_distribution 0.1.1
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +3 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +13 -0
- data/README.md +37 -0
- data/Rakefile +16 -0
- data/binomial_distribution.gemspec +37 -0
- data/certs/nephos.pem +22 -0
- data/lib/binomial_distribution.rb +83 -0
- data/test/binomial_distribution.rb +20 -0
- data/test/unit_test.rb +7 -0
- data/version +1 -0
- metadata +93 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6622daa3e81337424a264d794ab0cf6f7c86c375
|
4
|
+
data.tar.gz: 725288b83c2568b74a45ad7391aa813e3c3e222c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d472d9760571bd9bb1cc2ec21b7d23d485406e67efa3afcdb57e203c3974cc32b7c813027e137bf724c6b9d769fa7896fd6bbd46cb6d9acdb84f045591208ce5
|
7
|
+
data.tar.gz: e853d80ff48ffea3dee69b5f8b46ecc814f4ad626b3090144a2c2ab6e5ab8c53ac012bdb55c5d02f57460b44376ab89460735e9001d8d2fb0fc78bd167cca9cc
|
checksums.yaml.gz.sig
ADDED
data.tar.gz.sig
ADDED
Binary file
|
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# A ruby implementation of the Binomial Distribution
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
```sh
|
6
|
+
gem install binomial_distribution
|
7
|
+
```
|
8
|
+
|
9
|
+
or in the ``Gemfile``
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'binomial_distribution'
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
## Usage in Ruby
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'binomial_distribution'
|
20
|
+
|
21
|
+
Math.binomial_distribution(tries: 1, probability: 0.4, success: 1) # => 0.4
|
22
|
+
```
|
23
|
+
|
24
|
+
|
25
|
+
## Unitary tests
|
26
|
+
|
27
|
+
```sh
|
28
|
+
rake test
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
## Contributes !
|
33
|
+
|
34
|
+
Find a bug ? Want a new feature ?
|
35
|
+
Create a clear pull request and we'll see :)
|
36
|
+
|
37
|
+
- Nephos (poulet_a)
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'binomial_distribution'
|
3
|
+
s.version = File.read("version")
|
4
|
+
s.date = Time.now.getgm.to_s.split.first
|
5
|
+
s.summary = File.read("CHANGELOG").match(/^v[^\n]+\n((\t[^\n]+\n)+)/m)[1].split("\t").join
|
6
|
+
s.description = 'A ruby implementation of the Binomial Distribution'
|
7
|
+
s.authors = ['Nephos (poulet_a)']
|
8
|
+
s.email = ['arthur.poulet@mailoo.org']
|
9
|
+
s.files = %w(
|
10
|
+
lib/binomial_distribution.rb
|
11
|
+
|
12
|
+
README.md
|
13
|
+
CHANGELOG
|
14
|
+
|
15
|
+
Rakefile
|
16
|
+
Gemfile
|
17
|
+
Gemfile.lock
|
18
|
+
binomial_distribution.gemspec
|
19
|
+
version
|
20
|
+
|
21
|
+
test/unit_test.rb
|
22
|
+
test/binomial_distribution.rb
|
23
|
+
|
24
|
+
certs/nephos.pem
|
25
|
+
)
|
26
|
+
s.executables = %w(
|
27
|
+
)
|
28
|
+
s.homepage = 'https://github.com/Nephos/BinomialDistribution'
|
29
|
+
s.license = 'WTFPL'
|
30
|
+
|
31
|
+
s.cert_chain = ['certs/nephos.pem']
|
32
|
+
s.signing_key = File.expand_path('~/.ssh/gem-private_key.pem') if $0 =~ /gem\z/
|
33
|
+
|
34
|
+
# s.add_d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UdEgQfMB2BG2FydGh1
|
15
|
+
ci5wb3VsZXRAY3J5cHRvbGFiLm5ldDANBgkqhkiG9w0BAQUFAAOCAQEAo60BuRZM
|
16
|
+
5Vtn0H0kHHTPiXz09lwxx4cFpTqrTTM0T6qM3gKsR5eLyR1Rt5hGmYLPgY4ZW4zI
|
17
|
+
XcuDbgRJrczZs7AZNy5NXyVWpxCwkZzklpbGTuYQdFz7mKfVyALjLjrilOIktXcz
|
18
|
+
PvB7EoLlqYvq9cnwV3WE1MkRcBd07xpofcp0kxae2dCOZPUq24TcNaRe7Utw6jjA
|
19
|
+
iL0bcH7zZkzMsNoNpPAgsd3ovU4Mf9M+A3M8COmd7KzKyZ0k7P12u5InPWUsNQS2
|
20
|
+
tcYkgfqUJPitIJx1RvWZpIyH5uJhRUYK3+vU9nMOxez5WbIlC1TtpByKAPMX+sht
|
21
|
+
gib3AoIT8jh/2w==
|
22
|
+
-----END CERTIFICATE-----
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Math
|
2
|
+
|
3
|
+
def self.factorial(n)
|
4
|
+
raise ArgumentError, 'The argument muse be a natural Fixnum N' if not n.is_a? Fixnum
|
5
|
+
raise Math::DomainError, 'The argument must be a natural (out of domain -- factorial)' if n < 0
|
6
|
+
return 1 if n.zero?
|
7
|
+
return (1..n).inject(:*)
|
8
|
+
end unless self.method_defined? :factorial
|
9
|
+
|
10
|
+
def self.coef_binomial(n, k)
|
11
|
+
return 0 if n < 0 or k < 0 or n < k
|
12
|
+
return factorial(n) / (factorial(k) * factorial(n - k))
|
13
|
+
end
|
14
|
+
|
15
|
+
# @note if no named parameters are used, then it will try to use the unamed parameters (tries, success, probability)
|
16
|
+
def self.binomial_distribution(*unamed, tries: nil, success: nil, probability: nil)
|
17
|
+
unamed.reverse! # pop works on a stack
|
18
|
+
Binomial.new(tries: tries || unamed.pop, probability: probability || unamed.pop).distribute(success || unamed.pop)
|
19
|
+
end
|
20
|
+
|
21
|
+
class SuperiorityError < Exception; end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Array
|
25
|
+
def second
|
26
|
+
self[1]
|
27
|
+
end unless self.method_defined? :second
|
28
|
+
end
|
29
|
+
|
30
|
+
class Binomial
|
31
|
+
|
32
|
+
# @param n [Fixnum] number of tries
|
33
|
+
# @param p [Float] probability of success
|
34
|
+
# @param tries [Fixnum] alias for n
|
35
|
+
# @param probability [Float] alias for p
|
36
|
+
# @param failure [Float] alias for `p = 1 - failure`
|
37
|
+
# @note if no probability is defined, the default value will be 0.5
|
38
|
+
def initialize n: nil, p: nil, tries: nil, probability: nil, failure: nil
|
39
|
+
tries_count = n || tries
|
40
|
+
probability = p || probability || (failure && 1.0 - failure) || 0.5
|
41
|
+
raise ArgumentError, "The argument `n` `#{tries_count}` is not an Integer" unless tries_count.is_a? Integer
|
42
|
+
raise ArgumentError, "The argument `p` `#{probability}` is not a Numeric" unless probability.is_a? Numeric
|
43
|
+
tries_count = Float(tries_count)
|
44
|
+
raise Math::DomainError, "The argument `n` `#{tries_count}` is not in greater or equal to 0" if tries_count < 0.0
|
45
|
+
raise Math::DomainError, "The argument `p` `#{probability}` is not in greater or equal to 0" if probability < 0.0
|
46
|
+
raise Math::DomainError, "The argument `p` `#{probability}` is not in lesser or equal to 1" if probability > 1.0
|
47
|
+
@n = tries_count.to_i
|
48
|
+
@p = probability.to_f
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
"#<#{self.class} @n=#@n, @p=#@p>"
|
53
|
+
end
|
54
|
+
|
55
|
+
# @param k [Fixnum] number of test successful.
|
56
|
+
# @param k [Enumerable] list of number of test successful.
|
57
|
+
# @return [Float] probability
|
58
|
+
def distribute k
|
59
|
+
if k.is_a? Enumerable
|
60
|
+
distribute_enumerable k
|
61
|
+
elsif k.is_a? Integer
|
62
|
+
distribute_integer k
|
63
|
+
else
|
64
|
+
raise ArgumentError
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private def distribute_enumerable k
|
69
|
+
k.map{|p| calc(p) }.inject(&:+)
|
70
|
+
end
|
71
|
+
|
72
|
+
private def distribute_integer k
|
73
|
+
raise Math::SuperiorityError, "the number of success must be lesser or equal to the number of tries (#{@n})" if k > @n
|
74
|
+
Math.coef_binomial(@n, k) * @p**k * (1 - @p) ** (@n - k)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
# if __FILE__ == $0
|
81
|
+
# require 'pry'
|
82
|
+
# binding.pry
|
83
|
+
# end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class TestBinomialDistribution < Test::Unit::TestCase
|
2
|
+
|
3
|
+
def test_simple
|
4
|
+
0.0.step(by: 0.1, to: 1.0) do |proba|
|
5
|
+
assert_equal proba, Math.binomial_distribution(tries: 1, probability: proba, success: 1)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_nonamed_arguments
|
10
|
+
assert_equal 0.4, Math.binomial_distribution(tries: 1, probability: 0.4, success: 1)
|
11
|
+
assert_equal 0.4, Math.binomial_distribution(1, probability: 0.4, success: 1)
|
12
|
+
assert_equal 0.4, Math.binomial_distribution(1, tries: 1, probability: 0.4)
|
13
|
+
assert_equal 0.4, Math.binomial_distribution(0.4, tries: 1, success: 1)
|
14
|
+
assert_equal 0.4, Math.binomial_distribution(1, 0.4, success: 1)
|
15
|
+
assert_equal 0.4, Math.binomial_distribution(0.4, 1, tries: 1)
|
16
|
+
assert_equal 0.4, Math.binomial_distribution(1, 0.4, 1)
|
17
|
+
assert_equal 0.4, Math.binomial_distribution(1, 1, probability: 0.4)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/test/unit_test.rb
ADDED
data/version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binomial_distribution
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|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|
26
|
+
ci5wb3VsZXRAY3J5cHRvbGFiLm5ldDANBgkqhkiG9w0BAQUFAAOCAQEAo60BuRZM
|
27
|
+
5Vtn0H0kHHTPiXz09lwxx4cFpTqrTTM0T6qM3gKsR5eLyR1Rt5hGmYLPgY4ZW4zI
|
28
|
+
XcuDbgRJrczZs7AZNy5NXyVWpxCwkZzklpbGTuYQdFz7mKfVyALjLjrilOIktXcz
|
29
|
+
PvB7EoLlqYvq9cnwV3WE1MkRcBd07xpofcp0kxae2dCOZPUq24TcNaRe7Utw6jjA
|
30
|
+
iL0bcH7zZkzMsNoNpPAgsd3ovU4Mf9M+A3M8COmd7KzKyZ0k7P12u5InPWUsNQS2
|
31
|
+
tcYkgfqUJPitIJx1RvWZpIyH5uJhRUYK3+vU9nMOxez5WbIlC1TtpByKAPMX+sht
|
32
|
+
gib3AoIT8jh/2w==
|
33
|
+
-----END CERTIFICATE-----
|
34
|
+
date: 2016-05-05 00:00:00.000000000 Z
|
35
|
+
dependencies:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: nomorebeer
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '1.1'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.1'
|
50
|
+
description: A ruby implementation of the Binomial Distribution
|
51
|
+
email:
|
52
|
+
- arthur.poulet@mailoo.org
|
53
|
+
executables: []
|
54
|
+
extensions: []
|
55
|
+
extra_rdoc_files: []
|
56
|
+
files:
|
57
|
+
- CHANGELOG
|
58
|
+
- Gemfile
|
59
|
+
- Gemfile.lock
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- binomial_distribution.gemspec
|
63
|
+
- certs/nephos.pem
|
64
|
+
- lib/binomial_distribution.rb
|
65
|
+
- test/binomial_distribution.rb
|
66
|
+
- test/unit_test.rb
|
67
|
+
- version
|
68
|
+
homepage: https://github.com/Nephos/BinomialDistribution
|
69
|
+
licenses:
|
70
|
+
- WTFPL
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.5.1
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: "* Initialize the project"
|
92
|
+
test_files: []
|
93
|
+
has_rdoc:
|
metadata.gz.sig
ADDED
Binary file
|