croupier 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -1
- data/lib/croupier/distributions/geometric.rb +45 -0
- data/lib/croupier/distributions/nbinomial.rb +57 -0
- data/test/distributions/R_tests/test_geometric.R +23 -0
- data/test/distributions/R_tests/test_nbinomial.R +17 -0
- data/test/distributions/R_tests/test_normal.R +8 -8
- metadata +8 -2
data/README.md
CHANGED
@@ -32,7 +32,16 @@ where:
|
|
32
32
|
|
33
33
|
### Available distributions and options
|
34
34
|
|
35
|
-
Current version
|
35
|
+
Current version implements the following distributions:
|
36
|
+
|
37
|
+
* Cauchy
|
38
|
+
* Exponential
|
39
|
+
* Geometric
|
40
|
+
* Negative binomial
|
41
|
+
* Normal
|
42
|
+
* Poisson
|
43
|
+
* Triangular
|
44
|
+
* Uniform
|
36
45
|
|
37
46
|
To get a list of all the available distributions use the ```--help``` (or ```-h```) option with croupier:
|
38
47
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Croupier
|
2
|
+
module Distributions
|
3
|
+
|
4
|
+
#####################################################################
|
5
|
+
# Geometric Distribution
|
6
|
+
# Discrete probability distribution that expresses the number of X
|
7
|
+
# Bernoulli trials needed to get one success, supported on the
|
8
|
+
# set { 1, 2, 3, ...}
|
9
|
+
#
|
10
|
+
# Wikipedia -- http://en.wikipedia.org/wiki/Geometric_distribution
|
11
|
+
# I made this choice because it's Knuth choice.
|
12
|
+
# (The Art of Computer Programming, Volume 2, 3.4.1.F )
|
13
|
+
class Geometric < ::Croupier::Distribution
|
14
|
+
|
15
|
+
def initialize(options={})
|
16
|
+
@name = "Geometric distribution"
|
17
|
+
@description = "Discrete probability distribution that expresses the the number of X Bernoulli trials needed to get one success, supported on the set { 1, 2, 3, ...}"
|
18
|
+
configure(options)
|
19
|
+
raise Croupier::InputParamsError, "Probability of success must be in the interval [0,1]" if params[:success] > 1 || params[:success] < 0
|
20
|
+
end
|
21
|
+
|
22
|
+
# Fair point: it is not the inverse of the cdf,
|
23
|
+
# but it generates the distribution from an uniform.
|
24
|
+
def inv_cdf n
|
25
|
+
(Math.log(1-n) / Math.log(1-params[:success])).ceil
|
26
|
+
end
|
27
|
+
|
28
|
+
def default_parameters
|
29
|
+
{:success => 0.5}
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.cli_name
|
33
|
+
"geometric"
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.cli_options
|
37
|
+
{:options => [
|
38
|
+
[:success, 'success probability of each trial', {:type=>:float, :short => "-p", :default => 0.5}]
|
39
|
+
],
|
40
|
+
:banner => "Geometric distribution. Discrete probability distribution that expresses the number of X Bernoulli trials needed to get one success, supported on the set { 1, 2, 3, ...} }"
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Croupier
|
2
|
+
module Distributions
|
3
|
+
|
4
|
+
#####################################################################
|
5
|
+
# Negative Binomial Distribution
|
6
|
+
# Discrete probability distribution of the number of successes in a
|
7
|
+
# sequence of Bernoulli trials before a specified (non-random)
|
8
|
+
# number of failures (denoted size) occur.
|
9
|
+
#
|
10
|
+
# The parameter prob expresses the probability of success.
|
11
|
+
# Wikipedia -- http://en.wikipedia.org/wiki/Negative_binomial_distribution
|
12
|
+
class Nbinomial < ::Croupier::Distribution
|
13
|
+
|
14
|
+
def initialize(options={})
|
15
|
+
@name = "Negative binomial distribution"
|
16
|
+
@description = "Discrete probability distribution of the number of successes in a sequence of Bernoulli trials before a specified (non-random) number of failures (denoted size) occur."
|
17
|
+
configure(options)
|
18
|
+
raise Croupier::InputParamsError, "Probability of success must be in the interval [0,1]" if params[:success] > 1 || params[:success] < 0
|
19
|
+
end
|
20
|
+
|
21
|
+
# Fair point: it is not the inverse of the cdf,
|
22
|
+
# but it generates the distribution from an uniform.
|
23
|
+
def generate_sample n=1
|
24
|
+
generate_geometrics(n).each_slice(params[:size]).map do |sample|
|
25
|
+
sample.inject(-params[:size], &:+) # Inject starts on -size because
|
26
|
+
# this way it is equivalent to:
|
27
|
+
# sample.map{|x| x - 1}.inject(&:+)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def base_geometric
|
32
|
+
::Croupier::Distributions::Geometric.new(success: params[:success])
|
33
|
+
end
|
34
|
+
|
35
|
+
def generate_geometrics(n)
|
36
|
+
base_geometric.generate_sample(params[:size]*n)
|
37
|
+
end
|
38
|
+
|
39
|
+
def default_parameters
|
40
|
+
{:success => 0.5, :size => 1}
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.cli_name
|
44
|
+
"nbinomial"
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.cli_options
|
48
|
+
{:options => [
|
49
|
+
[:size, 'number of errors', {:type => :integer, :default => 1}],
|
50
|
+
[:success, 'success probability of each trial', {:type=>:float, :short => "-p", :default => 0.5}]
|
51
|
+
],
|
52
|
+
:banner => "Negative binomial distribution. Discrete probability distribution of the number of successes in a sequence of Bernoulli trials before a specified (non-random) number of failures (denoted size) occur."
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
context('** Geometric Distribution **')
|
2
|
+
|
3
|
+
# It uses the fact that it is equivalent to a negative binomial
|
4
|
+
# distribution with parameters size=1 and prob = p (Wikipedia
|
5
|
+
# set p_{nbinomial} to be the fail probability while R takes
|
6
|
+
# it as the success one).
|
7
|
+
# Besides, since for the equality to hold the support must start
|
8
|
+
# at zero, it just substracts one from the vector of samples.
|
9
|
+
context('ChiSquare Goodness of Fit test for p=0.5')
|
10
|
+
croupier_geometric <- read.table("../generated_samples/geometric_05.data")
|
11
|
+
cs_result<-summary(goodfit(croupier_geometric$V1 - 1, type="nbinomial", method="ML", par=list(size=1, prob=0.5)))
|
12
|
+
|
13
|
+
test_that("p-value > 0.05, geometric, p = 0.5", {
|
14
|
+
expect_true(cs_result['Pearson',3] > 0.05)
|
15
|
+
})
|
16
|
+
|
17
|
+
context('ChiSquare Goodness of Fit test for p = 0.05')
|
18
|
+
croupier_geometric <- read.table("../generated_samples/geometric_005.data")
|
19
|
+
cs_result<-summary(goodfit(croupier_geometric$V1 - 1, type="nbinomial", method="ML", par=list(size=1, prob=0.05)))
|
20
|
+
|
21
|
+
test_that("p-value > 0.05, geometric, p=0.05", {
|
22
|
+
expect_true(cs_result['Pearson',3] > 0.05)
|
23
|
+
})
|
@@ -0,0 +1,17 @@
|
|
1
|
+
context('** Negative Binomial Distribution **')
|
2
|
+
|
3
|
+
context('ChiSquare Goodness of Fit test for prob=0.5, size=5')
|
4
|
+
croupier_nbinomial <- read.table("../generated_samples/nbinomial_05_5.data")
|
5
|
+
cs_result<-summary(goodfit(croupier_nbinomial$V1, type="nbinomial", method="ML", par=list(size=5, prob=0.5)))
|
6
|
+
|
7
|
+
test_that("p-value > 0.05, nbinomial, prob = 0.5, size=5", {
|
8
|
+
expect_true(cs_result['Pearson',3] > 0.05)
|
9
|
+
})
|
10
|
+
|
11
|
+
context('ChiSquare Goodness of Fit test for prob = 0.25, size=15')
|
12
|
+
croupier_nbinomial <- read.table("../generated_samples/nbinomial_025_15.data")
|
13
|
+
cs_result<-summary(goodfit(croupier_nbinomial$V1, type="nbinomial", method="ML", par=list(size=15, prob=0.25)))
|
14
|
+
|
15
|
+
test_that("p-value > 0.05, nbinomial, prob=0.25, size=15", {
|
16
|
+
expect_true(cs_result['Pearson',3] > 0.05)
|
17
|
+
})
|
@@ -7,11 +7,11 @@ context('Kolmogorov-Smirnov test for default mean = 0 and std = 1')
|
|
7
7
|
croupier_normal <- read.table("../generated_samples/normal_0_1.data")
|
8
8
|
ks_result<-ks.test(croupier_normal$V1, "pnorm")
|
9
9
|
|
10
|
-
test_that("p-value > 0.05", {
|
10
|
+
test_that("p-value > 0.05, normal (0,1)", {
|
11
11
|
expect_true(ks_result$p.value > 0.05)
|
12
12
|
})
|
13
13
|
|
14
|
-
test_that("statistic converging to 0", {
|
14
|
+
test_that("statistic converging to 0, normal(0,1)", {
|
15
15
|
expect_true(as.numeric(ks_result$statistic) < 0.05)
|
16
16
|
})
|
17
17
|
|
@@ -19,11 +19,11 @@ context('Shapiro test for default mean = 0 and std = 1')
|
|
19
19
|
croupier_normal <- read.table("../generated_samples/normal_0_1.data")
|
20
20
|
s_result<-shapiro.test(croupier_normal$V1[1:5000])
|
21
21
|
|
22
|
-
test_that("p-value > 0.05", {
|
22
|
+
test_that("p-value > 0.05, normal (0,1)", {
|
23
23
|
expect_true(s_result$p.value > 0.05)
|
24
24
|
})
|
25
25
|
|
26
|
-
test_that("statistic converging to 0", {
|
26
|
+
test_that("statistic converging to 0, normal(0,1)", {
|
27
27
|
expect_true(as.numeric(ks_result$statistic) < 0.05)
|
28
28
|
})
|
29
29
|
|
@@ -31,11 +31,11 @@ context('Kolmogorov-Smirnov test for given mean = 5 and std = 6')
|
|
31
31
|
croupier_normal <- read.table("../generated_samples/normal_5_6.data")
|
32
32
|
ks_result<-ks.test((croupier_normal$V1 - 5)/6, "pnorm")
|
33
33
|
|
34
|
-
test_that("p-value > 0.05", {
|
34
|
+
test_that("p-value > 0.05, normal(5,6)", {
|
35
35
|
expect_true(ks_result$p.value > 0.05)
|
36
36
|
})
|
37
37
|
|
38
|
-
test_that("statistic converging to 0", {
|
38
|
+
test_that("statistic converging to 0, normal(5,6)", {
|
39
39
|
expect_true(as.numeric(ks_result$statistic) < 0.05)
|
40
40
|
})
|
41
41
|
|
@@ -43,10 +43,10 @@ context('Shapiro test for default mean = 5 and std = 1')
|
|
43
43
|
croupier_normal <- read.table("../generated_samples/normal_0_1.data")
|
44
44
|
s_result<-shapiro.test((croupier_normal$V1[1:5000] - 5 ) / 6)
|
45
45
|
|
46
|
-
test_that("p-value > 0.05", {
|
46
|
+
test_that("p-value > 0.05, normal(5,6)", {
|
47
47
|
expect_true(s_result$p.value > 0.05)
|
48
48
|
})
|
49
49
|
|
50
|
-
test_that("statistic converging to 0", {
|
50
|
+
test_that("statistic converging to 0, normal(5,6)", {
|
51
51
|
expect_true(as.numeric(ks_result$statistic) < 0.05)
|
52
52
|
})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: croupier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-06-
|
13
|
+
date: 2012-06-22 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Croupier is a Ruby gem to generate a random sample of numbers with a
|
16
16
|
given probability distribution.
|
@@ -30,6 +30,8 @@ files:
|
|
30
30
|
- lib/croupier/distribution.rb
|
31
31
|
- lib/croupier/distributions/cauchy.rb
|
32
32
|
- lib/croupier/distributions/exponential.rb
|
33
|
+
- lib/croupier/distributions/geometric.rb
|
34
|
+
- lib/croupier/distributions/nbinomial.rb
|
33
35
|
- lib/croupier/distributions/normal.rb
|
34
36
|
- lib/croupier/distributions/poisson.rb
|
35
37
|
- lib/croupier/distributions/triangular.rb
|
@@ -38,6 +40,8 @@ files:
|
|
38
40
|
- lib/croupier.rb
|
39
41
|
- test/distributions/R_tests/test_cauchy.R
|
40
42
|
- test/distributions/R_tests/test_exponential.R
|
43
|
+
- test/distributions/R_tests/test_geometric.R
|
44
|
+
- test/distributions/R_tests/test_nbinomial.R
|
41
45
|
- test/distributions/R_tests/test_normal.R
|
42
46
|
- test/distributions/R_tests/test_poisson.R
|
43
47
|
- test/distributions/R_tests/test_triangular.R
|
@@ -76,6 +80,8 @@ summary: Samples of random numbers with specific probability distributions
|
|
76
80
|
test_files:
|
77
81
|
- test/distributions/R_tests/test_cauchy.R
|
78
82
|
- test/distributions/R_tests/test_exponential.R
|
83
|
+
- test/distributions/R_tests/test_geometric.R
|
84
|
+
- test/distributions/R_tests/test_nbinomial.R
|
79
85
|
- test/distributions/R_tests/test_normal.R
|
80
86
|
- test/distributions/R_tests/test_poisson.R
|
81
87
|
- test/distributions/R_tests/test_triangular.R
|