distribution 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.travis.yml +13 -0
  4. data/.yardopts +5 -0
  5. data/Gemfile +5 -0
  6. data/History.txt +24 -8
  7. data/LICENCE.md +26 -0
  8. data/README.md +155 -0
  9. data/Rakefile +15 -19
  10. data/benchmark/binomial_coefficient.rb +19 -23
  11. data/benchmark/binomial_coefficient/experiment.rb +33 -36
  12. data/benchmark/factorial_hash.rb +7 -8
  13. data/benchmark/factorial_method.rb +4 -6
  14. data/benchmark/odd.rb +6 -7
  15. data/benchmark/power.rb +11 -11
  16. data/bin/distribution +26 -26
  17. data/data/template/spec.erb +7 -6
  18. data/distribution.gemspec +25 -0
  19. data/lib/distribution.rb +79 -124
  20. data/lib/distribution/beta.rb +6 -8
  21. data/lib/distribution/beta/gsl.rb +14 -9
  22. data/lib/distribution/beta/java.rb +1 -1
  23. data/lib/distribution/beta/ruby.rb +41 -7
  24. data/lib/distribution/binomial.rb +10 -11
  25. data/lib/distribution/binomial/gsl.rb +6 -5
  26. data/lib/distribution/binomial/java.rb +1 -1
  27. data/lib/distribution/binomial/ruby.rb +22 -15
  28. data/lib/distribution/bivariatenormal.rb +4 -5
  29. data/lib/distribution/bivariatenormal/gsl.rb +2 -2
  30. data/lib/distribution/bivariatenormal/java.rb +1 -1
  31. data/lib/distribution/bivariatenormal/ruby.rb +245 -254
  32. data/lib/distribution/chisquare.rb +8 -10
  33. data/lib/distribution/chisquare/gsl.rb +24 -19
  34. data/lib/distribution/chisquare/java.rb +1 -1
  35. data/lib/distribution/chisquare/ruby.rb +60 -55
  36. data/lib/distribution/chisquare/statistics2.rb +16 -13
  37. data/lib/distribution/distributable.rb +40 -0
  38. data/lib/distribution/exponential.rb +4 -5
  39. data/lib/distribution/exponential/gsl.rb +13 -9
  40. data/lib/distribution/exponential/ruby.rb +17 -11
  41. data/lib/distribution/f.rb +10 -11
  42. data/lib/distribution/f/gsl.rb +26 -22
  43. data/lib/distribution/f/java.rb +1 -1
  44. data/lib/distribution/f/ruby.rb +104 -105
  45. data/lib/distribution/f/statistics2.rb +22 -19
  46. data/lib/distribution/gamma.rb +5 -7
  47. data/lib/distribution/gamma/gsl.rb +13 -9
  48. data/lib/distribution/gamma/java.rb +1 -1
  49. data/lib/distribution/gamma/ruby.rb +5 -11
  50. data/lib/distribution/hypergeometric.rb +5 -8
  51. data/lib/distribution/hypergeometric/gsl.rb +5 -6
  52. data/lib/distribution/hypergeometric/java.rb +1 -1
  53. data/lib/distribution/hypergeometric/ruby.rb +34 -35
  54. data/lib/distribution/logistic.rb +6 -9
  55. data/lib/distribution/logistic/ruby.rb +14 -9
  56. data/lib/distribution/lognormal.rb +37 -0
  57. data/lib/distribution/lognormal/gsl.rb +21 -0
  58. data/lib/distribution/lognormal/ruby.rb +16 -0
  59. data/lib/distribution/math_extension.rb +187 -231
  60. data/lib/distribution/math_extension/chebyshev_series.rb +281 -272
  61. data/lib/distribution/math_extension/erfc.rb +28 -31
  62. data/lib/distribution/math_extension/exponential_integral.rb +17 -17
  63. data/lib/distribution/math_extension/gammastar.rb +19 -20
  64. data/lib/distribution/math_extension/gsl_utilities.rb +12 -12
  65. data/lib/distribution/math_extension/incomplete_beta.rb +52 -61
  66. data/lib/distribution/math_extension/incomplete_gamma.rb +166 -168
  67. data/lib/distribution/math_extension/log_utilities.rb +20 -22
  68. data/lib/distribution/normal.rb +11 -13
  69. data/lib/distribution/normal/gsl.rb +13 -10
  70. data/lib/distribution/normal/java.rb +30 -1
  71. data/lib/distribution/normal/ruby.rb +69 -59
  72. data/lib/distribution/normal/statistics2.rb +5 -2
  73. data/lib/distribution/normalmultivariate.rb +64 -64
  74. data/lib/distribution/poisson.rb +12 -14
  75. data/lib/distribution/poisson/gsl.rb +7 -7
  76. data/lib/distribution/poisson/java.rb +26 -0
  77. data/lib/distribution/poisson/ruby.rb +38 -9
  78. data/lib/distribution/shorthand.rb +17 -0
  79. data/lib/distribution/t.rb +16 -16
  80. data/lib/distribution/t/gsl.rb +27 -24
  81. data/lib/distribution/t/java.rb +1 -1
  82. data/lib/distribution/t/ruby.rb +99 -100
  83. data/lib/distribution/t/statistics2.rb +19 -19
  84. data/lib/distribution/uniform.rb +26 -0
  85. data/lib/distribution/uniform/gsl.rb +36 -0
  86. data/lib/distribution/uniform/ruby.rb +91 -0
  87. data/lib/distribution/version.rb +3 -0
  88. data/lib/distribution/weibull.rb +10 -0
  89. data/lib/distribution/weibull/gsl.rb +21 -0
  90. data/lib/distribution/weibull/ruby.rb +34 -0
  91. data/spec/beta_spec.rb +48 -50
  92. data/spec/binomial_spec.rb +80 -84
  93. data/spec/bivariatenormal_spec.rb +28 -35
  94. data/spec/chisquare_spec.rb +49 -52
  95. data/spec/distribution_spec.rb +11 -11
  96. data/spec/exponential_spec.rb +48 -39
  97. data/spec/f_spec.rb +73 -71
  98. data/spec/gamma_spec.rb +50 -53
  99. data/spec/hypergeometric_spec.rb +63 -69
  100. data/spec/logistic_spec.rb +31 -37
  101. data/spec/lognormal_spec.rb +54 -0
  102. data/spec/math_extension_spec.rb +192 -209
  103. data/spec/normal_spec.rb +80 -73
  104. data/spec/poisson_spec.rb +78 -36
  105. data/spec/shorthand_spec.rb +19 -22
  106. data/spec/spec_helper.rb +31 -6
  107. data/spec/t_spec.rb +63 -77
  108. data/spec/uniform_spec.rb +154 -0
  109. data/spec/weibull_spec.rb +17 -0
  110. data/vendor/java/commons-math-2.2.jar +0 -0
  111. metadata +91 -111
  112. data.tar.gz.sig +0 -0
  113. data/.autotest +0 -23
  114. data/.gemtest +0 -0
  115. data/Manifest.txt +0 -95
  116. data/README.txt +0 -100
  117. metadata.gz.sig +0 -0
@@ -1,109 +1,116 @@
1
- require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
-
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+
3
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
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
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)
18
+
19
+ mean = sum.to_f / samples
20
+ sd = Math.sqrt(ss.to_f / samples)
21
+ expect(mean).to be_within(0.5).of(exp_mean)
22
+ expect(sd).to be_within(0.3).of(exp_sd)
23
+ end
24
24
  end
25
- end
26
25
 
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")
26
+ shared_examples_for 'gaussian engine(with rng)' do
27
+ it 'rng with a specified seed should be reproducible' do
28
+ seed = rand(10)
29
+ rng1 = @engine.rng(0, 1, seed)
30
+ rng2 = @engine.rng(0, 1, seed)
31
+ expect((rng1.call)).to eq(rng2.call)
37
32
  end
38
33
  end
39
- end
40
34
 
41
- shared_examples_for "gaussian engine" do
42
- it "should return correct cdf" do
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")
35
+ shared_examples_for 'gaussian engine(with pdf)' do
36
+ it 'should return correct pdf' do
37
+ if @engine.respond_to? :pdf
38
+ 10.times do |i|
39
+ x = (i - 5) / 2.0
40
+ pdf = (1.0 / Distribution::SQ2PI) * Math.exp(-(x**2 / 2.0))
41
+ expect(@engine.pdf(x)).to be_within(1e-10).of(pdf)
42
+ end
43
+ else
44
+ pending("No #{@engine}.pdf")
45
+ end
48
46
  end
49
47
  end
50
- it "should return correct p_value" do
51
- if @engine.respond_to? :p_value
52
- @engine.p_value(0.5).should be_within(1e-3).of(0)
53
- 10.times do |i|
54
- x=(i-5) / 2.0
55
- cdf=@engine.cdf(x)
56
- @engine.p_value(cdf).should be_within(1e-6).of(x)
48
+
49
+ shared_examples_for 'gaussian engine' do
50
+ it 'should return correct cdf' do
51
+ if @engine.respond_to? :cdf
52
+ expect(@engine.cdf(1.96)).to be_within(1e-10).of(0.97500210485178)
53
+ expect(@engine.cdf(0)).to be_within(1e-10).of(0.5)
54
+ else
55
+ pending("No #{@engine}.cdf")
56
+ end
57
+ end
58
+ it 'should return correct p_value' do
59
+ if @engine.respond_to? :p_value
60
+ expect(@engine.p_value(0.5)).to be_within(1e-3).of(0)
61
+ 10.times do |i|
62
+ x = (i - 5) / 2.0
63
+ cdf = @engine.cdf(x)
64
+ expect(@engine.p_value(cdf)).to be_within(1e-6).of(x)
65
+ end
66
+ else
67
+ pending("No #{@engine}.p_value")
57
68
  end
58
- else
59
- pending("No #{@engine}.p_value")
60
69
  end
61
70
  end
62
- end
63
71
 
64
- describe "singleton" do
72
+ describe 'singleton' do
65
73
  before do
66
- @engine=Distribution::Normal
74
+ @engine = Distribution::Normal
67
75
  end
68
- it_should_behave_like "gaussian engine"
69
- it_should_behave_like "gaussian engine(with rng)"
70
- it_should_behave_like "gaussian engine(with pdf)"
76
+ it_should_behave_like 'gaussian engine'
77
+ it_should_behave_like 'gaussian engine(with rng)'
78
+ it_should_behave_like 'gaussian engine(with pdf)'
71
79
  end
72
-
80
+
73
81
  describe Distribution::Normal::Ruby_ do
74
82
  before do
75
- @engine=Distribution::Normal::Ruby_
83
+ @engine = Distribution::Normal::Ruby_
76
84
  end
77
- it_should_behave_like "gaussian engine"
78
- it_should_behave_like "gaussian engine(with rng)"
79
- it_should_behave_like "gaussian engine(with pdf)"
85
+ it_should_behave_like 'gaussian engine'
86
+ it_should_behave_like 'gaussian engine(with rng)'
87
+ it_should_behave_like 'gaussian engine(with pdf)'
80
88
  end
81
89
  if Distribution.has_gsl?
82
90
  describe Distribution::Normal::GSL_ do
83
91
  before do
84
- @engine=Distribution::Normal::GSL_
92
+ @engine = Distribution::Normal::GSL_
85
93
  end
86
- it_should_behave_like "gaussian engine"
87
- it_should_behave_like "gaussian engine(with rng)"
88
- it_should_behave_like "gaussian engine(with pdf)"
94
+ it_should_behave_like 'gaussian engine'
95
+ it_should_behave_like 'gaussian engine(with rng)'
96
+ it_should_behave_like 'gaussian engine(with pdf)'
89
97
  end
90
- end
98
+ end
91
99
  if Distribution.has_statistics2?
92
100
  describe Distribution::Normal::Statistics2_ do
93
101
  before do
94
- @engine=Distribution::Normal::Statistics2_
102
+ @engine = Distribution::Normal::Statistics2_
95
103
  end
96
- it_should_behave_like "gaussian engine"
97
- end
104
+ it_should_behave_like 'gaussian engine'
105
+ end
98
106
  end
99
-
107
+
100
108
  if Distribution.has_java?
101
109
  describe Distribution::Normal::Java_ do
102
110
  before do
103
- @engine=Distribution::Normal::Java_
111
+ @engine = Distribution::Normal::Java_
104
112
  end
105
- it_should_behave_like "gaussian engine"
106
- end
113
+ it_should_behave_like 'gaussian engine'
114
+ end
107
115
  end
108
-
109
116
  end
@@ -1,72 +1,114 @@
1
- require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
2
- include ExampleWithGSL
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+ include ExampleWithGSL
3
3
  describe Distribution::Poisson do
4
-
5
- shared_examples_for "poisson engine" do
6
- it "should return correct pdf" do
4
+ shared_examples_for 'poisson engine(with rng)' do
5
+ it 'should return correct rng' do
6
+ # The expected rng output when 1 is set as the seed value
7
+ exp_means = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9]
8
+
9
+ lambda_val = rand(10) + 1
10
+ exp_mean = exp_means[lambda_val - 1]
11
+ seed = 1
12
+
13
+ rng = @engine.rng(lambda_val, seed)
14
+
15
+ samples = 100
16
+ sum = 0
17
+ samples.times do
18
+ v = rng.call
19
+ sum += v
20
+ end
21
+ mean = sum / samples
22
+ expect(mean.to_i).to be_within(1e-10).of(exp_mean)
23
+ end
24
+ end
25
+
26
+ shared_examples_for 'poisson engine(with rng)' do
27
+ it 'rng with a specified seed should be reproducible' do
28
+ seed = 1
29
+ rng1 = @engine.rng(3, seed)
30
+ rng2 = @engine.rng(3, seed)
31
+ expect((rng1.call)).to eq(rng2.call)
32
+ end
33
+ end
34
+
35
+
36
+ shared_examples_for 'poisson engine' do
37
+ it 'should return correct pdf' do
7
38
  if @engine.respond_to? :pdf
8
- [0.5,1,1.5].each {|l|
39
+ [0.5, 1, 1.5].each {|l|
9
40
  1.upto(5) {|k|
10
- @engine.pdf(k,l).should be_within(1e-10).of((l**k*Math.exp(-l)).quo(Math.factorial(k)))
41
+ expect(@engine.pdf(k, l)).to be_within(1e-10).of((l**k * Math.exp(-l)).quo(Math.factorial(k)))
11
42
  }
12
43
  }
13
44
  else
14
45
  pending("No #{@engine}.pdf")
15
46
  end
16
47
  end
17
-
18
- it_only_with_gsl "should return correct cdf" do
48
+
49
+ it_only_with_gsl 'should return correct cdf' do
19
50
  if @engine.respond_to? :cdf
20
- [0.5,1,1.5,4,10].each {|l|
51
+ [0.5, 1, 1.5, 4, 10].each {|l|
21
52
  1.upto(5) {|k|
22
- @engine.cdf(k,l).should be_within(1e-10).of(GSL::Cdf.poisson_P(k,l))
53
+ expect(@engine.cdf(k, l)).to be_within(1e-10).of(GSL::Cdf.poisson_P(k, l))
23
54
  }
24
55
  }
25
-
56
+
26
57
  else
27
58
  pending("No #{@engine}.cdf")
28
59
  end
29
60
  end
30
-
31
-
32
- it "should return correct p_value" do
33
- pending("No exact p_value")
61
+
62
+ it 'should return correct p_value' do
63
+ pending('No exact p_value')
34
64
  if @engine.respond_to? :p_value
35
- [0.1,1,5,10].each {|l|
65
+ [0.1, 1, 5, 10].each {|l|
36
66
  1.upto(20) {|k|
37
- pr=@engine.cdf(k,l)
38
- @engine.p_value(pr,l).should eq(k)
67
+ pr = @engine.cdf(k, l)
68
+ expect(@engine.p_value(pr, l)).to eq(k)
39
69
  }
40
70
  }
41
71
  else
42
- pending("No #{@engine}.p_value")
72
+ skip("No #{@engine}.p_value")
43
73
  end
44
74
  end
45
75
  end
46
-
47
76
 
48
- describe "singleton" do
77
+ describe 'singleton' do
49
78
  before do
50
- @engine=Distribution::Poisson
79
+ @engine = Distribution::Poisson
51
80
  end
52
- it_should_behave_like "poisson engine"
81
+ it_should_behave_like 'poisson engine'
53
82
  end
54
-
83
+
55
84
  describe Distribution::Poisson::Ruby_ do
56
85
  before do
57
- @engine=Distribution::Poisson::Ruby_
86
+ @engine = Distribution::Poisson::Ruby_
58
87
  end
59
- it_should_behave_like "poisson engine"
60
-
88
+ it_should_behave_like 'poisson engine'
61
89
  end
62
-
63
- describe Distribution::Poisson::GSL_ do
64
- before do
65
- @engine=Distribution::Poisson::GSL_
90
+ if Distribution.has_gsl?
91
+ describe Distribution::Poisson::GSL_ do
92
+ before do
93
+ @engine = Distribution::Poisson::GSL_
94
+ end
95
+ it_should_behave_like 'poisson engine'
96
+ end
97
+ end
98
+ if Distribution.has_java?
99
+ describe Distribution::Poisson::Java_ do
100
+ before do
101
+ @engine = Distribution::Poisson::Java_
102
+ end
103
+ it_should_behave_like 'poisson engine'
104
+
105
+ it 'should return correct cdf' do
106
+ [0.5, 1, 1.5, 4, 10].each {|l|
107
+ 1.upto(5) {|k|
108
+ expect(@engine.cdf(k, l)).to be_within(1e-10).of(Distribution::Poisson::Ruby_.cdf(k, l))
109
+ }
110
+ }
111
+ end
66
112
  end
67
- it_should_behave_like "poisson engine"
68
-
69
113
  end
70
-
71
-
72
114
  end
@@ -1,34 +1,31 @@
1
- require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
2
  describe Distribution::Shorthand do
3
3
  include Distribution::Shorthand
4
- it "should have basic methods for all distributions" do
5
- [:Normal,:ChiSquare, :F, :Hypergeometric, :Binomial, :T].each do |d|
6
- klass=Distribution.const_get(d)
7
- shortname=klass::SHORTHAND
8
- methods=[:pdf, :cdf, :p_value].map {|m| "#{shortname}_#{m}".to_sym}
9
- methods.each do |m|
10
- Distribution::Shorthand.instance_methods.map {|v| v.to_sym}.should include(m)
4
+ it 'should have basic methods for all distributions' do
5
+ [:Normal, :ChiSquare, :F, :Hypergeometric, :Binomial, :T].each do |d|
6
+ klass = Distribution.const_get(d)
7
+ shortname = klass::SHORTHAND
8
+ methods = [:pdf, :cdf, :p_value].map { |m| "#{shortname}_#{m}".to_sym }
9
+ methods.each do |m|
10
+ expect(Distribution::Shorthand.instance_methods.map(&:to_sym)).to include(m)
11
11
  end
12
12
  end
13
-
14
13
  end
15
- it "should have exact methods discrete distributions" do
14
+ it 'should have exact methods discrete distributions' do
16
15
  [: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)
16
+ klass = Distribution.const_get(d)
17
+ shortname = klass::SHORTHAND
18
+ methods = [:epdf, :ecdf].map { |m| "#{shortname}_#{m}".to_sym }
19
+ methods.each do |m|
20
+ expect(Distribution::Shorthand.instance_methods.map(&:to_sym)).to include(m)
22
21
  end
23
22
  end
24
-
25
23
  end
26
24
 
27
-
28
- it "returns same values as long form" do
29
- x=rand()
30
- norm_cdf(x).should eql(Distribution::Normal.cdf(x))
31
- norm_pdf(x).should eql(Distribution::Normal.pdf(x))
32
- norm_p_value(x).should eql(Distribution::Normal.p_value(x))
25
+ it 'returns same values as long form' do
26
+ x = rand
27
+ expect(norm_cdf(x)).to eql(Distribution::Normal.cdf(x))
28
+ expect(norm_pdf(x)).to eql(Distribution::Normal.pdf(x))
29
+ expect(norm_p_value(x)).to eql(Distribution::Normal.p_value(x))
33
30
  end
34
31
  end
@@ -1,22 +1,47 @@
1
- $:.unshift(File.dirname(__FILE__)+"/../lib")
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
2
  begin
3
3
  require 'simplecov'
4
4
  SimpleCov.start do
5
- add_filter "/spec/"
6
- add_group "Libraries", "lib"
5
+ add_filter '/spec/'
6
+ add_group 'Libraries', 'lib'
7
7
  end
8
8
  rescue LoadError
9
9
  end
10
10
  require 'rspec'
11
11
  require 'distribution'
12
12
 
13
+ RSpec.configure do |config|
14
+ config.expect_with :rspec do |c|
15
+ c.syntax = [:should, :expect]
16
+ end
17
+
18
+ # Use color in STDOUT
19
+ config.color = true
20
+
21
+ # Use color not only in STDOUT but also in pagers and files
22
+ config.tty = true
23
+
24
+ # Use the specified formatter
25
+ config.formatter = :documentation # :progress, :html, :textmate
26
+ end
27
+
13
28
  module ExampleWithGSL
14
- def it_only_with_gsl(name,&block)
15
- it(name) do
29
+ def it_only_with_gsl(name, opts = {}, &block)
30
+ it(name, opts) do
16
31
  if Distribution.has_gsl?
17
32
  instance_eval(&block)
18
33
  else
19
- pending("Requires GSL")
34
+ skip('Requires GSL')
35
+ end
36
+ end
37
+ end
38
+
39
+ def it_only_with_java(name, &block)
40
+ it(name) do
41
+ if Distribution.has_java?
42
+ instance_eval(&block)
43
+ else
44
+ skip('Requires Java')
20
45
  end
21
46
  end
22
47
  end
@@ -1,111 +1,97 @@
1
- require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
2
 
3
3
  include ExampleWithGSL
4
4
 
5
5
  describe Distribution::T do
6
- shared_examples_for "T engine(with rng)" do
7
- it "should return correct rng" do
8
- pending()
6
+ shared_examples_for 'T engine(with rng)' do
7
+ it 'should return correct rng' do
8
+ pending
9
+ end
9
10
  end
10
- end
11
11
 
12
- shared_examples_for "T engine(cdf with fractional df)" do
13
- it "should return correct cdf with fractional df" do
14
- @engine.cdf(1,2.5).should be_within(1e-6).of(0.7979695)
15
- @engine.cdf(2,3.5).should be_within(1e-6).of(0.9369307)
16
- @engine.cdf(3,4.5).should be_within(1e-6).of(0.9828096)
17
-
12
+ shared_examples_for 'T engine(cdf with fractional df)' do
13
+ it 'should return correct cdf with fractional df' do
14
+ expect(@engine.cdf(1, 2.5)).to be_within(1e-6).of(0.7979695)
15
+ expect(@engine.cdf(2, 3.5)).to be_within(1e-6).of(0.9369307)
16
+ expect(@engine.cdf(3, 4.5)).to be_within(1e-6).of(0.9828096)
17
+ end
18
18
  end
19
- end
20
-
21
- shared_examples_for "T engine" do
22
- it_only_with_gsl "should return correct pdf" do
23
- if @engine.respond_to? :pdf
24
- [-2,0.1,0.5,1,2].each{|t|
25
- [2,5,10].each{|n|
26
- @engine.pdf(t,n).should be_within(1e-6).of(GSL::Ran.tdist_pdf(t,n))
27
- @engine.pdf(t,n.to_f).should be_within(1e-6).of(@engine.pdf(t,n))
28
19
 
20
+ shared_examples_for 'T engine' do
21
+ it_only_with_gsl 'should return correct pdf' do
22
+ if @engine.respond_to? :pdf
23
+ [-2, 0.1, 0.5, 1, 2].each{|t|
24
+ [2, 5, 10].each{|n|
25
+ expect(@engine.pdf(t, n)).to be_within(1e-6).of(GSL::Ran.tdist_pdf(t, n))
26
+ expect(@engine.pdf(t, n.to_f)).to be_within(1e-6).of(@engine.pdf(t, n))
27
+ }
29
28
  }
30
- }
31
- else
32
- pending("No #{@engine}.pdf")
29
+ else
30
+ pending("No #{@engine}.pdf")
31
+ end
33
32
  end
34
- end
35
- it_only_with_gsl "should return correct cdf" do
36
- if @engine.respond_to? :cdf
37
- # Testing with R values
38
- @engine.cdf(1,2).should be_within(1e-7).of(0.7886751)
39
- @engine.cdf(1,2.0).should be_within(1e-7).of(0.7886751)
40
- @engine.cdf(1,3.0).should be_within(1e-7).of(0.8044989)
41
-
42
- [-2,0.1,0.5,1,2].each{|t|
43
- [2,5,10].each{|n|
44
- @engine.cdf(t,n).should be_within(1e-4).of(GSL::Cdf.tdist_P(t,n))
45
- @engine.cdf(t,n).should be_within(1e-4).of(@engine.cdf(t,n.to_f))
46
-
33
+ it_only_with_gsl 'should return correct cdf' do
34
+ if @engine.respond_to? :cdf
35
+ # Testing with R values
36
+ expect(@engine.cdf(1, 2)).to be_within(1e-7).of(0.7886751)
37
+ expect(@engine.cdf(1, 2.0)).to be_within(1e-7).of(0.7886751)
38
+ expect(@engine.cdf(1, 3.0)).to be_within(1e-7).of(0.8044989)
39
+
40
+ [-2, 0.1, 0.5, 1, 2].each{|t|
41
+ [2, 5, 10].each{|n|
42
+ expect(@engine.cdf(t, n)).to be_within(1e-4).of(GSL::Cdf.tdist_P(t, n))
43
+ expect(@engine.cdf(t, n)).to be_within(1e-4).of(@engine.cdf(t, n.to_f))
44
+ }
47
45
  }
48
- }
49
- else
50
- pending("No #{@engine}.cdf")
46
+ else
47
+ pending("No #{@engine}.cdf")
48
+ end
51
49
  end
52
-
53
- end
54
- it_only_with_gsl "should return correct p_value" do
55
- if @engine.respond_to? :p_value
56
- [-2,0.1,0.5,1,2].each{|t|
57
- [2,5,10].each{|n|
58
- area=Distribution::T.cdf(t,n)
59
- @engine.p_value(area,n).should be_within(1e-4).of(GSL::Cdf.tdist_Pinv(area,n))
60
- }
61
- }
62
- else
63
- pending("No #{@engine}.p_value")
50
+ it_only_with_gsl 'should return correct p_value' do
51
+ if @engine.respond_to? :p_value
52
+ [-2, 0.1, 0.5, 1, 2].each{|t|
53
+ [2, 5, 10].each{|n|
54
+ area = Distribution::T.cdf(t, n)
55
+ expect(@engine.p_value(area, n)).to be_within(1e-4).of(GSL::Cdf.tdist_Pinv(area, n))
56
+ }
57
+ }
58
+ else
59
+ pending("No #{@engine}.p_value")
60
+ end
64
61
  end
65
62
  end
66
- end
67
63
 
68
- describe "singleton" do
64
+ describe 'singleton' do
69
65
  before do
70
- @engine=Distribution::T
66
+ @engine = Distribution::T
71
67
  end
72
- it_should_behave_like "T engine"
68
+ it_should_behave_like 'T engine'
73
69
  end
74
-
70
+
75
71
  describe Distribution::T::Ruby_ do
76
72
  before do
77
- @engine=Distribution::T::Ruby_
73
+ @engine = Distribution::T::Ruby_
78
74
  end
79
- it_should_behave_like "T engine"
80
- it_should_behave_like "T engine(cdf with fractional df)"
81
-
75
+ it_should_behave_like 'T engine'
76
+ it_should_behave_like 'T engine(cdf with fractional df)'
82
77
  end
78
+
83
79
  if Distribution.has_gsl?
84
80
  describe Distribution::T::GSL_ do
85
81
  before do
86
- @engine=Distribution::T::GSL_
82
+ @engine = Distribution::T::GSL_
87
83
  end
88
- it_should_behave_like "T engine"
89
- it_should_behave_like "T engine(cdf with fractional df)"
84
+ it_should_behave_like 'T engine'
85
+ it_should_behave_like 'T engine(cdf with fractional df)'
90
86
  end
91
87
  end
92
- =begin
93
- if Distribution.has_statistics2?
94
- describe Distribution::T::Statistics2_ do
95
- before do
96
- @engine=Distribution::T::Statistics2_
97
- end
98
- it_should_behave_like "T engine"
99
- end
100
- end
101
- =end
88
+
102
89
  if Distribution.has_java?
103
90
  describe Distribution::T::Java_ do
104
91
  before do
105
- @engine=Distribution::T::Java_
92
+ @engine = Distribution::T::Java_
106
93
  end
107
- it_should_behave_like "T engine"
108
- end
94
+ it_should_behave_like 'T engine'
95
+ end
109
96
  end
110
-
111
97
  end