distribution 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +4 -6
  3. data/.yardopts +5 -0
  4. data/History.txt +3 -0
  5. data/README.md +87 -44
  6. data/benchmark/binomial_coefficient.rb +19 -23
  7. data/benchmark/binomial_coefficient/experiment.rb +33 -36
  8. data/benchmark/factorial_hash.rb +7 -8
  9. data/benchmark/factorial_method.rb +4 -6
  10. data/benchmark/odd.rb +6 -7
  11. data/benchmark/power.rb +11 -11
  12. data/bin/distribution +26 -26
  13. data/distribution.gemspec +3 -4
  14. data/lib/distribution.rb +55 -96
  15. data/lib/distribution/beta/gsl.rb +10 -5
  16. data/lib/distribution/beta/ruby.rb +3 -1
  17. data/lib/distribution/binomial/ruby.rb +5 -2
  18. data/lib/distribution/bivariatenormal.rb +4 -5
  19. data/lib/distribution/bivariatenormal/gsl.rb +2 -2
  20. data/lib/distribution/bivariatenormal/java.rb +1 -1
  21. data/lib/distribution/bivariatenormal/ruby.rb +245 -254
  22. data/lib/distribution/chisquare.rb +8 -10
  23. data/lib/distribution/chisquare/gsl.rb +24 -19
  24. data/lib/distribution/chisquare/java.rb +1 -1
  25. data/lib/distribution/chisquare/ruby.rb +25 -25
  26. data/lib/distribution/chisquare/statistics2.rb +16 -13
  27. data/lib/distribution/distributable.rb +40 -0
  28. data/lib/distribution/exponential.rb +4 -5
  29. data/lib/distribution/exponential/gsl.rb +13 -9
  30. data/lib/distribution/exponential/ruby.rb +14 -9
  31. data/lib/distribution/f.rb +1 -1
  32. data/lib/distribution/f/gsl.rb +26 -22
  33. data/lib/distribution/f/java.rb +1 -1
  34. data/lib/distribution/f/ruby.rb +16 -19
  35. data/lib/distribution/f/statistics2.rb +22 -19
  36. data/lib/distribution/gamma.rb +5 -7
  37. data/lib/distribution/gamma/gsl.rb +13 -9
  38. data/lib/distribution/gamma/java.rb +1 -1
  39. data/lib/distribution/gamma/ruby.rb +5 -11
  40. data/lib/distribution/hypergeometric.rb +5 -8
  41. data/lib/distribution/hypergeometric/gsl.rb +4 -5
  42. data/lib/distribution/hypergeometric/java.rb +1 -1
  43. data/lib/distribution/hypergeometric/ruby.rb +34 -35
  44. data/lib/distribution/logistic.rb +5 -8
  45. data/lib/distribution/logistic/ruby.rb +13 -8
  46. data/lib/distribution/lognormal.rb +5 -7
  47. data/lib/distribution/lognormal/gsl.rb +8 -6
  48. data/lib/distribution/lognormal/ruby.rb +5 -9
  49. data/lib/distribution/math_extension.rb +6 -15
  50. data/lib/distribution/math_extension/chebyshev_series.rb +281 -272
  51. data/lib/distribution/math_extension/erfc.rb +26 -29
  52. data/lib/distribution/math_extension/exponential_integral.rb +17 -17
  53. data/lib/distribution/math_extension/gammastar.rb +19 -20
  54. data/lib/distribution/math_extension/gsl_utilities.rb +12 -12
  55. data/lib/distribution/math_extension/incomplete_beta.rb +52 -61
  56. data/lib/distribution/math_extension/incomplete_gamma.rb +166 -168
  57. data/lib/distribution/math_extension/log_utilities.rb +20 -22
  58. data/lib/distribution/normal.rb +11 -13
  59. data/lib/distribution/normal/gsl.rb +13 -10
  60. data/lib/distribution/normal/java.rb +14 -13
  61. data/lib/distribution/normal/ruby.rb +68 -58
  62. data/lib/distribution/normal/statistics2.rb +5 -2
  63. data/lib/distribution/normalmultivariate.rb +64 -64
  64. data/lib/distribution/poisson.rb +11 -13
  65. data/lib/distribution/poisson/gsl.rb +7 -7
  66. data/lib/distribution/poisson/java.rb +19 -24
  67. data/lib/distribution/poisson/ruby.rb +38 -9
  68. data/lib/distribution/shorthand.rb +17 -0
  69. data/lib/distribution/t.rb +13 -15
  70. data/lib/distribution/t/gsl.rb +27 -24
  71. data/lib/distribution/t/java.rb +1 -1
  72. data/lib/distribution/t/ruby.rb +99 -100
  73. data/lib/distribution/t/statistics2.rb +19 -19
  74. data/lib/distribution/uniform.rb +26 -0
  75. data/lib/distribution/uniform/gsl.rb +36 -0
  76. data/lib/distribution/uniform/ruby.rb +91 -0
  77. data/lib/distribution/version.rb +1 -1
  78. data/lib/distribution/weibull.rb +6 -7
  79. data/lib/distribution/weibull/gsl.rb +16 -16
  80. data/lib/distribution/weibull/ruby.rb +30 -23
  81. data/spec/beta_spec.rb +45 -47
  82. data/spec/binomial_spec.rb +77 -85
  83. data/spec/bivariatenormal_spec.rb +28 -35
  84. data/spec/chisquare_spec.rb +48 -52
  85. data/spec/distribution_spec.rb +10 -10
  86. data/spec/exponential_spec.rb +44 -49
  87. data/spec/f_spec.rb +4 -4
  88. data/spec/gamma_spec.rb +50 -53
  89. data/spec/hypergeometric_spec.rb +63 -69
  90. data/spec/logistic_spec.rb +32 -37
  91. data/spec/lognormal_spec.rb +25 -31
  92. data/spec/math_extension_spec.rb +192 -210
  93. data/spec/normal_spec.rb +80 -73
  94. data/spec/poisson_spec.rb +63 -41
  95. data/spec/shorthand_spec.rb +19 -22
  96. data/spec/spec_helper.rb +8 -9
  97. data/spec/t_spec.rb +63 -77
  98. data/spec/uniform_spec.rb +154 -0
  99. data/spec/weibull_spec.rb +13 -14
  100. metadata +17 -8
@@ -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,43 +1,71 @@
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
-
33
-
34
- it "should return correct p_value" do
35
- pending("No exact p_value")
61
+
62
+ it 'should return correct p_value' do
63
+ pending('No exact p_value')
36
64
  if @engine.respond_to? :p_value
37
- [0.1,1,5,10].each {|l|
65
+ [0.1, 1, 5, 10].each {|l|
38
66
  1.upto(20) {|k|
39
- pr=@engine.cdf(k,l)
40
- @engine.p_value(pr,l).should eq(k)
67
+ pr = @engine.cdf(k, l)
68
+ expect(@engine.p_value(pr, l)).to eq(k)
41
69
  }
42
70
  }
43
71
  else
@@ -45,48 +73,42 @@ describe Distribution::Poisson do
45
73
  end
46
74
  end
47
75
  end
48
-
49
76
 
50
- describe "singleton" do
77
+ describe 'singleton' do
51
78
  before do
52
- @engine=Distribution::Poisson
79
+ @engine = Distribution::Poisson
53
80
  end
54
- it_should_behave_like "poisson engine"
81
+ it_should_behave_like 'poisson engine'
55
82
  end
56
-
83
+
57
84
  describe Distribution::Poisson::Ruby_ do
58
85
  before do
59
- @engine=Distribution::Poisson::Ruby_
86
+ @engine = Distribution::Poisson::Ruby_
60
87
  end
61
- it_should_behave_like "poisson engine"
62
-
88
+ it_should_behave_like 'poisson engine'
63
89
  end
64
90
  if Distribution.has_gsl?
65
91
  describe Distribution::Poisson::GSL_ do
66
92
  before do
67
- @engine=Distribution::Poisson::GSL_
93
+ @engine = Distribution::Poisson::GSL_
68
94
  end
69
- it_should_behave_like "poisson engine"
70
-
95
+ it_should_behave_like 'poisson engine'
71
96
  end
72
97
  end
73
98
  if Distribution.has_java?
74
99
  describe Distribution::Poisson::Java_ do
75
100
  before do
76
- @engine=Distribution::Poisson::Java_
101
+ @engine = Distribution::Poisson::Java_
77
102
  end
78
- it_should_behave_like "poisson engine"
79
-
80
-
81
- it "should return correct cdf" do
82
- [0.5,1,1.5,4,10].each {|l|
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|
83
107
  1.upto(5) {|k|
84
- @engine.cdf(k,l).should be_within(1e-10).of(Distribution::Poisson::Ruby_.cdf(k,l))
108
+ expect(@engine.cdf(k, l)).to be_within(1e-10).of(Distribution::Poisson::Ruby_.cdf(k, l))
85
109
  }
86
110
  }
87
111
  end
88
-
89
-
90
112
  end
91
113
  end
92
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,9 +1,9 @@
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
@@ -26,24 +26,23 @@ RSpec.configure do |config|
26
26
  end
27
27
 
28
28
  module ExampleWithGSL
29
- def it_only_with_gsl(name,opts={},&block)
29
+ def it_only_with_gsl(name, opts = {}, &block)
30
30
  it(name, opts) do
31
31
  if Distribution.has_gsl?
32
32
  instance_eval(&block)
33
33
  else
34
- skip("Requires GSL")
34
+ skip('Requires GSL')
35
35
  end
36
36
  end
37
37
  end
38
-
39
- def it_only_with_java(name,&block)
38
+
39
+ def it_only_with_java(name, &block)
40
40
  it(name) do
41
41
  if Distribution.has_java?
42
42
  instance_eval(&block)
43
43
  else
44
- skip("Requires Java")
44
+ skip('Requires Java')
45
45
  end
46
46
  end
47
47
  end
48
-
49
48
  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