distribution 0.6.0 → 0.8.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 (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,280 +1,263 @@
1
- require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
2
  include ExampleWithGSL
3
3
  describe Distribution::MathExtension do
4
- it "binomial coefficient should be correctly calculated" do
5
-
6
- n=50
4
+ it 'binomial coefficient should be correctly calculated' do
5
+ n = 50
7
6
  n.times do |k|
8
- Math.binomial_coefficient(n,k).should eq(Math.factorial(n).quo(Math.factorial(k)*Math.factorial(n-k)))
7
+ expect(Math.binomial_coefficient(n, k)).to eq(Math.factorial(n).quo(Math.factorial(k) * Math.factorial(n - k))), "not correct for k=#{k}"
9
8
  end
10
9
  end
11
10
 
12
- it "ChebyshevSeries for :sin should return correct values" do
13
- #Math::SIN_CS.evaluate()
11
+ it 'ChebyshevSeries for :sin should return correct values' do
12
+ # Math::SIN_CS.evaluate()
14
13
  end
15
14
 
16
- it "log_1plusx_minusx should return correct values" do
15
+ it 'log_1plusx_minusx should return correct values' do
17
16
  # Tests from GSL-1.9
18
- Math::Log.log_1plusx_minusx(1.0e-10).should be_within(1e-10).of(-4.999999999666666667e-21)
19
- Math::Log.log_1plusx_minusx(1.0e-8).should be_within(1e-10).of(-4.999999966666666917e-17)
20
- Math::Log.log_1plusx_minusx(1.0e-4).should be_within(1e-10).of(-4.999666691664666833e-09)
21
- Math::Log.log_1plusx_minusx(0.1).should be_within(1e-10).of(-0.004689820195675139956)
22
- Math::Log.log_1plusx_minusx(0.49).should be_within(1e-10).of(-0.09122388004263222704)
23
-
24
- Math::Log.log_1plusx_minusx(-0.49).should be_within(1e-10).of(-0.18334455326376559639)
25
- Math::Log.log_1plusx_minusx(1.0).should be_within(1e-10).of(Math::LN2 - 1.0)
26
- Math::Log.log_1plusx_minusx(-0.99).should be_within(1e-10).of(-3.615170185988091368)
17
+ expect(Math::Log.log_1plusx_minusx(1.0e-10)).to be_within(1e-10).of(-4.999999999666666667e-21)
18
+ expect(Math::Log.log_1plusx_minusx(1.0e-8)).to be_within(1e-10).of(-4.999999966666666917e-17)
19
+ expect(Math::Log.log_1plusx_minusx(1.0e-4)).to be_within(1e-10).of(-4.999666691664666833e-09)
20
+ expect(Math::Log.log_1plusx_minusx(0.1)).to be_within(1e-10).of(-0.004689820195675139956)
21
+ expect(Math::Log.log_1plusx_minusx(0.49)).to be_within(1e-10).of(-0.09122388004263222704)
22
+
23
+ expect(Math::Log.log_1plusx_minusx(-0.49)).to be_within(1e-10).of(-0.18334455326376559639)
24
+ expect(Math::Log.log_1plusx_minusx(1.0)).to be_within(1e-10).of(Math::LN2 - 1.0)
25
+ expect(Math::Log.log_1plusx_minusx(-0.99)).to be_within(1e-10).of(-3.615170185988091368)
27
26
  end
28
27
 
29
- it "log_1plusx should return correct values" do
28
+ it 'log_1plusx should return correct values' do
30
29
  # Tests from GSL-1.9
31
- Math::Log.log_1plusx(1.0e-10).should be_within(1e-10).of(9.999999999500000000e-11)
32
- Math::Log.log_1plusx(1.0e-8).should be_within(1e-10).of(9.999999950000000333e-09)
33
- Math::Log.log_1plusx(1.0e-4).should be_within(1e-10).of(0.00009999500033330833533)
34
- Math::Log.log_1plusx(0.1).should be_within(1e-10).of(0.09531017980432486004)
35
- Math::Log.log_1plusx(0.49).should be_within(1e-10).of(0.3987761199573677730)
36
-
37
- Math::Log.log_1plusx(-0.49).should be_within(1e-10).of(-0.6733445532637655964)
38
- Math::Log.log_1plusx(1.0).should be_within(1e-10).of(Math::LN2)
39
- Math::Log.log_1plusx(-0.99).should be_within(1e-10).of(-4.605170185988091368)
30
+ expect(Math::Log.log_1plusx(1.0e-10)).to be_within(1e-10).of(9.999999999500000000e-11)
31
+ expect(Math::Log.log_1plusx(1.0e-8)).to be_within(1e-10).of(9.999999950000000333e-09)
32
+ expect(Math::Log.log_1plusx(1.0e-4)).to be_within(1e-10).of(0.00009999500033330833533)
33
+ expect(Math::Log.log_1plusx(0.1)).to be_within(1e-10).of(0.09531017980432486004)
34
+ expect(Math::Log.log_1plusx(0.49)).to be_within(1e-10).of(0.3987761199573677730)
35
+
36
+ expect(Math::Log.log_1plusx(-0.49)).to be_within(1e-10).of(-0.6733445532637655964)
37
+ expect(Math::Log.log_1plusx(1.0)).to be_within(1e-10).of(Math::LN2)
38
+ expect(Math::Log.log_1plusx(-0.99)).to be_within(1e-10).of(-4.605170185988091368)
40
39
  end
41
40
 
42
- it "log_beta should return correct values" do
43
- Math::Beta.log_beta(1.0e-8, 1.0e-8).first.should be_within(1e-10).of(19.113827924512310617)
44
- Math::Beta.log_beta(1.0e-8, 0.01).first.should be_within(1e-10).of(18.420681743788563403)
45
- Math::Beta.log_beta(1.0e-8, 1.0).first.should be_within(1e-10).of(18.420680743952365472)
46
- Math::Beta.log_beta(1.0e-8, 10.0).first.should be_within(1e-10).of(18.420680715662683009)
47
- Math::Beta.log_beta(1.0e-8, 1000.0).first.should be_within(1e-10).of(18.420680669107656949)
48
- Math::Beta.log_beta(0.1, 0.1).first.should be_within(1e-10).of(2.9813614810376273949)
49
- Math::Beta.log_beta(0.1, 1.0).first.should be_within(1e-10).of(2.3025850929940456840)
50
- Math::Beta.log_beta(0.1, 100.0).first.should be_within(1e-10).of(1.7926462324527931217)
51
- Math::Beta.log_beta(0.1, 1000).first.should be_within(1e-10).of(1.5619821298353164928)
52
- Math::Beta.log_beta(1.0, 1.00025).first.should be_within(1e-10).of(-0.0002499687552073570)
53
- Math::Beta.log_beta(1.0, 1.01).first.should be_within(1e-10).of(-0.009950330853168082848)
54
- Math::Beta.log_beta(1.0, 1000.0).first.should be_within(1e-10).of(-6.907755278982137052)
55
- Math::Beta.log_beta(100.0, 100.0).first.should be_within(1e-10).of(-139.66525908670663927)
56
- Math::Beta.log_beta(100.0, 1000.0).first.should be_within(1e-10).of(-336.4348576477366051)
57
- Math::Beta.log_beta(100.0, 1.0e+8).first.should be_within(1e-10).of(-1482.9339185256447309)
41
+ it 'log_beta should return correct values' do
42
+ expect(Math::Beta.log_beta(1.0e-8, 1.0e-8).first).to be_within(1e-10).of(19.113827924512310617)
43
+ expect(Math::Beta.log_beta(1.0e-8, 0.01).first).to be_within(1e-10).of(18.420681743788563403)
44
+ expect(Math::Beta.log_beta(1.0e-8, 1.0).first).to be_within(1e-10).of(18.420680743952365472)
45
+ expect(Math::Beta.log_beta(1.0e-8, 10.0).first).to be_within(1e-10).of(18.420680715662683009)
46
+ expect(Math::Beta.log_beta(1.0e-8, 1000.0).first).to be_within(1e-10).of(18.420680669107656949)
47
+ expect(Math::Beta.log_beta(0.1, 0.1).first).to be_within(1e-10).of(2.9813614810376273949)
48
+ expect(Math::Beta.log_beta(0.1, 1.0).first).to be_within(1e-10).of(2.3025850929940456840)
49
+ expect(Math::Beta.log_beta(0.1, 100.0).first).to be_within(1e-10).of(1.7926462324527931217)
50
+ expect(Math::Beta.log_beta(0.1, 1000).first).to be_within(1e-10).of(1.5619821298353164928)
51
+ expect(Math::Beta.log_beta(1.0, 1.00025).first).to be_within(1e-10).of(-0.0002499687552073570)
52
+ expect(Math::Beta.log_beta(1.0, 1.01).first).to be_within(1e-10).of(-0.009950330853168082848)
53
+ expect(Math::Beta.log_beta(1.0, 1000.0).first).to be_within(1e-10).of(-6.907755278982137052)
54
+ expect(Math::Beta.log_beta(100.0, 100.0).first).to be_within(1e-10).of(-139.66525908670663927)
55
+ expect(Math::Beta.log_beta(100.0, 1000.0).first).to be_within(1e-10).of(-336.4348576477366051)
56
+ expect(Math::Beta.log_beta(100.0, 1.0e+8).first).to be_within(1e-10).of(-1482.9339185256447309)
58
57
  end
59
58
 
60
- it "regularized_beta should return correct values" do
61
- Math.regularized_beta(0.0,1.0, 1.0).should be_within(1e-10).of(0.0)
62
- Math.regularized_beta(1.0, 1.0, 1.0).should be_within(1e-10).of(1.0)
63
- Math.regularized_beta(1.0, 0.1, 0.1).should be_within(1e-10).of(1.0)
64
- Math.regularized_beta(0.5, 1.0, 1.0).should be_within(1e-10).of(0.5)
65
- Math.regularized_beta(0.5, 0.1, 1.0).should be_within(1e-10).of(0.9330329915368074160)
66
- Math.regularized_beta(0.5, 10.0, 1.0).should be_within(1e-10).of(0.0009765625000000000000)
67
- Math.regularized_beta(0.5, 50.0, 1.0).should be_within(1e-10).of(8.881784197001252323e-16)
68
- Math.regularized_beta(0.5, 1.0, 0.1).should be_within(1e-10).of(0.06696700846319258402)
69
- Math.regularized_beta(0.5, 1.0, 10.0).should be_within(1e-10).of(0.99902343750000000000)
70
- Math.regularized_beta(0.5, 1.0, 50.0).should be_within(1e-10).of(0.99999999999999911180)
71
- Math.regularized_beta(0.1, 1.0, 1.0).should be_within(1e-10).of(0.10)
72
- Math.regularized_beta(0.1, 1.0, 2.0).should be_within(1e-10).of(0.19)
73
- Math.regularized_beta(0.9, 1.0, 2.0).should be_within(1e-10).of(0.99)
74
- Math.regularized_beta(0.5, 50.0, 60.0).should be_within(1e-10).of(0.8309072939016694143)
75
- Math.regularized_beta(0.5, 90.0, 90.0).should be_within(1e-10).of(0.5)
76
- Math.regularized_beta(0.5, 500.0, 500.0).should be_within(1e-10).of(0.5)
77
- Math.regularized_beta(0.4, 5000.0, 5000.0).should be_within(1e-10).of(4.518543727260666383e-91)
78
- Math.regularized_beta(0.6, 5000.0, 5000.0).should be_within(1e-10).of(1.0)
79
- Math.regularized_beta(0.6, 5000.0, 2000.0).should be_within(1e-10).of(8.445388773903332659e-89)
59
+ it 'regularized_beta should return correct values' do
60
+ expect(Math.regularized_beta(0.0, 1.0, 1.0)).to be_within(1e-10).of(0.0)
61
+ expect(Math.regularized_beta(1.0, 1.0, 1.0)).to be_within(1e-10).of(1.0)
62
+ expect(Math.regularized_beta(1.0, 0.1, 0.1)).to be_within(1e-10).of(1.0)
63
+ expect(Math.regularized_beta(0.5, 1.0, 1.0)).to be_within(1e-10).of(0.5)
64
+ expect(Math.regularized_beta(0.5, 0.1, 1.0)).to be_within(1e-10).of(0.9330329915368074160)
65
+ expect(Math.regularized_beta(0.5, 10.0, 1.0)).to be_within(1e-10).of(0.0009765625000000000000)
66
+ expect(Math.regularized_beta(0.5, 50.0, 1.0)).to be_within(1e-10).of(8.881784197001252323e-16)
67
+ expect(Math.regularized_beta(0.5, 1.0, 0.1)).to be_within(1e-10).of(0.06696700846319258402)
68
+ expect(Math.regularized_beta(0.5, 1.0, 10.0)).to be_within(1e-10).of(0.99902343750000000000)
69
+ expect(Math.regularized_beta(0.5, 1.0, 50.0)).to be_within(1e-10).of(0.99999999999999911180)
70
+ expect(Math.regularized_beta(0.1, 1.0, 1.0)).to be_within(1e-10).of(0.10)
71
+ expect(Math.regularized_beta(0.1, 1.0, 2.0)).to be_within(1e-10).of(0.19)
72
+ expect(Math.regularized_beta(0.9, 1.0, 2.0)).to be_within(1e-10).of(0.99)
73
+ expect(Math.regularized_beta(0.5, 50.0, 60.0)).to be_within(1e-10).of(0.8309072939016694143)
74
+ expect(Math.regularized_beta(0.5, 90.0, 90.0)).to be_within(1e-10).of(0.5)
75
+ expect(Math.regularized_beta(0.5, 500.0, 500.0)).to be_within(1e-10).of(0.5)
76
+ expect(Math.regularized_beta(0.4, 5000.0, 5000.0)).to be_within(1e-10).of(4.518543727260666383e-91)
77
+ expect(Math.regularized_beta(0.6, 5000.0, 5000.0)).to be_within(1e-10).of(1.0)
78
+ expect(Math.regularized_beta(0.6, 5000.0, 2000.0)).to be_within(1e-10).of(8.445388773903332659e-89)
80
79
  end
81
- it_only_with_gsl "incomplete_beta should return correct values" do
82
-
83
- a=rand()*10+1
84
- b=rand()*10+1
85
- ib = GSL::Function.alloc { |t| t**(a-1)*(1-t)**(b-1)}
80
+ it_only_with_gsl 'incomplete_beta should return correct values' do
81
+ a = rand * 10 + 1
82
+ b = rand * 10 + 1
83
+ ib = GSL::Function.alloc { |t| t**(a - 1) * (1 - t)**(b - 1) }
86
84
  w = GSL::Integration::Workspace.alloc(1000)
87
85
  1.upto(10) {|x|
88
- inte=ib.qag([0,x / 10.0],w)
89
- Math.incomplete_beta(x/10.0, a ,b).should be_within(1e-10).of(inte[0])
86
+ inte = ib.qag([0, x / 10.0], w)
87
+ expect(Math.incomplete_beta(x / 10.0, a, b)).to be_within(1e-10).of(inte[0])
90
88
  }
91
89
  end
92
90
 
93
- it "gammastar should return correct values" do
91
+ it 'gammastar should return correct values' do
94
92
  # Tests from GSL-1.9
95
- Math::Gammastar.evaluate(1.0e-08).should be_within(1e-10).of(3989.423555759890865)
96
- Math::Gammastar.evaluate(1.0e-05).should be_within(1e-10).of(126.17168469882690233)
97
- Math::Gammastar.evaluate(0.001).should be_within(1e-10).of(12.708492464364073506)
98
- Math::Gammastar.evaluate(1.5).should be_within(1e-10).of(1.0563442442685598666)
99
- Math::Gammastar.evaluate(3.0).should be_within(1e-10).of(1.0280645179187893045)
100
- Math::Gammastar.evaluate(9.0).should be_within(1e-10).of(1.0092984264218189715)
101
- Math::Gammastar.evaluate(11.0).should be_within(1e-10).of(1.0076024283104962850)
102
- Math::Gammastar.evaluate(100.0).should be_within(1e-10).of(1.0008336778720121418)
103
- Math::Gammastar.evaluate(1.0e+05).should be_within(1e-10).of(1.0000008333336805529)
104
- Math::Gammastar.evaluate(1.0e+20).should be_within(1e-10).of(1.0)
93
+ expect(Math::Gammastar.evaluate(1.0e-08)).to be_within(1e-10).of(3989.423555759890865)
94
+ expect(Math::Gammastar.evaluate(1.0e-05)).to be_within(1e-10).of(126.17168469882690233)
95
+ expect(Math::Gammastar.evaluate(0.001)).to be_within(1e-10).of(12.708492464364073506)
96
+ expect(Math::Gammastar.evaluate(1.5)).to be_within(1e-10).of(1.0563442442685598666)
97
+ expect(Math::Gammastar.evaluate(3.0)).to be_within(1e-10).of(1.0280645179187893045)
98
+ expect(Math::Gammastar.evaluate(9.0)).to be_within(1e-10).of(1.0092984264218189715)
99
+ expect(Math::Gammastar.evaluate(11.0)).to be_within(1e-10).of(1.0076024283104962850)
100
+ expect(Math::Gammastar.evaluate(100.0)).to be_within(1e-10).of(1.0008336778720121418)
101
+ expect(Math::Gammastar.evaluate(1.0e+05)).to be_within(1e-10).of(1.0000008333336805529)
102
+ expect(Math::Gammastar.evaluate(1.0e+20)).to be_within(1e-10).of(1.0)
105
103
  end
106
104
 
107
- it "erfc_e should return correct values" do
105
+ it 'erfc_e should return correct values' do
108
106
  # From GSL-1.9. For troubleshooting gammq.
109
- Math::erfc_e(-10.0).should be_within(1e-10).of(2.0)
110
- Math::erfc_e(-5.0000002).should be_within(1e-10).of(1.9999999999984625433)
111
- Math::erfc_e(-5.0).should be_within(1e-10).of(1.9999999999984625402)
112
- Math::erfc_e(-1.0).should be_within(1e-10).of(1.8427007929497148693)
113
- Math::erfc_e(-0.5).should be_within(1e-10).of(1.5204998778130465377)
114
- Math::erfc_e(1.0).should be_within(1e-10).of(0.15729920705028513066)
115
- Math::erfc_e(3.0).should be_within(1e-10).of(0.000022090496998585441373)
116
- Math::erfc_e(7.0).should be_within(1e-10).of(4.183825607779414399e-23)
117
- Math::erfc_e(10.0).should be_within(1e-10).of(2.0884875837625447570e-45)
107
+ expect(Math.erfc_e(-10.0)).to be_within(1e-10).of(2.0)
108
+ expect(Math.erfc_e(-5.0000002)).to be_within(1e-10).of(1.9999999999984625433)
109
+ expect(Math.erfc_e(-5.0)).to be_within(1e-10).of(1.9999999999984625402)
110
+ expect(Math.erfc_e(-1.0)).to be_within(1e-10).of(1.8427007929497148693)
111
+ expect(Math.erfc_e(-0.5)).to be_within(1e-10).of(1.5204998778130465377)
112
+ expect(Math.erfc_e(1.0)).to be_within(1e-10).of(0.15729920705028513066)
113
+ expect(Math.erfc_e(3.0)).to be_within(1e-10).of(0.000022090496998585441373)
114
+ expect(Math.erfc_e(7.0)).to be_within(1e-10).of(4.183825607779414399e-23)
115
+ expect(Math.erfc_e(10.0)).to be_within(1e-10).of(2.0884875837625447570e-45)
118
116
  end
119
117
 
118
+ it 'unnormalized_incomplete_gamma with x=0 should return correct values' do
119
+ expect(Math.unnormalized_incomplete_gamma(-1.5, 0)).to be_within(1e-10).of(4.0 * Math.sqrt(Math::PI) / 3.0)
120
+ expect(Math.unnormalized_incomplete_gamma(-0.5, 0)).to be_within(1e-10).of(-2 * Math.sqrt(Math::PI))
121
+ expect(Math.unnormalized_incomplete_gamma(0.5, 0)).to be_within(1e-10).of(Math.sqrt(Math::PI))
122
+ expect(Math.unnormalized_incomplete_gamma(1.0, 0)).to eq 1.0
123
+ expect(Math.unnormalized_incomplete_gamma(1.5, 0)).to be_within(1e-10).of(Math.sqrt(Math::PI) / 2.0)
124
+ expect(Math.unnormalized_incomplete_gamma(2.0, 0)).to eq 1.0
125
+ expect(Math.unnormalized_incomplete_gamma(2.5, 0)).to be_within(1e-10).of(0.75 * Math.sqrt(Math::PI))
126
+
127
+ expect(Math.unnormalized_incomplete_gamma(3.0, 0)).to be_within(1e-12).of(2.0)
120
128
 
121
- it "unnormalized_incomplete_gamma with x=0 should return correct values" do
122
- Math.unnormalized_incomplete_gamma(-1.5, 0).should be_within(1e-10).of(4.0*Math.sqrt(Math::PI)/3.0)
123
- Math.unnormalized_incomplete_gamma(-0.5, 0).should be_within(1e-10).of(-2*Math.sqrt(Math::PI))
124
- Math.unnormalized_incomplete_gamma(0.5, 0).should be_within(1e-10).of(Math.sqrt(Math::PI))
125
- Math.unnormalized_incomplete_gamma(1.0, 0).should eq 1.0
126
- Math.unnormalized_incomplete_gamma(1.5, 0).should be_within(1e-10).of(Math.sqrt(Math::PI)/2.0)
127
- Math.unnormalized_incomplete_gamma(2.0, 0).should eq 1.0
128
- Math.unnormalized_incomplete_gamma(2.5, 0).should be_within(1e-10).of(0.75*Math.sqrt(Math::PI))
129
- Math.unnormalized_incomplete_gamma(3.0, 0).should eq 2.0
130
- Math.unnormalized_incomplete_gamma(3.5, 0).should be_within(1e-10).of(15.0*Math.sqrt(Math::PI) / 8.0)
131
- Math.unnormalized_incomplete_gamma(4.0, 0).should eq 6.0
129
+ expect(Math.unnormalized_incomplete_gamma(3.5, 0)).to be_within(1e-10).of(15.0 * Math.sqrt(Math::PI) / 8.0)
130
+ expect(Math.unnormalized_incomplete_gamma(4.0, 0)).to be_within(1e-12).of(6.0)
132
131
  end
133
132
 
134
- it "incomplete_gamma should return correct values" do
133
+ it 'incomplete_gamma should return correct values' do
135
134
  # Tests from GSL-1.9
136
- Math.incomplete_gamma(1e-100, 0.001).should be_within(1e-10).of(1.0)
137
- Math.incomplete_gamma(0.001, 0.001).should be_within(1e-10).of(0.9936876467088602902)
138
- Math.incomplete_gamma(0.001, 1.0).should be_within(1e-10).of(0.9997803916424144436)
139
- Math.incomplete_gamma(0.001, 10.0).should be_within(1e-10).of(0.9999999958306921828)
140
- Math.incomplete_gamma(1.0, 0.001).should be_within(1e-10).of(0.0009995001666250083319)
141
- Math.incomplete_gamma(1.0, 1.01).should be_within(1e-10).of(0.6357810204284766802)
142
- Math.incomplete_gamma(1.0, 10.0).should be_within(1e-10).of(0.9999546000702375151)
143
- Math.incomplete_gamma(10.0, 10.01).should be_within(1e-10).of(0.5433207586693410570)
144
- Math.incomplete_gamma(10.0, 20.0).should be_within(1e-10).of(0.9950045876916924128)
145
- Math.incomplete_gamma(1000.0, 1000.1).should be_within(1e-10).of(0.5054666401440661753)
146
- Math.incomplete_gamma(1000.0, 2000.0).should be_within(1e-10).of(1.0)
135
+ expect(Math.incomplete_gamma(1e-100, 0.001)).to be_within(1e-10).of(1.0)
136
+ expect(Math.incomplete_gamma(0.001, 0.001)).to be_within(1e-10).of(0.9936876467088602902)
137
+ expect(Math.incomplete_gamma(0.001, 1.0)).to be_within(1e-10).of(0.9997803916424144436)
138
+ expect(Math.incomplete_gamma(0.001, 10.0)).to be_within(1e-10).of(0.9999999958306921828)
139
+ expect(Math.incomplete_gamma(1.0, 0.001)).to be_within(1e-10).of(0.0009995001666250083319)
140
+ expect(Math.incomplete_gamma(1.0, 1.01)).to be_within(1e-10).of(0.6357810204284766802)
141
+ expect(Math.incomplete_gamma(1.0, 10.0)).to be_within(1e-10).of(0.9999546000702375151)
142
+ expect(Math.incomplete_gamma(10.0, 10.01)).to be_within(1e-10).of(0.5433207586693410570)
143
+ expect(Math.incomplete_gamma(10.0, 20.0)).to be_within(1e-10).of(0.9950045876916924128)
144
+ expect(Math.incomplete_gamma(1000.0, 1000.1)).to be_within(1e-10).of(0.5054666401440661753)
145
+ expect(Math.incomplete_gamma(1000.0, 2000.0)).to be_within(1e-10).of(1.0)
147
146
 
148
147
  # designed to trap the a-x=1 problem
149
148
  # These next two are 1e-7 because they give the same output as GSL, but GSL is apparently not totally accurate here.
150
149
  # It's a problem with log_1plusx_mx (log_1plusx_minusx in my code)
151
- Math.incomplete_gamma(100, 99.0).should be_within(1e-7).of(0.4733043303994607)
152
- Math.incomplete_gamma(200, 199.0).should be_within(1e-7).of(0.4811585880878718)
150
+ expect(Math.incomplete_gamma(100, 99.0)).to be_within(1e-7).of(0.4733043303994607)
151
+ expect(Math.incomplete_gamma(200, 199.0)).to be_within(1e-7).of(0.4811585880878718)
153
152
 
154
153
  # Test for x86 cancellation problems
155
- Math.incomplete_gamma(5670, 4574).should be_within(1e-10).of(3.063972328743934e-55)
154
+ expect(Math.incomplete_gamma(5670, 4574)).to be_within(1e-10).of(3.063972328743934e-55)
156
155
  end
157
156
 
158
- it "gammq should return correct values" do
157
+ it 'gammq should return correct values' do
159
158
  # Tests from GSL-1.9
160
- Math.gammq(0.0, 0.001).should be_within(1e-10).of(0.0)
161
- Math.gammq(0.001, 0.001).should be_within(1e-10).of(0.006312353291139709793)
162
- Math.gammq(0.001, 1.0).should be_within(1e-10).of(0.00021960835758555639171)
163
- Math.gammq(0.001, 2.0).should be_within(1e-10).of(0.00004897691783098147880)
164
- Math.gammq(0.001, 5.0).should be_within(1e-10).of(1.1509813397308608541e-06)
165
- Math.gammq(1.0, 0.001).should be_within(1e-10).of(0.9990004998333749917)
166
- Math.gammq(1.0, 1.01).should be_within(1e-10).of(0.3642189795715233198)
167
- Math.gammq(1.0, 10.0).should be_within(1e-10).of(0.00004539992976248485154)
168
- Math.gammq(10.0, 10.01).should be_within(1e-10).of(0.4566792413306589430)
169
- Math.gammq(10.0, 100.0).should be_within(1e-10).of(1.1253473960842733885e-31)
170
- Math.gammq(1000.0, 1000.1).should be_within(1e-10).of(0.4945333598559338247)
171
- Math.gammq(1000.0, 2000.0).should be_within(1e-10).of(6.847349459614753180e-136)
159
+ expect(Math.gammq(0.0, 0.001)).to be_within(1e-10).of(0.0)
160
+ expect(Math.gammq(0.001, 0.001)).to be_within(1e-10).of(0.006312353291139709793)
161
+ expect(Math.gammq(0.001, 1.0)).to be_within(1e-10).of(0.00021960835758555639171)
162
+ expect(Math.gammq(0.001, 2.0)).to be_within(1e-10).of(0.00004897691783098147880)
163
+ expect(Math.gammq(0.001, 5.0)).to be_within(1e-10).of(1.1509813397308608541e-06)
164
+ expect(Math.gammq(1.0, 0.001)).to be_within(1e-10).of(0.9990004998333749917)
165
+ expect(Math.gammq(1.0, 1.01)).to be_within(1e-10).of(0.3642189795715233198)
166
+ expect(Math.gammq(1.0, 10.0)).to be_within(1e-10).of(0.00004539992976248485154)
167
+ expect(Math.gammq(10.0, 10.01)).to be_within(1e-10).of(0.4566792413306589430)
168
+ expect(Math.gammq(10.0, 100.0)).to be_within(1e-10).of(1.1253473960842733885e-31)
169
+ expect(Math.gammq(1000.0, 1000.1)).to be_within(1e-10).of(0.4945333598559338247)
170
+ expect(Math.gammq(1000.0, 2000.0)).to be_within(1e-10).of(6.847349459614753180e-136)
172
171
 
173
172
  # designed to trap the a-x=1 problem
174
- Math.gammq(100, 99.0).should be_within(1e-10).of(0.5266956696005394)
175
- Math.gammq(200, 199.0).should be_within(1e-10).of(0.5188414119121281)
176
-
177
- # Test for x86 cancellation problems
178
- Math.gammq(5670, 4574).should be_within(1e-10).of(1.0000000000000000)
173
+ expect(Math.gammq(100, 99.0)).to be_within(1e-10).of(0.5266956696005394)
174
+ expect(Math.gammq(200, 199.0)).to be_within(1e-10).of(0.5188414119121281)
179
175
 
176
+ # Test for x86 cancellation problems
177
+ expect(Math.gammq(5670, 4574)).to be_within(1e-10).of(1.0000000000000000)
180
178
 
181
179
  # test suggested by Michel Lespinasse [gsl-discuss Sat, 13 Nov 2004]
182
- Math.gammq(1.0e+06-1.0, 1.0e+06-2.0).should be_within(1e-10).of(0.50026596175224547004)
180
+ expect(Math.gammq(1.0e+06 - 1.0, 1.0e+06 - 2.0)).to be_within(1e-10).of(0.50026596175224547004)
183
181
 
184
182
  # tests in asymptotic regime related to Lespinasse test
185
- Math.gammq(1.0e+06+2.0, 1.0e+06+1.0).should be_within(1e-10).of(0.50026596135330304336)
186
- Math.gammq(1.0e+06, 1.0e+06-2.0).should be_within(1e-10).of(0.50066490399940144811)
187
- Math.gammq(1.0e+07, 1.0e+07-2.0).should be_within(1e-10).of(0.50021026104978614908)
183
+ expect(Math.gammq(1.0e+06 + 2.0, 1.0e+06 + 1.0)).to be_within(1e-10).of(0.50026596135330304336)
184
+ expect(Math.gammq(1.0e+06, 1.0e+06 - 2.0)).to be_within(1e-10).of(0.50066490399940144811)
185
+ expect(Math.gammq(1.0e+07, 1.0e+07 - 2.0)).to be_within(1e-10).of(0.50021026104978614908)
188
186
  end
189
187
 
190
-
191
- it "rising_factorial should return correct values" do
192
-
193
- x=rand(10)+1
194
- Math.rising_factorial(x,0).should eq 1
195
- Math.rising_factorial(x,1).should eq x
196
- Math.rising_factorial(x,2).should eq x**2+x
197
- Math.rising_factorial(x,3).should eq x**3+3*x**2+2*x
198
- Math.rising_factorial(x,4).should eq x**4+6*x**3+11*x**2+6*x
199
-
188
+ it 'rising_factorial should return correct values' do
189
+ x = rand(10) + 1
190
+ expect(Math.rising_factorial(x, 0)).to eq 1
191
+ expect(Math.rising_factorial(x, 1)).to eq x
192
+ expect(Math.rising_factorial(x, 2)).to eq x**2 + x
193
+ expect(Math.rising_factorial(x, 3)).to eq x**3 + 3 * x**2 + 2 * x
194
+ expect(Math.rising_factorial(x, 4)).to eq x**4 + 6 * x**3 + 11 * x**2 + 6 * x
200
195
  end
201
-
202
- it "permutations should return correct values" do
203
- n=rand(50)+50
196
+
197
+ it 'permutations should return correct values' do
198
+ n = rand(50) + 50
204
199
  10.times { |k|
205
- Math.permutations(n,k).should eq(Math.factorial(n) / Math.factorial(n-k))
200
+ expect(Math.permutations(n, k)).to eq(Math.factorial(n) / Math.factorial(n - k))
206
201
  }
207
-
208
-
209
- Math.permutations(n,n).should eq(Math.factorial(n) / Math.factorial(n-n))
202
+
203
+ expect(Math.permutations(n, n)).to eq(Math.factorial(n) / Math.factorial(n - n))
210
204
  end
211
-
212
-
213
- it "exact regularized incomplete beta should behave properly" do
214
-
215
- Math.exact_regularized_beta(0.5,5,5).should be_within(1e-6).of(0.5)
216
- Math.exact_regularized_beta(0.5,5,6).should be_within(1e-6).of(0.6230469)
217
- Math.exact_regularized_beta(0.5,5,7).should be_within(1e-6).of(0.725586)
218
-
219
- a=5
220
- b=5
221
- Math.exact_regularized_beta(0,a,b).should eq 0
222
- Math.exact_regularized_beta(1,a,b).should eq 1
223
- x=rand()
224
-
225
- Math.exact_regularized_beta(x,a,b).should be_within(1e-6). of(1-Math.regularized_beta(1-x,b,a))
226
-
227
-
205
+
206
+ it 'exact regularized incomplete beta should behave properly' do
207
+ expect(Math.exact_regularized_beta(0.5, 5, 5)).to be_within(1e-6).of(0.5)
208
+ expect(Math.exact_regularized_beta(0.5, 5, 6)).to be_within(1e-6).of(0.6230469)
209
+ expect(Math.exact_regularized_beta(0.5, 5, 7)).to be_within(1e-6).of(0.725586)
210
+
211
+ a = 5
212
+ b = 5
213
+ expect(Math.exact_regularized_beta(0, a, b)).to eq 0
214
+ expect(Math.exact_regularized_beta(1, a, b)).to eq 1
215
+ x = rand
216
+
217
+ expect(Math.exact_regularized_beta(x, a, b)).to be_within(1e-6). of(1 - Math.regularized_beta(1 - x, b, a))
228
218
  end
229
-
230
- it "binomial coefficient(gamma) with n<=48 should be correct " do
231
-
232
- [1,5,10,25,48].each {|n|
233
- k=(n/2).to_i
234
- Math.binomial_coefficient_gamma(n,k).round.should eq(Math.binomial_coefficient(n,k))
219
+
220
+ it 'binomial coefficient(gamma) with n<=48 should be correct ' do
221
+ [1, 5, 10, 25, 48].each {|n|
222
+ k = (n / 2).to_i
223
+ expect(Math.binomial_coefficient_gamma(n, k).round).to eq(Math.binomial_coefficient(n, k))
235
224
  }
236
225
  end
237
-
238
- it "binomial coefficient(gamma) with 48<n<1000 should have 11 correct digits" do
239
-
240
- [50,100,200,1000].each {|n|
241
- k=(n/2).to_i
242
- obs=Math.binomial_coefficient_gamma(n, k).to_i.to_s[0,11]
243
- exp=Math.binomial_coefficient(n, k).to_i.to_s[0,11]
244
-
245
- obs.should eq(exp)
226
+
227
+ it 'binomial coefficient(gamma) with 48<n<1000 should have 11 correct digits' do
228
+ [50, 100, 200, 1000].each {|n|
229
+ k = (n / 2).to_i
230
+ obs = Math.binomial_coefficient_gamma(n, k).to_i.to_s[0, 11]
231
+ exp = Math.binomial_coefficient(n, k).to_i.to_s[0, 11]
232
+
233
+ expect(obs).to eq(exp)
246
234
  }
247
235
  end
248
-
249
- describe Distribution::MathExtension::SwingFactorial do
250
236
 
251
- it "Math.factorial should return correct values x<20" do
252
- ac=3628800 # 10!
237
+ describe Distribution::MathExtension::SwingFactorial do
238
+ it 'Math.factorial should return correct values x<20' do
239
+ ac = 3_628_800 # 10!
253
240
  11.upto(19).each do |i|
254
- ac*=i
255
- Math.factorial(i).should eq(ac)
241
+ ac *= i
242
+ expect(Math.factorial(i)).to eq(ac)
256
243
  end
257
244
  end
258
-
259
- it "Math.factorial should return correct values for values 21<x<33" do
260
-
261
- ac=2432902008176640000 # 20!
245
+
246
+ it 'Math.factorial should return correct values for values 21<x<33' do
247
+ ac = 2_432_902_008_176_640_000 # 20!
262
248
  21.upto(33).each do |i|
263
- ac*=i
264
- Math.factorial(i).should eq(ac)
249
+ ac *= i
250
+ expect(Math.factorial(i)).to eq(ac)
265
251
  end
266
-
267
252
  end
268
-
269
- it "Math.factorial should return correct values for values x>33" do
270
-
271
- ac=8683317618811886495518194401280000000 # 33!
272
- Math.factorial(33).should eq ac
253
+
254
+ it 'Math.factorial should return correct values for values x>33' do
255
+ ac = 8_683_317_618_811_886_495_518_194_401_280_000_000 # 33!
256
+ expect(Math.factorial(33)).to eq ac
273
257
  34.upto(40).each do |i|
274
- ac*=i
275
- Math.factorial(i).should eq(ac)
258
+ ac *= i
259
+ expect(Math.factorial(i)).to eq(ac)
276
260
  end
277
-
278
261
  end
279
262
  end
280
263
  end