rubystats 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9002f153bddfc7d2089ad357dd7eb2ded0580315
4
- data.tar.gz: 6ee9d7ac4a874ce488f09b13fd52d41b34c26de8
3
+ metadata.gz: d5175da9b495dae606b09aa31d28749aecea3186
4
+ data.tar.gz: c5b88f1fb680e8cf3e145cecef48edf5d211ad69
5
5
  SHA512:
6
- metadata.gz: b960257b9ba3aa6bbf0507db971b0a8a069c8baa10c83727b52f94a939b20dbfa52966c74c184b1daebde3f2d53be990237d17cc6248b1dfb394a03d8ad4c933
7
- data.tar.gz: b42484ba551d47356f2345fe4119a8e81768ccff8d6e40077f5243e4137fddb70230dd66c75e4a3a1db1f78050cc703264ed6803c0ad3cc81b90ac1c2000d198
6
+ metadata.gz: a2256ea1de0b37962a8f3d5bdc14a33352d4a9a9d12c638df6f1c931464c145fcf1e50b82a123a7c34b75041fbc16b3e560dfca53983e90a132d1a1bc903c4ae
7
+ data.tar.gz: 27f8ad23f16195d41786b58f002150d48bbe1a9af7ccaae2ee703c78637dffa7207479f38affdc2c9296e5fb2827b6a61d60897dab0a667710988ce735479bcf
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ Gemfile.lock
1
2
  .rvmrc
2
3
  *.gem
3
4
  *.rbc
@@ -5,6 +5,8 @@ before_script:
5
5
  - 'echo ''gem: --no-ri --no-rdoc'' > ~/.gemrc' # skip installing docs for gems
6
6
  script: 'bundle exec rake test'
7
7
  rvm:
8
+ - 1.9.3
8
9
  - 2.0.0
9
10
  - 2.1.0
10
11
  - 2.2.0
12
+ - 2.3.0
@@ -81,7 +81,7 @@ Output:
81
81
 
82
82
  == REQUIREMENTS:
83
83
 
84
- * Ruby > 1.8.2 (may work with earlier versions)
84
+ * Ruby >= 1.9.3 (may work with earlier versions)
85
85
 
86
86
  == INSTALL:
87
87
 
@@ -70,7 +70,7 @@ module Rubystats
70
70
  # variable x is less then X, i.e. P(x < _x).
71
71
  def cdf(_x)
72
72
  if _x.class == Array
73
- inv_vals = []
73
+ pdf_vals = []
74
74
  for i in (0 ..._x.length)
75
75
  pdf_vals[i] = get_cdf(_x[i])
76
76
  end
@@ -150,7 +150,7 @@ module Rubystats
150
150
  am = @n * p
151
151
  if @n < 25
152
152
  bnl = 0.0
153
- for i in (1...@n)
153
+ (1...@n).each do
154
154
  if Kernel.rand < p
155
155
  bnl = bnl.next
156
156
  end
@@ -30,14 +30,14 @@ module Rubystats
30
30
  # See http://lib.stat.cmu.edu/apstat/245
31
31
  def lngamm(z)
32
32
  x = 0
33
- x += 0.0000001659470187408462/(z+7)
34
- x += 0.000009934937113930748 /(z+6)
35
- x -= 0.1385710331296526 /(z+5)
36
- x += 12.50734324009056 /(z+4)
37
- x -= 176.6150291498386 /(z+3)
38
- x += 771.3234287757674 /(z+2)
39
- x -= 1259.139216722289 /(z+1)
40
- x += 676.5203681218835 /(z)
33
+ x += 0.0000001659470187408462 / (z+7)
34
+ x += 0.000009934937113930748 / (z+6)
35
+ x -= 0.1385710331296526 / (z+5)
36
+ x += 12.50734324009056 / (z+4)
37
+ x -= 176.6150291498386 / (z+3)
38
+ x += 771.3234287757674 / (z+2)
39
+ x -= 1259.139216722289 / (z+1)
40
+ x += 676.5203681218835 / (z)
41
41
  x += 0.9999999999995183
42
42
 
43
43
  return(Math.log(x)-5.58106146679532777-z+(z-0.5) * Math.log(z+6.5))
@@ -159,7 +159,7 @@ module Rubystats
159
159
  n1_ = n11_ + n12_
160
160
  n_1 = n11_ + n21_
161
161
  n = n11_ + n12_ + n21_ + n22_
162
- prob = exact(n11_,n1_,n_1,n)
162
+ exact(n11_,n1_,n_1,n)
163
163
  left = @sless
164
164
  right = @slarg
165
165
  twotail = @sleft + @sright
@@ -36,6 +36,9 @@ module Rubystats
36
36
 
37
37
  include Rubystats::NumericalConstants
38
38
 
39
+ attr_reader :log_gamma_cache_res, :log_gamma_cache_x,
40
+ :log_beta_cache_res, :log_beta_cache_p, :log_beta_cache_q
41
+
39
42
  @log_gamma_cache_res = 0.0
40
43
  @log_gamma_cache_x = 0.0
41
44
  @log_beta_cache_res = 0.0
@@ -43,7 +46,7 @@ module Rubystats
43
46
  @log_beta_cache_q = 0.0
44
47
 
45
48
  def log_beta(p,q)
46
- if p != @log_beta_cache_p || q != @log_beta_cache_q
49
+ if p != log_beta_cache_p || q != log_beta_cache_q
47
50
  @log_beta_cache_p = p
48
51
  @log_beta_cache_q = q
49
52
  if (p <= 0.0) || (q <= 0.0) || (p + q) > LOG_GAMMA_X_MAX_VALUE
@@ -52,7 +55,7 @@ module Rubystats
52
55
  @log_beta_cache_res = log_gamma(p) + log_gamma(q) - log_gamma(p + q)
53
56
  end
54
57
  end
55
- return @log_beta_cache_res
58
+ return log_beta_cache_res
56
59
  end
57
60
 
58
61
  # Gamma function.
@@ -84,7 +87,7 @@ module Rubystats
84
87
  # </P>
85
88
  # Author:: Jaco van Kooten
86
89
 
87
- def orig_gamma(x)
90
+ def orig_gamma(x)
88
91
  # Gamma related constants
89
92
  g_p = [ -1.71618513886549492533811, 24.7656508055759199108314,
90
93
  -379.804256470945635097577, 629.331155312818442661052,
@@ -93,7 +96,7 @@ module Rubystats
93
96
  g_q = [-30.8402300119738975254353, 315.350626979604161529144,
94
97
  -1015.15636749021914166146, -3107.77167157231109440444,
95
98
  22538.1184209801510330112, 4755.84627752788110767815,
96
- -134659.959864969306392456, -115132.259675553483497211 ]
99
+ -134659.959864969306392456, -115132.259675553483497211 ]
97
100
  g_c = [-0.001910444077728, 8.4171387781295e-4, -5.952379913043012e-4,
98
101
  7.93650793500350248e-4, -0.002777777777777681622553,
99
102
  0.08333333333333333331554247, 0.0057083835261 ]
@@ -102,14 +105,14 @@ module Rubystats
102
105
  n = 0
103
106
  y = x
104
107
  parity = false
105
- if y <= 0.0
108
+ if y <= 0.0
106
109
  # ----------------------------------------------------------------------
107
110
  # Argument is negative
108
111
  # ----------------------------------------------------------------------
109
112
  y = -(x)
110
113
  y1 = y.to_i
111
114
  res = y - y1
112
- if res != 0.0
115
+ if res != 0.0
113
116
  if y1 != (((y1*0.5).to_i) * 2.0)
114
117
  parity = true
115
118
  fact = -M_pi/sin(M_pi * res)
@@ -141,7 +144,7 @@ module Rubystats
141
144
  # ----------------------------------------------------------------------
142
145
  z = y
143
146
  y += 1
144
- else
147
+ else
145
148
  # ----------------------------------------------------------------------
146
149
  # 1.0 .LT. argument .LT. 12.0, reduce argument if necessary
147
150
  # ----------------------------------------------------------------------
@@ -154,7 +157,7 @@ module Rubystats
154
157
  # ----------------------------------------------------------------------
155
158
  xnum = 0.0
156
159
  xden = 1.0
157
- for i in (0...8)
160
+ for i in (0...8)
158
161
  xnum = (xnum + g_p[i]) * z
159
162
  xden = xden * z + g_q[i]
160
163
  end
@@ -164,7 +167,7 @@ module Rubystats
164
167
  # Adjust result for case 0.0 .LT. argument .LT. 1.0
165
168
  # ----------------------------------------------------------------------
166
169
  res /= y1
167
- elsif y1 > y
170
+ elsif y1 > y
168
171
  # ----------------------------------------------------------------------
169
172
  # Adjust result for case 2.0 .LT. argument .LT. 12.0
170
173
  # ----------------------------------------------------------------------
@@ -173,14 +176,14 @@ module Rubystats
173
176
  y += 1
174
177
  end
175
178
  end
176
- else
179
+ else
177
180
  # ----------------------------------------------------------------------
178
181
  # Evaluate for argument .GE. 12.0
179
182
  # ----------------------------------------------------------------------
180
183
  if y <= GAMMA_X_MAX_VALUE
181
184
  ysq = y * y
182
185
  sum = g_c[6]
183
- for i in(0...6)
186
+ for i in(0...6)
184
187
  sum = sum / ysq + g_c[i]
185
188
  sum = sum / y - y + log(SQRT2PI)
186
189
  sum += (y - 0.5) * log(y)
@@ -276,8 +279,8 @@ module Rubystats
276
279
  lg_frtbig = 2.25e76
277
280
  pnt68 = 0.6796875
278
281
 
279
- if x == @log_gamma_cache_x
280
- return @log_gamma_cache_res
282
+ if x == log_gamma_cache_x
283
+ return log_gamma_cache_res
281
284
  end
282
285
 
283
286
  y = x
@@ -359,14 +362,14 @@ module Rubystats
359
362
 
360
363
 
361
364
  # Incomplete Gamma function.
362
- # The computation is based on approximations presented in
365
+ # The computation is based on approximations presented in
363
366
  # Numerical Recipes, Chapter 6.2 (W.H. Press et al, 1992).
364
367
  # @param a require a>=0
365
368
  # @param x require x>=0
366
369
  # @return 0 if x<0, a<=0 or a>2.55E305 to avoid errors and over/underflow
367
370
  # @author Jaco van Kooten
368
371
 
369
- def incomplete_gamma(a, x)
372
+ def incomplete_gamma(a, x)
370
373
  if x <= 0.0 || a <= 0.0 || a > LOG_GAMMA_X_MAX_VALUE
371
374
  return 0.0
372
375
  elsif x < (a + 1.0)
@@ -381,7 +384,7 @@ module Rubystats
381
384
  ap = a
382
385
  del = 1.0 / a
383
386
  sum = del
384
- for n in (1...MAX_ITERATIONS)
387
+ (1...MAX_ITERATIONS).each do
385
388
  ap += 1
386
389
  del *= x / ap
387
390
  sum += del
@@ -393,14 +396,14 @@ module Rubystats
393
396
  end
394
397
 
395
398
  # Author:: Jaco van Kooten
396
- def gamma_fraction(a, x)
399
+ def gamma_fraction(a, x)
397
400
  b = x + 1.0 - a
398
401
  c = 1.0 / XMININ
399
402
  d = 1.0 / b
400
403
  h = d
401
404
  del= 0.0
402
405
  an = 0.0
403
- for i in (1...MAX_ITERATIONS)
406
+ for i in (1...MAX_ITERATIONS)
404
407
  if (del-1.0).abs > PRECISION
405
408
  an = -i * (i - a)
406
409
  b += 2.0
@@ -424,10 +427,10 @@ module Rubystats
424
427
  #
425
428
  # Author:: Jaco van Kooten
426
429
 
427
- def beta(p, q)
430
+ def beta(p, q)
428
431
  if p <= 0.0 || q <= 0.0 || (p + q) > LOG_GAMMA_X_MAX_VALUE
429
432
  return 0.0
430
- else
433
+ else
431
434
  return Math.exp(log_beta(p, q))
432
435
  end
433
436
  end
@@ -437,17 +440,17 @@ module Rubystats
437
440
  # Author:: Jaco van Kooten
438
441
  # Author:: Paul Meagher
439
442
  #
440
- # The computation is based on formulas from Numerical Recipes,
443
+ # The computation is based on formulas from Numerical Recipes,
441
444
  # Chapter 6.4 (W.H. Press et al, 1992).
442
445
 
443
- def incomplete_beta(x, p, q)
446
+ def incomplete_beta(x, p, q)
444
447
  if x <= 0.0
445
448
  return 0.0
446
449
  elsif x >= 1.0
447
450
  return 1.0
448
451
  elsif (p <= 0.0) || (q <= 0.0) || (p + q) > LOG_GAMMA_X_MAX_VALUE
449
452
  return 0.0
450
- else
453
+ else
451
454
  beta_gam = Math.exp( -log_beta(p, q) + p * Math.log(x) + q * Math.log(1.0 - x) )
452
455
  if x < (p + 1.0) / (p + q + 2.0)
453
456
  return beta_gam * beta_fraction(x, p, q) / p
@@ -462,13 +465,13 @@ module Rubystats
462
465
  # Based on an idea from Numerical Recipes (W.H. Press et al, 1992).
463
466
  # Author:: Jaco van Kooten
464
467
 
465
- def beta_fraction(x, p, q)
468
+ def beta_fraction(x, p, q)
466
469
  c = 1.0
467
470
  sum_pq = p + q
468
471
  p_plus = p + 1.0
469
472
  p_minus = p - 1.0
470
473
  h = 1.0 - sum_pq * x / p_plus
471
- if h.abs < XMININ
474
+ if h.abs < XMININ
472
475
  h = XMININ
473
476
  end
474
477
  h = 1.0 / h
@@ -476,7 +479,7 @@ module Rubystats
476
479
  m = 1
477
480
  delta = 0.0
478
481
 
479
- while (m <= MAX_ITERATIONS) && ((delta - 1.0).abs > PRECISION)
482
+ while (m <= MAX_ITERATIONS) && ((delta - 1.0).abs > PRECISION)
480
483
  m2 = 2 * m
481
484
  # even index for d
482
485
  d = m * (q - m) * x / ( (p_minus + m2) * (p + m2))
@@ -563,14 +566,14 @@ module Rubystats
563
566
  # where
564
567
  # P1(s) = degree 6 poly in s
565
568
  # Q1(s) = degree 6 poly in s
566
- #
569
+ #
567
570
  # 3. For x in [1.25,1/0.35(~2.857143)],
568
571
  # erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1)
569
572
  # erf(x) = 1 - erfc(x)
570
573
  # where
571
574
  # R1(z) = degree 7 poly in z, (z=1/x^2)
572
575
  # S1(z) = degree 8 poly in z
573
- #
576
+ #
574
577
  # 4. For x in [1/0.35,28]
575
578
  # erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0
576
579
  # = 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6<x<0
@@ -580,7 +583,7 @@ module Rubystats
580
583
  # where
581
584
  # R2(z) = degree 6 poly in z, (z=1/x^2)
582
585
  # S2(z) = degree 7 poly in z
583
- #
586
+ #
584
587
  # Note1:
585
588
  # To compute exp(-x*x-0.5625+R/S), let s be a single
586
589
  # PRECISION number and s := x then
@@ -602,16 +605,16 @@ module Rubystats
602
605
  # erf(x) = sign(x) *(1 - tiny) (raise inexact)
603
606
  # erfc(x) = tiny*tiny (raise underflow) if x > 0
604
607
  # = 2 - tiny if x<0
605
- #
608
+ #
606
609
  # 7. Special case:
607
610
  # erf(0) = 0, erf(inf) = 1, erf(-inf) = -1,
608
611
  # erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
609
612
  # erfc/erf(NaN) is NaN
610
- #
613
+ #
611
614
  # $efx8 = 1.02703333676410069053e00
612
- #
615
+ #
613
616
  # Coefficients for approximation to erf on [0,0.84375]
614
- #
617
+ #
615
618
 
616
619
  # Error function.
617
620
  # Based on C-code for the error function developed at Sun Microsystems.
@@ -654,7 +657,7 @@ module Rubystats
654
657
  # 0 < |x| < 0.84375
655
658
  if abs_x < 0.84375
656
659
  #|x| < 2**-28
657
- if abs_x < 3.7252902984619141e-9
660
+ if abs_x < 3.7252902984619141e-9
658
661
  retval = abs_x + abs_x * e_efx
659
662
  else
660
663
  s = x * x
@@ -666,12 +669,12 @@ module Rubystats
666
669
  end
667
670
  elsif abs_x < 1.25
668
671
  s = abs_x - 1.0
669
- p = ePa[0] + s * (ePa[1] + s *
670
- (ePa[2] + s * (ePa[3] + s *
672
+ p = ePa[0] + s * (ePa[1] + s *
673
+ (ePa[2] + s * (ePa[3] + s *
671
674
  (ePa[4] + s * (ePa[5] + s * ePa[6])))))
672
675
 
673
- q = 1.0 + s * (eQa[0] + s *
674
- (eQa[1] + s * (eQa[2] + s *
676
+ q = 1.0 + s * (eQa[0] + s *
677
+ (eQa[1] + s * (eQa[2] + s *
675
678
  (eQa[3] + s * (eQa[4] + s * eQa[5])))))
676
679
  retval = e_erx + p / q
677
680
 
@@ -736,27 +739,27 @@ module Rubystats
736
739
  else
737
740
  s = 1.0/(abs_x * abs_x)
738
741
  if abs_x < 2.8571428
739
- r = eRa[0] + s * (eRa[1] + s *
740
- (eRa[2] + s * (eRa[3] + s * (eRa[4] + s *
741
- (eRa[5] + s *(eRa[6] + s * eRa[7])
742
+ r = eRa[0] + s * (eRa[1] + s *
743
+ (eRa[2] + s * (eRa[3] + s * (eRa[4] + s *
744
+ (eRa[5] + s * (eRa[6] + s * eRa[7])
742
745
  )))))
743
746
 
744
- s = 1.0 + s * (eSa[0] + s * (eSa[1] + s *
745
- (eSa[2] + s * (eSa[3] + s * (eSa[4] + s *
747
+ s = 1.0 + s * (eSa[0] + s * (eSa[1] + s *
748
+ (eSa[2] + s * (eSa[3] + s * (eSa[4] + s *
746
749
  (eSa[5] + s * (eSa[6] + s * eSa[7])))))))
747
750
 
748
751
  else
749
- r = eRb[0] + s * (eRb[1] + s *
750
- (eRb[2] + s * (eRb[3] + s * (eRb[4] + s *
752
+ r = eRb[0] + s * (eRb[1] + s *
753
+ (eRb[2] + s * (eRb[3] + s * (eRb[4] + s *
751
754
  (eRb[5] + s * eRb[6])))))
752
755
 
753
- s = 1.0 + s * (eSb[0] + s *
754
- (eSb[1] + s * (eSb[2] + s * (eSb[3] + s *
756
+ s = 1.0 + s * (eSb[0] + s *
757
+ (eSb[1] + s * (eSb[2] + s * (eSb[3] + s *
755
758
  (eSb[4] + s * (eSb[5] + s * eSb[6]))))))
756
759
  end
757
760
  retval = Math.exp(-x * x - 0.5625 + r/s) / abs_x
758
761
  end
759
762
  return ( if x >= 0.0 then retval else 2.0 - retval end )
760
- end
763
+ end
761
764
  end
762
765
  end
@@ -19,6 +19,7 @@ module Rubystats
19
19
  @variance = sigma**2
20
20
  @pdf_denominator = SQRT2PI * Math.sqrt(@variance)
21
21
  @cdf_denominator = SQRT2 * Math.sqrt(@variance)
22
+ @use_last = nil
22
23
  end
23
24
 
24
25
  # Returns the mean of the distribution
@@ -154,7 +154,7 @@ module Rubystats
154
154
  _pdf = pdf(x)
155
155
  if _pdf != 0.0
156
156
  dx = error / _pdf
157
- x_new = x -dx
157
+ x_new = x - dx
158
158
  end
159
159
  # If the NR fails to converge (which for example may be the
160
160
  # case if the initial guess is too rough) we apply a bisection
@@ -1,3 +1,3 @@
1
1
  module Rubystats
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.test_files = `git ls-files -- Appraisals {test,spec,features,gemfiles}/*`.split("\n")
12
12
 
13
13
  s.require_paths = ['lib']
14
- s.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
14
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.3")
15
15
 
16
16
  s.authors = ['Ilya Scharrenbroich', 'Bryan Donovan - http://www.bryandonovan.com', 'Phillip Baker']
17
17
 
@@ -1,14 +1,33 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  require 'minitest/autorun'
3
3
  require 'rubystats/normal_distribution'
4
4
 
5
5
  class TestNormal < MiniTest::Unit::TestCase
6
- def test_simple
6
+ def test_cdf
7
7
 
8
8
  norm = Rubystats::NormalDistribution.new(10,2)
9
9
  cdf = norm.cdf(11)
10
10
 
11
- assert_equal("0.691462461274013",cdf.to_s)
12
- assert_not_nil(norm.rng)
11
+ assert_equal('0.691462461274013', '%.15f' % cdf)
12
+ refute_nil(norm.rng)
13
+ end
14
+
15
+ def test_distribution
16
+ norm = Rubystats::NormalDistribution.new(10.0, 1.0)
17
+ assert_instance_of Float, norm.rng
18
+
19
+ total = 10000
20
+ values =Array.new(total).map{norm.rng.to_i}
21
+ histogram = Hash[*values.group_by{ |v| v }.flat_map{ |k, v| [k, v.size] }]
22
+
23
+ one_sigma = histogram[9] + histogram[10]
24
+ assert_in_epsilon 0.682689492137 , one_sigma.to_f / total, 0.02, 'the 1-sigma-environment should contain 68.3%'
25
+
26
+ two_sigma = one_sigma + histogram[8] + histogram[11]
27
+ assert_in_epsilon 0.954499736104 , two_sigma.to_f / total, 0.01, 'the 2-sigma-environment should contain 95.4%'
28
+
29
+ three_sigma = two_sigma + histogram[7] + histogram[12]
30
+ assert_in_epsilon 0.997300203937 , three_sigma.to_f / total, 0.005, 'the 3-sigma-environment should contain 99.7%'
31
+
13
32
  end
14
33
  end
@@ -15,6 +15,6 @@ class TestNormal < MiniTest::Unit::TestCase
15
15
  assert_equal("0.6914624612740131",cdf.to_s)
16
16
  refute_nil(norm.rng)
17
17
 
18
- expd = ExponentialDistribution.new(2)
18
+ ExponentialDistribution.new(2)
19
19
  end
20
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubystats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Scharrenbroich
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-02-01 00:00:00.000000000 Z
13
+ date: 2016-07-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -95,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
- version: 1.9.2
98
+ version: 1.9.3
99
99
  required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="