distribution 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.txt +9 -0
  3. data/Manifest.txt +12 -0
  4. data/README.txt +24 -7
  5. data/Rakefile +2 -0
  6. data/benchmark/binomial_coefficient.rb +55 -0
  7. data/benchmark/factorial_method.rb +4 -1
  8. data/benchmark/odd.rb +22 -0
  9. data/lib/distribution.rb +11 -7
  10. data/lib/distribution/binomial.rb +22 -20
  11. data/lib/distribution/binomial/gsl.rb +14 -0
  12. data/lib/distribution/binomial/java.rb +9 -0
  13. data/lib/distribution/binomial/ruby.rb +26 -0
  14. data/lib/distribution/bivariatenormal.rb +7 -5
  15. data/lib/distribution/bivariatenormal/java.rb +9 -0
  16. data/lib/distribution/chisquare.rb +1 -0
  17. data/lib/distribution/chisquare/gsl.rb +3 -3
  18. data/lib/distribution/chisquare/java.rb +9 -0
  19. data/lib/distribution/f.rb +1 -0
  20. data/lib/distribution/f/gsl.rb +3 -3
  21. data/lib/distribution/f/java.rb +9 -0
  22. data/lib/distribution/hypergeometric.rb +2 -0
  23. data/lib/distribution/hypergeometric/gsl.rb +2 -2
  24. data/lib/distribution/hypergeometric/java.rb +9 -0
  25. data/lib/distribution/hypergeometric/ruby.rb +24 -27
  26. data/lib/distribution/math_extension.rb +90 -8
  27. data/lib/distribution/normal.rb +1 -0
  28. data/lib/distribution/normal/java.rb +9 -0
  29. data/lib/distribution/t.rb +2 -0
  30. data/lib/distribution/t/java.rb +9 -0
  31. data/lib/distribution/t/statistics2.rb +1 -1
  32. data/spec/binomial_spec.rb +118 -0
  33. data/spec/distribution_spec.rb +1 -1
  34. data/spec/f_spec.rb +1 -1
  35. data/spec/hypergeometric_spec.rb +49 -13
  36. data/spec/math_extension_spec.rb +57 -9
  37. data/spec/normal_spec.rb +7 -3
  38. data/spec/shorthand_spec.rb +16 -2
  39. data/spec/t_spec.rb +1 -1
  40. metadata +44 -5
  41. metadata.gz.sig +0 -0
@@ -13,7 +13,7 @@ describe Distribution do
13
13
  if Distribution.has_statistics2?
14
14
  defined?(Statistics2).should be_true
15
15
  else
16
- defined?(Statistics2).should be_true
16
+ defined?(Statistics2).should be_false
17
17
  end
18
18
  end
19
19
  end
@@ -42,7 +42,7 @@ shared_examples_for "F engine" do
42
42
  end
43
43
 
44
44
  end
45
- it "should return correct p_value" do
45
+ it_only_with_gsl "should return correct p_value" do
46
46
  if @engine.respond_to? :p_value
47
47
 
48
48
  [0.1,0.5,1,2,10,20,30].each{|f|
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
2
  include ExampleWithGSL
3
- require 'ruby-prof'
3
+ #require 'ruby-prof'
4
4
 
5
5
  # Need to test:
6
6
  # * that Fixnum fast_choose returns same as choose
@@ -8,30 +8,55 @@ require 'ruby-prof'
8
8
  # * that cdf in Ruby_ returns the same value as cdf in GSL_
9
9
 
10
10
  describe Distribution::Hypergeometric do
11
+ describe Distribution::Hypergeometric::GSL_ do
12
+ before do
13
+ @ruby=Distribution::Hypergeometric::Ruby_
14
+ @engine=Distribution::Hypergeometric::GSL_
15
+ end
16
+ it_only_with_gsl "cdf should return values near to exact Ruby calculations" do
17
+ #RubyProf.start
18
+
19
+ if @engine.respond_to? :cdf
20
+ #[2].each do |k|
21
+ [0,1,2,4,8,16].each do |k|
22
+ @engine.cdf(k, 80, 100, 10000).should be_within(1e-10).of(@ruby.cdf(k, 80, 100, 10000))
23
+ end
24
+ #result = RubyProf.stop
25
+
26
+ # Print a flat profile to text
27
+ #printer = RubyProf::FlatPrinter.new(result)
28
+ #printer.print(STDOUT)
29
+
30
+ else
31
+ pending("No #{@engine}.pdf")
32
+ end
33
+ end
34
+
35
+ end
11
36
  describe Distribution::Hypergeometric::Ruby_ do
37
+
12
38
  before do
13
39
  @engine=Distribution::Hypergeometric::Ruby_
14
40
  end
15
41
  it_only_with_gsl "pdf_fast should return same as pdf" do
16
- pending("Not working yet")
42
+ pending("Aprox. factorial doesn't work right")
17
43
  if @engine.respond_to? :pdf
18
44
  [0,1,2,4,8,16].each do |k|
19
-
20
45
  @engine.pdf_aprox(k, 80, 100, 1000).should be_within(1e-8).of(GSL::Ran::hypergeometric_pdf(k, 80, 920, 100))
21
46
  end
22
47
  else
23
48
  pending("No #{@engine}.pdf")
24
49
  end
25
50
  end
26
-
27
-
51
+
28
52
  it_only_with_gsl "should return correct pdf" do
29
53
  #RubyProf.start
30
54
 
31
55
  if @engine.respond_to? :pdf
32
56
  #[2].each do |k|
33
57
  [0,1,2,4,8,16].each do |k|
34
- @engine.pdf(k, 80, 100, 10000).should be_within(1e-8).of(GSL::Ran::hypergeometric_pdf(k, 80, 9920, 100))
58
+ #puts "k:#{k}->#{@engine.pdf(k, 80, 100, 10000).to_f}"
59
+ @engine.pdf(k, 80, 100, 10000).to_f.should be_within(1e-8).of(GSL::Ran::hypergeometric_pdf(k, 80, 9920, 100))
35
60
  end
36
61
  #result = RubyProf.stop
37
62
 
@@ -44,24 +69,25 @@ describe Distribution::Hypergeometric do
44
69
  end
45
70
  end
46
71
 
72
+
73
+
74
+
47
75
  it "should return correct cdf" do
48
- total=rand(5)+30
76
+ total=rand(5)+3000
49
77
  n=rand(10)+15
50
78
  m=rand(10)+5
51
79
  ac=0
52
80
  0.upto(m) do |i|
53
81
  ac+=@engine.pdf(i,m,n,total)
54
82
  @engine.cdf(i,m,n,total).should eq(ac)
55
-
56
83
  end
57
84
  end
58
85
 
59
86
  it "should return correct p_value" do
60
-
61
87
  #0.upto(10) do |i|
62
88
  # puts "#{i}:#{@engine.pdf(i,5,7,10)}"
63
89
  #end
64
- total=rand(5)+30
90
+ total=rand(5)+3000
65
91
  n=rand(10)+15
66
92
  m=rand(10)+5
67
93
  ac=0
@@ -69,9 +95,19 @@ describe Distribution::Hypergeometric do
69
95
  ac+=@engine.pdf(k,m,n,total)
70
96
  @engine.p_value(ac, m, n, total).should eq(k)
71
97
  end
72
- end
73
-
74
-
98
+ end
75
99
  end
100
+ describe Distribution::Hypergeometric do
101
+ before do
102
+ @engine=Distribution::Hypergeometric
103
+ end
104
+ it {@engine.should respond_to(:exact_pdf) }
105
+ it {@engine.should respond_to(:exact_cdf) }
106
+ it {@engine.should respond_to(:exact_p_value) }
107
+ it "exact pdf should return a Rational" do
108
+ @engine.exact_pdf(1,1,1,1).should_not be_a(Float)
109
+ @engine.exact_pdf(16, 80, 100, 10000).should_not be_a(Float)
110
+ end
76
111
 
112
+ end
77
113
  end
@@ -1,17 +1,63 @@
1
1
  require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
2
  describe Distribution::MathExtension do
3
- it "fast_factorial should return 11 valid digits" do
4
- pending("Doesn't worth the effort")
5
- valid=11
6
- (2..10).each {|i|
7
- n=2**i
8
- aprox=Math.fast_factorial(n).round.to_s[0,valid]
9
- exact=Math.factorial(n).to_s[0,valid]
10
- aprox.should eq exact
11
-
3
+ it "binomial coefficient should be correctly calculated" do
4
+
5
+ n=50
6
+ n.times do |k|
7
+ Math.binomial_coefficient(n,k).should eq(Math.factorial(n).quo(Math.factorial(k)*Math.factorial(n-k)))
8
+ end
9
+ end
10
+
11
+ it "binomial coefficient(gamma) with n<=48 should be correct " do
12
+
13
+ [1,5,10,25,48].each {|n|
14
+ k=(n/2).to_i
15
+ Math.binomial_coefficient_gamma(n,k).round.should eq(Math.binomial_coefficient(n,k))
16
+ }
17
+ end
18
+ it "rising_factorial should return correct values" do
19
+
20
+ x=rand(10)+1
21
+ Math.rising_factorial(x,0).should eq 1
22
+ Math.rising_factorial(x,1).should eq x
23
+ Math.rising_factorial(x,2).should eq x**2+x
24
+ Math.rising_factorial(x,3).should eq x**3+3*x**2+2*x
25
+ Math.rising_factorial(x,4).should eq x**4+6*x**3+11*x**2+6*x
26
+
27
+ end
28
+ it "incomplete beta function should return similar results to R" do
29
+ pending("Not working yet")
30
+ Math.incomplete_beta(0.5,5,6).should be_within(1e-6).of(Math.beta(5,6)*0.6230469)
31
+ Math.incomplete_beta(0.6,5,6).should be_within(1e-6).of(Math.beta(5,6)*0.0006617154)
32
+ end
33
+ it "regularized incomplete beta should behave properly" do
34
+
35
+ Math.regularized_beta_function(0.5,5,5).should eq 0.5
36
+ Math.regularized_beta_function(0.5,5,6).should be_within(1e-6).of(0.6230469)
37
+ Math.regularized_beta_function(0.5,5,7).should be_within(1e-6).of(0.725586)
38
+
39
+ a=5
40
+ b=5
41
+ Math.regularized_beta_function(0,a,b).should eq 0
42
+ Math.regularized_beta_function(1,a,b).should eq 1
43
+ x=rand()
44
+
45
+ Math.regularized_beta_function(x,a,b).should be_within(1e-6). of(1-Math.regularized_beta_function(1-x,b,a))
46
+
47
+
48
+ end
49
+ it "binomial coefficient(gamma) with 48<n<1000 should have 12 correct digits" do
50
+
51
+ [50,100,1000].each {|n|
52
+ k=n/2.to_i
53
+ obs=Math.binomial_coefficient_gamma(n,k).to_i.to_s[0,12]
54
+ exp=Math.binomial_coefficient(n,k).to_i.to_s[0,12]
55
+ obs.should eq(exp)
12
56
  }
13
57
  end
58
+
14
59
  describe Distribution::MathExtension::SwingFactorial do
60
+
15
61
  it "Math.factorial should return correct values x<20" do
16
62
  ac=3628800 # 10!
17
63
  11.upto(19).each do |i|
@@ -21,6 +67,7 @@ describe Distribution::MathExtension do
21
67
  end
22
68
 
23
69
  it "Math.factorial should return correct values for values 21<x<33" do
70
+
24
71
  ac=2432902008176640000 # 20!
25
72
  21.upto(33).each do |i|
26
73
  ac*=i
@@ -30,6 +77,7 @@ describe Distribution::MathExtension do
30
77
  end
31
78
 
32
79
  it "Math.factorial should return correct values for values x>33" do
80
+
33
81
  ac=8683317618811886495518194401280000000 # 33!
34
82
  Math.factorial(33).should eq ac
35
83
  34.upto(40).each do |i|
@@ -40,8 +40,12 @@ end
40
40
 
41
41
  shared_examples_for "gaussian engine" do
42
42
  it "should return correct cdf" do
43
- @engine.cdf(1.96).should be_within(1e-10).of(0.97500210485178)
44
- @engine.cdf(0).should be_within(1e-10).of(0.5)
43
+ if @engine.respond_to? :cdf
44
+ @engine.cdf(1.96).should be_within(1e-10).of(0.97500210485178)
45
+ @engine.cdf(0).should be_within(1e-10).of(0.5)
46
+ else
47
+ pending("No #{@engine}.cdf")
48
+ end
45
49
  end
46
50
  it "should return correct p_value" do
47
51
  if @engine.respond_to? :p_value
@@ -98,7 +102,7 @@ end
98
102
  before do
99
103
  @engine=Distribution::Normal::Java_
100
104
  end
101
- it_should_behave_like "all gaussian engines"
105
+ it_should_behave_like "gaussian engine"
102
106
  end
103
107
  end
104
108
 
@@ -2,15 +2,29 @@ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
2
  describe Distribution::Shorthand do
3
3
  include Distribution::Shorthand
4
4
  it "should have basic methods for all distributions" do
5
- [:Normal,:ChiSquare, :F, :Hypergeometric, :T].each do |d|
5
+ [:Normal,:ChiSquare, :F, :Hypergeometric, :Binomial, :T].each do |d|
6
6
  klass=Distribution.const_get(d)
7
7
  shortname=klass::SHORTHAND
8
8
  methods=[:pdf, :cdf, :p_value].map {|m| "#{shortname}_#{m}".to_sym}
9
9
  methods.each do |m|
10
- Distribution::Shorthand.instance_methods.should include(m)
10
+ Distribution::Shorthand.instance_methods.map {|v| v.to_sym}.should include(m)
11
11
  end
12
12
  end
13
+
13
14
  end
15
+ it "should have exact methods discrete distributions" do
16
+ [:Hypergeometric, :Binomial].each do |d|
17
+ klass=Distribution.const_get(d)
18
+ shortname=klass::SHORTHAND
19
+ methods=[:epdf, :ecdf].map {|m| "#{shortname}_#{m}".to_sym}
20
+ methods.each do |m|
21
+ Distribution::Shorthand.instance_methods.map {|v| v.to_sym}.should include(m)
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+
14
28
  it "returns same values as long form" do
15
29
  x=rand()
16
30
  norm_cdf(x).should eql(Distribution::Normal.cdf(x))
@@ -38,7 +38,7 @@ shared_examples_for "T engine" do
38
38
  end
39
39
 
40
40
  end
41
- it "should return correct p_value" do
41
+ it_only_with_gsl "should return correct p_value" do
42
42
  if @engine.respond_to? :p_value
43
43
  [-2,0.1,0.5,1,2].each{|t|
44
44
  [2,5,10].each{|n|
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Claudio Bustos
@@ -35,7 +35,7 @@ cert_chain:
35
35
  rpP0jjs0
36
36
  -----END CERTIFICATE-----
37
37
 
38
- date: 2011-01-27 00:00:00 -03:00
38
+ date: 2011-01-28 00:00:00 -03:00
39
39
  default_executable:
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
@@ -54,9 +54,36 @@ dependencies:
54
54
  type: :development
55
55
  version_requirements: *id001
56
56
  - !ruby/object:Gem::Dependency
57
- name: hoe
57
+ name: rspec
58
58
  prerelease: false
59
59
  requirement: &id002 !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 2
66
+ - 0
67
+ version: "2.0"
68
+ type: :development
69
+ version_requirements: *id002
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubyforge
72
+ prerelease: false
73
+ requirement: &id003 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id003
83
+ - !ruby/object:Gem::Dependency
84
+ name: hoe
85
+ prerelease: false
86
+ requirement: &id004 !ruby/object:Gem::Requirement
60
87
  none: false
61
88
  requirements:
62
89
  - - ">="
@@ -67,7 +94,7 @@ dependencies:
67
94
  - 0
68
95
  version: 2.8.0
69
96
  type: :development
70
- version_requirements: *id002
97
+ version_requirements: *id004
71
98
  description: |-
72
99
  Statistical Distributions multi library wrapper.
73
100
  Uses Ruby by default and C (statistics2/GSL) or Java extensions where available.
@@ -89,35 +116,47 @@ files:
89
116
  - Manifest.txt
90
117
  - README.txt
91
118
  - Rakefile
119
+ - benchmark/binomial_coefficient.rb
92
120
  - benchmark/factorial_method.rb
121
+ - benchmark/odd.rb
93
122
  - bin/distribution
94
123
  - lib/distribution.rb
95
124
  - lib/distribution/binomial.rb
125
+ - lib/distribution/binomial/gsl.rb
126
+ - lib/distribution/binomial/java.rb
127
+ - lib/distribution/binomial/ruby.rb
96
128
  - lib/distribution/bivariatenormal.rb
97
129
  - lib/distribution/bivariatenormal/gsl.rb
130
+ - lib/distribution/bivariatenormal/java.rb
98
131
  - lib/distribution/bivariatenormal/ruby.rb
99
132
  - lib/distribution/bivariatenormal/statistics2.rb
100
133
  - lib/distribution/chisquare.rb
101
134
  - lib/distribution/chisquare/gsl.rb
135
+ - lib/distribution/chisquare/java.rb
102
136
  - lib/distribution/chisquare/ruby.rb
103
137
  - lib/distribution/chisquare/statistics2.rb
104
138
  - lib/distribution/f.rb
105
139
  - lib/distribution/f/gsl.rb
140
+ - lib/distribution/f/java.rb
106
141
  - lib/distribution/f/ruby.rb
107
142
  - lib/distribution/f/statistics2.rb
108
143
  - lib/distribution/hypergeometric.rb
109
144
  - lib/distribution/hypergeometric/gsl.rb
145
+ - lib/distribution/hypergeometric/java.rb
110
146
  - lib/distribution/hypergeometric/ruby.rb
111
147
  - lib/distribution/math_extension.rb
112
148
  - lib/distribution/normal.rb
113
149
  - lib/distribution/normal/gsl.rb
150
+ - lib/distribution/normal/java.rb
114
151
  - lib/distribution/normal/ruby.rb
115
152
  - lib/distribution/normal/statistics2.rb
116
153
  - lib/distribution/normalmultivariate.rb
117
154
  - lib/distribution/t.rb
118
155
  - lib/distribution/t/gsl.rb
156
+ - lib/distribution/t/java.rb
119
157
  - lib/distribution/t/ruby.rb
120
158
  - lib/distribution/t/statistics2.rb
159
+ - spec/binomial_spec.rb
121
160
  - spec/bivariatenormal_spec.rb
122
161
  - spec/chisquare_spec.rb
123
162
  - spec/distribution_spec.rb
metadata.gz.sig CHANGED
Binary file