bernoulli 0.2.1 → 0.3.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 +5 -5
- data/lib/bernoulli/version.rb +1 -1
- data/lib/bernoulli.rb +73 -66
- data/test/test_bernoulli.rb +16 -6
- metadata +2 -2
data/README.md
CHANGED
@@ -28,8 +28,8 @@ Or we could just `require 'bernoulli'` in any Ruby script.
|
|
28
28
|
|
29
29
|
Then we create a new instance of class Bernoulli and calculate our desired probability and properties like so
|
30
30
|
|
31
|
-
x = Bernoulli.new(180, 1.0/6)
|
32
|
-
# => #<Bernoulli
|
31
|
+
x = Bernoulli::Distribution.new(180, 1.0/6)
|
32
|
+
# => #<Bernoulli::Distribution @n=180, @p=0.16666666666666666>
|
33
33
|
|
34
34
|
# The probability of getting between 25 and 36 sixes is
|
35
35
|
x[25..36] # => 0.7665588897840108
|
@@ -45,8 +45,8 @@ We could also calculate the standard deviation, excess or skewness:
|
|
45
45
|
|
46
46
|
`bernoulli` can also do empirical tests. Let's look at a smaller example We can simulate the tossing of 4 fair coins
|
47
47
|
|
48
|
-
y = Bernoulli.new(4, 0.5)
|
49
|
-
# => #<Bernoulli
|
48
|
+
y = Bernoulli::Distribution.new(4, 0.5)
|
49
|
+
# => #<Bernoulli::Distribution @n=4, @p=0.5>
|
50
50
|
|
51
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
52
|
|
@@ -81,7 +81,7 @@ Feel free to cantact me about anything I could/should add or to contribute in a
|
|
81
81
|
|
82
82
|
1. Fork it
|
83
83
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
84
|
-
3. Commit your changes (`git commit -am '
|
84
|
+
3. Commit your changes (`git commit -am 'added some feature'`)
|
85
85
|
4. Push to the branch (`git push origin my-new-feature`)
|
86
86
|
5. Create new Pull Request
|
87
87
|
|
data/lib/bernoulli/version.rb
CHANGED
data/lib/bernoulli.rb
CHANGED
@@ -1,79 +1,86 @@
|
|
1
1
|
require 'bernoulli/math'
|
2
|
+
require 'bernoulli/version'
|
2
3
|
require 'csv'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@n = n
|
11
|
-
@p = p
|
12
|
-
end
|
13
|
-
|
14
|
-
def probability(k)
|
15
|
-
Math.binomial(@n, k) * @p**k * (1 - @p)**(@n - k)
|
16
|
-
end
|
17
|
-
|
18
|
-
def [](k)
|
19
|
-
if k.is_a? Integer
|
20
|
-
raise Math::DomainError if not (k <= @n and k >= 0)
|
21
|
-
probability(k).to_f
|
22
|
-
elsif k.is_a? Range
|
23
|
-
raise Math::DomainError if not (k.begin >= 0 and k.end <= @n)
|
24
|
-
sum = 0
|
25
|
-
k.each do |i|
|
26
|
-
sum += probability(i)
|
5
|
+
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'
|
27
11
|
end
|
28
|
-
|
29
|
-
|
30
|
-
raise TypeError
|
12
|
+
@n = n
|
13
|
+
@p = p
|
31
14
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
s = []
|
36
|
-
@n.times do
|
37
|
-
s << (rand < @p ? 1 : 0)
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"#<Bernoulli::Distribution @n=#{@n}, @p=#{@p}>"
|
38
18
|
end
|
39
|
-
s
|
40
|
-
end
|
41
|
-
|
42
|
-
def sample_value
|
43
|
-
sample.inject(:+)
|
44
|
-
end
|
45
|
-
alias :sv :sample_value
|
46
|
-
|
47
|
-
def expected_value
|
48
|
-
@n * @p
|
49
|
-
end
|
50
|
-
alias :ev :expected_value
|
51
|
-
|
52
|
-
def variance
|
53
|
-
@n * @p * (1 - @p)
|
54
|
-
end
|
55
|
-
alias :v :variance
|
56
|
-
|
57
|
-
def standard_deviation
|
58
|
-
Math.sqrt(variance)
|
59
|
-
end
|
60
|
-
alias :sd :standard_deviation
|
61
|
-
|
62
|
-
def skewness
|
63
|
-
(1 - 2 * @p) / (Math.sqrt(@n * @p * (1 - @p)))
|
64
|
-
end
|
65
19
|
|
66
|
-
|
67
|
-
|
68
|
-
|
20
|
+
def probability(k)
|
21
|
+
Math.binomial(@n, k) * @p**k * (1 - @p)**(@n - k)
|
22
|
+
end
|
69
23
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
74
37
|
end
|
75
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
|
+
|
76
84
|
end
|
77
|
-
|
78
85
|
end
|
79
86
|
|
data/test/test_bernoulli.rb
CHANGED
@@ -2,23 +2,28 @@ require 'minitest/autorun'
|
|
2
2
|
require 'minitest/pride'
|
3
3
|
require 'bernoulli'
|
4
4
|
|
5
|
-
class
|
5
|
+
class DistributionTest < MiniTest::Unit::TestCase
|
6
6
|
|
7
7
|
def test_new_experiment
|
8
8
|
assert_raises RuntimeError do
|
9
|
-
Bernoulli.new(10, 1.1)
|
9
|
+
Bernoulli::Distribution.new(10, 1.1)
|
10
10
|
end
|
11
11
|
assert_raises RuntimeError do
|
12
|
-
Bernoulli.new(-10, 0.7)
|
12
|
+
Bernoulli::Distribution.new(-10, 0.7)
|
13
13
|
end
|
14
14
|
assert_raises RuntimeError do
|
15
|
-
Bernoulli.new(4.5, 0.2)
|
15
|
+
Bernoulli::Distribution.new(4.5, 0.2)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
def setup
|
20
|
-
@x = Bernoulli.new(180, 1.0/6)
|
21
|
-
@y = Bernoulli.new(2, 0.5)
|
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
|
22
27
|
end
|
23
28
|
|
24
29
|
def test_coin_toss
|
@@ -47,5 +52,10 @@ class BernoulliTest < MiniTest::Unit::TestCase
|
|
47
52
|
assert_in_delta 0.0066666666666667, @x.excess, 1.0e-7
|
48
53
|
end
|
49
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
|
+
|
50
60
|
end
|
51
61
|
|
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.
|
4
|
+
version: 0.3.0
|
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-
|
12
|
+
date: 2012-06-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|