distribution 0.1.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.
Files changed (42) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.autotest +23 -0
  3. data/History.txt +3 -0
  4. data/Manifest.txt +39 -0
  5. data/README.txt +71 -0
  6. data/Rakefile +19 -0
  7. data/bin/distribution +3 -0
  8. data/lib/distribution.rb +148 -0
  9. data/lib/distribution/bivariatenormal.rb +25 -0
  10. data/lib/distribution/bivariatenormal/gsl.rb +11 -0
  11. data/lib/distribution/bivariatenormal/ruby.rb +281 -0
  12. data/lib/distribution/bivariatenormal/statistics2.rb +0 -0
  13. data/lib/distribution/chisquare.rb +29 -0
  14. data/lib/distribution/chisquare/gsl.rb +27 -0
  15. data/lib/distribution/chisquare/ruby.rb +85 -0
  16. data/lib/distribution/chisquare/statistics2.rb +21 -0
  17. data/lib/distribution/f.rb +28 -0
  18. data/lib/distribution/f/gsl.rb +28 -0
  19. data/lib/distribution/f/ruby.rb +117 -0
  20. data/lib/distribution/f/statistics2.rb +26 -0
  21. data/lib/distribution/math_extension.rb +72 -0
  22. data/lib/distribution/normal.rb +36 -0
  23. data/lib/distribution/normal/gsl.rb +24 -0
  24. data/lib/distribution/normal/ruby.rb +99 -0
  25. data/lib/distribution/normal/statistics2.rb +14 -0
  26. data/lib/distribution/normalmultivariate.rb +73 -0
  27. data/lib/distribution/t.rb +27 -0
  28. data/lib/distribution/t/gsl.rb +29 -0
  29. data/lib/distribution/t/ruby.rb +105 -0
  30. data/lib/distribution/t/statistics2.rb +28 -0
  31. data/spec/bivariatenormal_spec.rb +63 -0
  32. data/spec/chisquare_spec.rb +89 -0
  33. data/spec/distribution_spec.rb +19 -0
  34. data/spec/f_spec.rb +107 -0
  35. data/spec/normal_spec.rb +105 -0
  36. data/spec/shorthand_function.rb +6 -0
  37. data/spec/shorthand_spec.rb +14 -0
  38. data/spec/spec.opts +3 -0
  39. data/spec/spec_helper.rb +23 -0
  40. data/spec/t_spec.rb +98 -0
  41. metadata +160 -0
  42. metadata.gz.sig +1 -0
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+ include ExampleWithGSL
3
+ describe Distribution::BivariateNormal do
4
+ shared_examples_for "all pdf normal capables engines" do
5
+ it_only_with_gsl "should return correct pdf" do
6
+ if @engine.respond_to? :pdf
7
+ [0.2,0.4,0.6,0.8,0.9, 0.99,0.999,0.999999].each {|rho|
8
+ @engine.pdf(0,0, rho , 1,1).should be_within(1e-8).of(GSL::Ran::bivariate_gaussian_pdf(0, 0, 1,1,rho))
9
+ }
10
+ else
11
+ pending("No #{@engine}.pdf")
12
+ end
13
+ end
14
+ end
15
+
16
+ shared_examples_for "all cdf normal capables engines" do
17
+ it "should return correct cdf" do
18
+ if @engine.respond_to? :cdf
19
+ @engine.cdf(2,0.5,0.5).should be_within(1e-3).of(0.686)
20
+ @engine.cdf(2,0.0,0.5).should be_within(1e-3).of(0.498)
21
+ @engine.cdf(1.5,0.5,0.5).should be_within(1e-3).of(0.671)
22
+ v=rand
23
+ @engine.cdf(10,0,v).should be_within(1e-3).of(Distribution::Normal.cdf(0))
24
+ else
25
+ pending("No #{@engine}.cdf")
26
+
27
+ end
28
+ end
29
+
30
+ end
31
+ describe "singleton" do
32
+ before do
33
+ @engine=Distribution::BivariateNormal
34
+ end
35
+ it_should_behave_like "all pdf normal capables engines"
36
+ it_should_behave_like "all cdf normal capables engines"
37
+
38
+ end
39
+
40
+ describe Distribution::Normal::Ruby_ do
41
+ before do
42
+ @engine=Distribution::BivariateNormal::Ruby_
43
+ end
44
+ it_should_behave_like "all pdf normal capables engines"
45
+ it_should_behave_like "all cdf normal capables engines"
46
+ it "Ganz method should return similar method to Hull one" do
47
+ [-3,-2,-1,0,1,1.5].each {|x|
48
+ @engine.cdf_hull(x,x,0.5).should be_within(0.001).of(@engine.cdf_genz(x,x,0.5))
49
+
50
+ }
51
+ end
52
+
53
+ end
54
+ describe Distribution::Normal::GSL_ do
55
+ before do
56
+ @engine=Distribution::BivariateNormal::GSL_
57
+ end
58
+ it_should_behave_like "all pdf normal capables engines"
59
+
60
+ end
61
+
62
+
63
+ end
@@ -0,0 +1,89 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+ include ExampleWithGSL
3
+ describe Distribution::ChiSquare do
4
+
5
+ shared_examples_for "Chi-square engine(with pdf)" do
6
+ it_only_with_gsl "should return correct pdf" do
7
+ if @engine.respond_to? :pdf
8
+ 1.upto(10) do |k|
9
+ v=1+rand(5)
10
+ chi=GSL::Ran.chisq_pdf(v,k)
11
+ @engine.pdf(v,k).should be_within(10e-10).of(chi)
12
+ end
13
+ else
14
+ pending("No #{@engine}.pdf")
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ shared_examples_for "Chi-square engine" do
21
+
22
+ it_only_with_gsl "should return correct cdf" do
23
+ if @engine.respond_to? :cdf
24
+ 1.upto(10) do |k|
25
+ v=1+rand(5)
26
+ chi=GSL::Cdf::chisq_P(v,k)
27
+ @engine.cdf(v,k).should be_within(10e-10).of(chi)
28
+ end
29
+ else
30
+ pending("No #{@engine}.cdf")
31
+ end
32
+ end
33
+ it "should return correct p_value" do
34
+ if @engine.respond_to? :p_value
35
+ 1.upto(10) do |k|
36
+ v=1+rand(5)
37
+ pr=@engine.cdf(v,k)
38
+ @engine.p_value(pr,k).should be_within(10e-4).of(v)
39
+ end
40
+ else
41
+ pending("No #{@engine}.p_value")
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "singleton" do
47
+ before do
48
+ @engine=Distribution::ChiSquare
49
+ end
50
+ it_should_behave_like "Chi-square engine"
51
+ it_should_behave_like "Chi-square engine(with pdf)"
52
+ end
53
+
54
+ describe Distribution::ChiSquare::Ruby_ do
55
+ before do
56
+ @engine=Distribution::ChiSquare::Ruby_
57
+ end
58
+ it_should_behave_like "Chi-square engine"
59
+ it_should_behave_like "Chi-square engine(with pdf)"
60
+ end
61
+ if Distribution.has_gsl?
62
+ describe Distribution::ChiSquare::GSL_ do
63
+ before do
64
+ @engine=Distribution::ChiSquare::GSL_
65
+ end
66
+ it_should_behave_like "Chi-square engine"
67
+ it_should_behave_like "Chi-square engine(with pdf)"
68
+ end
69
+ end
70
+ if Distribution.has_statistics2?
71
+ describe Distribution::ChiSquare::Statistics2_ do
72
+ before do
73
+ @engine=Distribution::ChiSquare::Statistics2_
74
+ end
75
+ it_should_behave_like "Chi-square engine"
76
+ end
77
+ end
78
+
79
+ if Distribution.has_java?
80
+ describe Distribution::ChiSquare::Java_ do
81
+ before do
82
+ @engine=Distribution::ChiSquare::Java_
83
+ end
84
+ it_should_behave_like "Chi-square engine"
85
+ it_should_behave_like "Chi-square engine(with pdf)"
86
+ end
87
+ end
88
+
89
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+ describe Distribution do
3
+ it "should respond to has_gsl?" do
4
+ lambda {Distribution.has_gsl?}.should_not raise_exception
5
+ if Distribution.has_gsl?
6
+ defined?(GSL).should be_true
7
+ else
8
+ defined?(GSL).should be_false
9
+ end
10
+ end
11
+ it "should respond to has_statistics2?" do
12
+ lambda {Distribution.has_statistics2?}.should_not raise_exception
13
+ if Distribution.has_statistics2?
14
+ defined?(Statistics2).should be_true
15
+ else
16
+ defined?(Statistics2).should be_true
17
+ end
18
+ end
19
+ end
data/spec/f_spec.rb ADDED
@@ -0,0 +1,107 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+
3
+ include ExampleWithGSL
4
+
5
+ describe Distribution::F do
6
+ shared_examples_for "F engine(with rng)" do
7
+ it "should return correct rng" do
8
+ pending()
9
+ end
10
+ end
11
+
12
+ shared_examples_for "F engine(with pdf)" do
13
+ it_only_with_gsl "should return correct pdf" do
14
+ if @engine.respond_to? :pdf
15
+ [0.1,0.5,1,2,10,20,30].each{|f|
16
+ [2,5,10].each{|n2|
17
+ [2,5,10].each{|n1|
18
+ @engine.pdf(f,n1,n2).should be_within(1e-4).of(GSL::Ran.fdist_pdf(f,n1,n2))
19
+ }
20
+ }
21
+ }
22
+ else
23
+ pending("No #{@engine}.pdf")
24
+ end
25
+ end
26
+ end
27
+
28
+ shared_examples_for "F engine" do
29
+
30
+ it_only_with_gsl "should return correct cdf" do
31
+ if @engine.respond_to? :cdf
32
+ [0.1,0.5,1,2,10,20,30].each{|f|
33
+ [2,5,10].each{|n2|
34
+ [2,5,10].each{|n1|
35
+ @engine.cdf(f,n1,n2).should be_within(1e-4).of(GSL::Cdf.fdist_P(f,n1,n2))
36
+
37
+ }
38
+ }
39
+ }
40
+ else
41
+ pending("No #{@engine}.cdf")
42
+ end
43
+
44
+ end
45
+ it "should return correct p_value" do
46
+ if @engine.respond_to? :p_value
47
+
48
+ [0.1,0.5,1,2,10,20,30].each{|f|
49
+ [2,5,10].each{|n2|
50
+ [2,5,10].each{|n1|
51
+ area=@engine.cdf(f,n1,n2)
52
+ @engine.p_value(area,n1,n2).should be_within(1e-4).of(GSL::Cdf.fdist_Pinv(area,n1,n2))
53
+ }
54
+ }
55
+ }
56
+
57
+
58
+ else
59
+ pending("No #{@engine}.p_value")
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "singleton" do
65
+ before do
66
+ @engine=Distribution::F
67
+ end
68
+ it_should_behave_like "F engine"
69
+ it_should_behave_like "F engine(with pdf)"
70
+ end
71
+
72
+ describe Distribution::F::Ruby_ do
73
+ before do
74
+ @engine=Distribution::F::Ruby_
75
+ end
76
+ it_should_behave_like "F engine"
77
+ it_should_behave_like "F engine(with pdf)"
78
+ end
79
+ if Distribution.has_gsl?
80
+ describe Distribution::F::GSL_ do
81
+ before do
82
+ @engine=Distribution::F::GSL_
83
+ end
84
+ it_should_behave_like "F engine"
85
+ it_should_behave_like "F engine(with pdf)"
86
+ end
87
+ end
88
+ if Distribution.has_statistics2?
89
+ describe Distribution::F::Statistics2_ do
90
+ before do
91
+ @engine=Distribution::F::Statistics2_
92
+ end
93
+ it_should_behave_like "F engine"
94
+ end
95
+ end
96
+
97
+ if Distribution.has_java?
98
+ describe Distribution::F::Java_ do
99
+ before do
100
+ @engine=Distribution::F::Java_
101
+ end
102
+ it_should_behave_like "F engine"
103
+ it_should_behave_like "F engine(with pdf)"
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+
3
+ describe Distribution::Normal do
4
+ shared_examples_for "gaussian engine(with rng)" do
5
+ it "should return correct rng" do
6
+ samples=100
7
+ sum=0
8
+ ss=0
9
+ exp_mean=rand(10)-5
10
+ exp_sd=1
11
+ rng=@engine.rng(exp_mean,exp_sd)
12
+
13
+ samples.times do
14
+ v=rng.call
15
+ sum+=v
16
+ ss+=(v-exp_mean)**2
17
+ end
18
+
19
+
20
+ mean=sum.to_f/samples
21
+ sd=Math::sqrt(ss.to_f/samples)
22
+ mean.should be_within(0.5).of(exp_mean)
23
+ sd.should be_within(0.3).of(exp_sd)
24
+ end
25
+ end
26
+
27
+ shared_examples_for "gaussian engine(with pdf)" do
28
+ it "should return correct pdf" do
29
+ if @engine.respond_to? :pdf
30
+ 10.times do |i|
31
+ x=(i-5)/2.0
32
+ pdf=(1.0 / Distribution::SQ2PI)*Math::exp(-(x**2/2.0))
33
+ @engine.pdf(x).should be_within(1e-10).of(pdf)
34
+ end
35
+ else
36
+ pending("No #{@engine}.pdf")
37
+ end
38
+ end
39
+ end
40
+
41
+ shared_examples_for "gaussian engine" do
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)
45
+ end
46
+ it "should return correct p_value" do
47
+ if @engine.respond_to? :p_value
48
+ @engine.p_value(0.5).should be_within(1e-3).of(0)
49
+ 10.times do |i|
50
+ x=(i-5) / 2.0
51
+ cdf=@engine.cdf(x)
52
+ @engine.p_value(cdf).should be_within(1e-6).of(x)
53
+ end
54
+ else
55
+ pending("No #{@engine}.p_value")
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "singleton" do
61
+ before do
62
+ @engine=Distribution::Normal
63
+ end
64
+ it_should_behave_like "gaussian engine"
65
+ it_should_behave_like "gaussian engine(with rng)"
66
+ it_should_behave_like "gaussian engine(with pdf)"
67
+ end
68
+
69
+ describe Distribution::Normal::Ruby_ do
70
+ before do
71
+ @engine=Distribution::Normal::Ruby_
72
+ end
73
+ it_should_behave_like "gaussian engine"
74
+ it_should_behave_like "gaussian engine(with rng)"
75
+ it_should_behave_like "gaussian engine(with pdf)"
76
+ end
77
+ if Distribution.has_gsl?
78
+ describe Distribution::Normal::GSL_ do
79
+ before do
80
+ @engine=Distribution::Normal::GSL_
81
+ end
82
+ it_should_behave_like "gaussian engine"
83
+ it_should_behave_like "gaussian engine(with rng)"
84
+ it_should_behave_like "gaussian engine(with pdf)"
85
+ end
86
+ end
87
+ if Distribution.has_statistics2?
88
+ describe Distribution::Normal::Statistics2_ do
89
+ before do
90
+ @engine=Distribution::Normal::Statistics2_
91
+ end
92
+ it_should_behave_like "gaussian engine"
93
+ end
94
+ end
95
+
96
+ if Distribution.has_java?
97
+ describe Distribution::Normal::Java_ do
98
+ before do
99
+ @engine=Distribution::Normal::Java_
100
+ end
101
+ it_should_behave_like "all gaussian engines"
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+ describe Distribution::Function do
3
+ it "should correct factorial" do
4
+ Distribution::Function.factorial(5).should eq(5*4*3*2*1)
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+ describe Distribution::Shorthand do
3
+ include Distribution::Shorthand
4
+ it "should have normal methods" do
5
+ Distribution::Shorthand.instance_methods.map{|v| v.to_sym}.should include(:norm_pdf, :norm_cdf, :norm_rng,:norm_p_value)
6
+ end
7
+ it "returns same values as long form" do
8
+ x=rand()
9
+ norm_cdf(x).should eql(Distribution::Normal.cdf(x))
10
+ norm_pdf(x).should eql(Distribution::Normal.pdf(x))
11
+ norm_p_value(x).should eql(Distribution::Normal.p_value(x))
12
+
13
+ end
14
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ -f s
3
+ -b
@@ -0,0 +1,23 @@
1
+ $:.unshift(File.dirname(__FILE__)+"/../lib")
2
+ begin
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ add_filter "/spec/"
6
+ add_group "Libraries", "lib"
7
+ end
8
+ rescue LoadError
9
+ end
10
+ require 'rspec'
11
+ require 'distribution'
12
+
13
+ module ExampleWithGSL
14
+ def it_only_with_gsl(name,&block)
15
+ it(name) do
16
+ if Distribution.has_gsl?
17
+ instance_eval(&block)
18
+ else
19
+ pending("Requires GSL")
20
+ end
21
+ end
22
+ end
23
+ end
data/spec/t_spec.rb ADDED
@@ -0,0 +1,98 @@
1
+ require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
+
3
+ include ExampleWithGSL
4
+
5
+ describe Distribution::T do
6
+ shared_examples_for "T engine(with rng)" do
7
+ it "should return correct rng" do
8
+ pending()
9
+ end
10
+ end
11
+
12
+ shared_examples_for "T engine(with pdf)" do
13
+ it_only_with_gsl "should return correct pdf" do
14
+ if @engine.respond_to? :pdf
15
+ [-2,0.1,0.5,1,2].each{|t|
16
+ [2,5,10].each{|n|
17
+ @engine.pdf(t,n).should be_within(1e-6).of(GSL::Ran.tdist_pdf(t,n))
18
+
19
+ }
20
+ }
21
+ else
22
+ pending("No #{@engine}.pdf")
23
+ end
24
+ end
25
+ end
26
+
27
+ shared_examples_for "T engine" do
28
+
29
+ it_only_with_gsl "should return correct cdf" do
30
+ if @engine.respond_to? :cdf
31
+ [-2,0.1,0.5,1,2].each{|t|
32
+ [2,5,10].each{|n|
33
+ @engine.cdf(t,n).should be_within(1e-4).of(GSL::Cdf.tdist_P(t,n))
34
+ }
35
+ }
36
+ else
37
+ pending("No #{@engine}.cdf")
38
+ end
39
+
40
+ end
41
+ it "should return correct p_value" do
42
+ if @engine.respond_to? :p_value
43
+ [-2,0.1,0.5,1,2].each{|t|
44
+ [2,5,10].each{|n|
45
+ area=Distribution::T.cdf(t,n)
46
+ @engine.p_value(area,n).should be_within(1e-4).of(GSL::Cdf.tdist_Pinv(area,n))
47
+ }
48
+ }
49
+ else
50
+ pending("No #{@engine}.p_value")
51
+ end
52
+ end
53
+ end
54
+
55
+ describe "singleton" do
56
+ before do
57
+ @engine=Distribution::T
58
+ end
59
+ it_should_behave_like "T engine"
60
+ it_should_behave_like "T engine(with pdf)"
61
+ end
62
+
63
+ describe Distribution::T::Ruby_ do
64
+ before do
65
+ @engine=Distribution::T::Ruby_
66
+ end
67
+ it_should_behave_like "T engine"
68
+ it_should_behave_like "T engine(with pdf)"
69
+ end
70
+ if Distribution.has_gsl?
71
+ describe Distribution::T::GSL_ do
72
+ before do
73
+ @engine=Distribution::T::GSL_
74
+ end
75
+ it_should_behave_like "T engine"
76
+ it_should_behave_like "T engine(with pdf)"
77
+ end
78
+ end
79
+ if Distribution.has_statistics2?
80
+ describe Distribution::T::Statistics2_ do
81
+ before do
82
+ @engine=Distribution::T::Statistics2_
83
+ end
84
+ it_should_behave_like "T engine"
85
+ end
86
+ end
87
+
88
+ if Distribution.has_java?
89
+ describe Distribution::T::Java_ do
90
+ before do
91
+ @engine=Distribution::T::Java_
92
+ end
93
+ it_should_behave_like "T engine"
94
+ it_should_behave_like "T engine(with pdf)"
95
+ end
96
+ end
97
+
98
+ end