ruby-statistics 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +3 -4
- data/lib/statistics/distribution/bernoulli.rb +35 -0
- data/lib/statistics/distribution/geometric.rb +76 -0
- data/lib/statistics/distribution/logseries.rb +51 -0
- data/lib/statistics/distribution/negative_binomial.rb +51 -0
- data/lib/statistics/version.rb +1 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1e73ce22e1ad6da4f9d2925ed5884d6e72d915ebad14b56b9e81c64c422c14cf
|
4
|
+
data.tar.gz: 18fa7cafd8bbf457dd8963b2e666525964ad588257ffc8774cee2d1250f4b469
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d362a9a2a5ea950ccc37ca5754dca0644040f49563afb14bbd10792ac2ef1f13853724cb693107fa6eb571972432026c5b3cf363ea4932513360f33513147401
|
7
|
+
data.tar.gz: cd5dd0a2b386fcaa0190d369a1f9d40248d4ed315e165df43403884cb33438902dd080a3f1579ba498eb88a5f2936ba91f6cd4a9e31a948ac31e2d124ba232fe
|
data/.travis.yml
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Statistics
|
2
|
+
module Distribution
|
3
|
+
class Bernoulli
|
4
|
+
def self.density_function(n, p)
|
5
|
+
return if n != 0 && n != 1 # The support of the distribution is n = {0, 1}.
|
6
|
+
|
7
|
+
case n
|
8
|
+
when 0 then 1.0 - p
|
9
|
+
when 1 then p
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.cumulative_function(n, p)
|
14
|
+
return if n != 0 && n != 1 # The support of the distribution is n = {0, 1}.
|
15
|
+
|
16
|
+
case n
|
17
|
+
when 0 then 1.0 - p
|
18
|
+
when 1 then 1.0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.variance(p)
|
23
|
+
p * (1.0 - p)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.skewness(p)
|
27
|
+
(1.0 - 2.0*p).to_f / Math.sqrt(p * (1.0 - p))
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.kurtosis(p)
|
31
|
+
(6.0 * (p ** 2) - (6 * p) + 1) / (p * (1.0 - p))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Statistics
|
2
|
+
module Distribution
|
3
|
+
class Geometric
|
4
|
+
attr_accessor :probability_of_success, :always_success_allowed
|
5
|
+
|
6
|
+
def initialize(p, always_success: false)
|
7
|
+
self.probability_of_success = p.to_f
|
8
|
+
self.always_success_allowed = always_success
|
9
|
+
end
|
10
|
+
|
11
|
+
def density_function(k)
|
12
|
+
k = k.to_i
|
13
|
+
|
14
|
+
if always_success_allowed
|
15
|
+
return if k < 0
|
16
|
+
|
17
|
+
((1.0 - probability_of_success) ** k) * probability_of_success
|
18
|
+
else
|
19
|
+
return if k <= 0
|
20
|
+
|
21
|
+
((1.0 - probability_of_success) ** (k - 1.0)) * probability_of_success
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def cumulative_function(k)
|
26
|
+
k = k.to_i
|
27
|
+
|
28
|
+
if always_success_allowed
|
29
|
+
return if k < 0
|
30
|
+
|
31
|
+
1.0 - ((1.0 - probability_of_success) ** (k + 1.0))
|
32
|
+
else
|
33
|
+
return if k <= 0
|
34
|
+
|
35
|
+
1.0 - ((1.0 - probability_of_success) ** k)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def mean
|
40
|
+
if always_success_allowed
|
41
|
+
(1.0 - probability_of_success) / probability_of_success
|
42
|
+
else
|
43
|
+
1.0 / probability_of_success
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def median
|
48
|
+
if always_success_allowed
|
49
|
+
(-1.0 / Math.log2(1.0 - probability_of_success)).ceil - 1.0
|
50
|
+
else
|
51
|
+
(-1.0 / Math.log2(1.0 - probability_of_success)).ceil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def mode
|
56
|
+
if always_success_allowed
|
57
|
+
0.0
|
58
|
+
else
|
59
|
+
1.0
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def variance
|
64
|
+
(1.0 - probability_of_success) / (probability_of_success ** 2)
|
65
|
+
end
|
66
|
+
|
67
|
+
def skewness
|
68
|
+
(2.0 - probability_of_success) / Math.sqrt(1.0 - probability_of_success)
|
69
|
+
end
|
70
|
+
|
71
|
+
def kurtosis
|
72
|
+
6.0 + ((probability_of_success ** 2) / (1.0 - probability_of_success))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Statistics
|
2
|
+
module Distribution
|
3
|
+
class LogSeries
|
4
|
+
def self.density_function(k, p)
|
5
|
+
return if k <= 0
|
6
|
+
k = k.to_i
|
7
|
+
|
8
|
+
left = (-1.0 / Math.log(1.0 - p))
|
9
|
+
right = (p ** k).to_f
|
10
|
+
|
11
|
+
left * right / k
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.cumulative_function(k, p)
|
15
|
+
return if k <= 0
|
16
|
+
|
17
|
+
# Sadly, the incomplete beta function is converging
|
18
|
+
# too fast to zero and breaking the calculation on logs.
|
19
|
+
# So, we default to the basic definition of the CDF which is
|
20
|
+
# the integral (-Inf, K) of the PDF, with P(X <= x) which can
|
21
|
+
# be solved as a summation of all PDFs from 1 to K. Note that the summation approach
|
22
|
+
# only applies to discrete distributions.
|
23
|
+
#
|
24
|
+
# right = Math.incomplete_beta_function(p, (k + 1).floor, 0) / Math.log(1.0 - p)
|
25
|
+
# 1.0 + right
|
26
|
+
|
27
|
+
result = 0.0
|
28
|
+
1.upto(k) do |number|
|
29
|
+
result += self.density_function(number, p)
|
30
|
+
end
|
31
|
+
|
32
|
+
result
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.mode
|
36
|
+
1.0
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.mean(p)
|
40
|
+
(-1.0 / Math.log(1.0 - p)) * (p / (1.0 - p))
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.variance(p)
|
44
|
+
up = p + Math.log(1.0 - p)
|
45
|
+
down = ((1.0 - p) ** 2) * (Math.log(1.0 - p) ** 2)
|
46
|
+
|
47
|
+
(-1.0 * p) * (up / down.to_f)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Statistics
|
2
|
+
module Distribution
|
3
|
+
class NegativeBinomial
|
4
|
+
attr_accessor :number_of_failures, :probability_per_trial
|
5
|
+
|
6
|
+
def initialize(r, p)
|
7
|
+
self.number_of_failures = r.to_i
|
8
|
+
self.probability_per_trial = p
|
9
|
+
end
|
10
|
+
|
11
|
+
def probability_mass_function(k)
|
12
|
+
return if number_of_failures < 0 || k < 0 || k > number_of_failures
|
13
|
+
|
14
|
+
left = Math.combination(k + number_of_failures - 1, k)
|
15
|
+
right = ((1 - probability_per_trial) ** number_of_failures) * (probability_per_trial ** k)
|
16
|
+
|
17
|
+
left * right
|
18
|
+
end
|
19
|
+
|
20
|
+
def cumulative_function(k)
|
21
|
+
return if k < 0 || k > number_of_failures
|
22
|
+
k = k.to_i
|
23
|
+
|
24
|
+
1.0 - Math.incomplete_beta_function(probability_per_trial, k + 1, number_of_failures)
|
25
|
+
end
|
26
|
+
|
27
|
+
def mean
|
28
|
+
(probability_per_trial * number_of_failures)/(1 - probability_per_trial).to_f
|
29
|
+
end
|
30
|
+
|
31
|
+
def variance
|
32
|
+
(probability_per_trial * number_of_failures)/((1 - probability_per_trial) ** 2).to_f
|
33
|
+
end
|
34
|
+
|
35
|
+
def skewness
|
36
|
+
(1 + probability_per_trial).to_f / Math.sqrt(probability_per_trial * number_of_failures)
|
37
|
+
end
|
38
|
+
|
39
|
+
def mode
|
40
|
+
if number_of_failures > 1
|
41
|
+
up = probability_per_trial * (number_of_failures - 1)
|
42
|
+
down = (1 - probability_per_trial).to_f
|
43
|
+
|
44
|
+
(up/down).floor
|
45
|
+
elsif number_of_failures <= 1
|
46
|
+
0.0
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/statistics/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-statistics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- esteban zapata
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -137,10 +137,14 @@ files:
|
|
137
137
|
- lib/math.rb
|
138
138
|
- lib/statistics.rb
|
139
139
|
- lib/statistics/distribution.rb
|
140
|
+
- lib/statistics/distribution/bernoulli.rb
|
140
141
|
- lib/statistics/distribution/beta.rb
|
141
142
|
- lib/statistics/distribution/binomial.rb
|
142
143
|
- lib/statistics/distribution/chi_squared.rb
|
143
144
|
- lib/statistics/distribution/f.rb
|
145
|
+
- lib/statistics/distribution/geometric.rb
|
146
|
+
- lib/statistics/distribution/logseries.rb
|
147
|
+
- lib/statistics/distribution/negative_binomial.rb
|
144
148
|
- lib/statistics/distribution/normal.rb
|
145
149
|
- lib/statistics/distribution/poisson.rb
|
146
150
|
- lib/statistics/distribution/t_student.rb
|
@@ -173,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
177
|
version: '0'
|
174
178
|
requirements: []
|
175
179
|
rubyforge_project:
|
176
|
-
rubygems_version: 2.
|
180
|
+
rubygems_version: 2.7.3
|
177
181
|
signing_key:
|
178
182
|
specification_version: 4
|
179
183
|
summary: A ruby gem for som specific statistics. Inspired by the jStat js library.
|