ruby-statistics 3.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +44 -11
  3. data/README.md +19 -13
  4. data/bin/console +1 -1
  5. data/lib/math.rb +56 -3
  6. data/lib/{statistics → ruby-statistics}/distribution/bernoulli.rb +1 -1
  7. data/lib/{statistics → ruby-statistics}/distribution/beta.rb +1 -1
  8. data/lib/{statistics → ruby-statistics}/distribution/binomial.rb +1 -1
  9. data/lib/{statistics → ruby-statistics}/distribution/chi_squared.rb +10 -4
  10. data/lib/{statistics → ruby-statistics}/distribution/empirical.rb +1 -1
  11. data/lib/{statistics → ruby-statistics}/distribution/f.rb +1 -1
  12. data/lib/ruby-statistics/distribution/gamma.rb +85 -0
  13. data/lib/{statistics → ruby-statistics}/distribution/geometric.rb +1 -1
  14. data/lib/{statistics → ruby-statistics}/distribution/logseries.rb +1 -1
  15. data/lib/{statistics → ruby-statistics}/distribution/negative_binomial.rb +1 -1
  16. data/lib/{statistics → ruby-statistics}/distribution/normal.rb +1 -1
  17. data/lib/{statistics → ruby-statistics}/distribution/poisson.rb +1 -1
  18. data/lib/{statistics → ruby-statistics}/distribution/t_student.rb +1 -1
  19. data/lib/ruby-statistics/distribution/tables/chi_squared.rb +88 -0
  20. data/lib/{statistics → ruby-statistics}/distribution/uniform.rb +1 -1
  21. data/lib/{statistics → ruby-statistics}/distribution/weibull.rb +1 -1
  22. data/lib/{statistics → ruby-statistics}/distribution.rb +1 -1
  23. data/lib/{statistics → ruby-statistics}/spearman_rank_coefficient.rb +1 -1
  24. data/lib/{statistics → ruby-statistics}/statistical_test/chi_squared_test.rb +1 -1
  25. data/lib/{statistics → ruby-statistics}/statistical_test/f_test.rb +1 -1
  26. data/lib/{statistics → ruby-statistics}/statistical_test/kolmogorov_smirnov_test.rb +1 -1
  27. data/lib/{statistics → ruby-statistics}/statistical_test/t_test.rb +8 -4
  28. data/lib/{statistics → ruby-statistics}/statistical_test/wilcoxon_rank_sum_test.rb +1 -1
  29. data/lib/{statistics → ruby-statistics}/statistical_test.rb +3 -3
  30. data/lib/ruby-statistics/version.rb +3 -0
  31. data/lib/{statistics.rb → ruby-statistics.rb} +2 -2
  32. data/ruby-statistics.gemspec +11 -8
  33. metadata +68 -48
  34. data/lib/statistics/version.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9651bb87c7c5e4b3b0c8d4dcdfce61e1c997dab203f8efac779fbd5894684f1
4
- data.tar.gz: d08439506ff0edecf6366875b5af3352c63bf059bbb7a1ac820d491d803f3ae4
3
+ metadata.gz: e4cb88874b11506652b269c9ffea61763e5ba7a0379b9d6d24eee591800ece00
4
+ data.tar.gz: a7bf474908dc4911075a2d4ac3d6d8805dedd6bfaccb23e0581875bfc0350c47
5
5
  SHA512:
6
- metadata.gz: 60651ef8cd377bda6611b2e8390dbc0697c2088a16addfd125785fa90be770604e551f33127a3d0f3ad2804d9824e59ff5b914598af5daec19c0696fcb5c051e
7
- data.tar.gz: f34f9b4826fa60c71535c26070127fbe71d6ca9a37b14d3c07364579a9c186db8964318bfe9343537c9e2bc93b1e0706c9b707dc922bd64c9a4af77b2fd9d983
6
+ metadata.gz: 9881d53b1068826f9730cc79370c2c80c079177af9a6ea656ab837aab55b3a2ed62844ba2d5122d23291e4aa70049f9c3e3be2ffda5bfbc1e8299a6399f74d06
7
+ data.tar.gz: fe2cccb772734250ba5bc072fa01990b952d5da5b1cbbc4ee8ca4c1ef59e39cc5b468502b9c78d5c6b6cb5842fa76182c293619e8cfddacb895238f6e23c41b0
@@ -1,33 +1,66 @@
1
1
  name: Ruby
2
2
 
3
- on: [push]
3
+ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ build: # Latest ruby
7
7
 
8
8
  runs-on: ubuntu-latest
9
9
 
10
10
  steps:
11
- - uses: actions/checkout@v2.3.4
12
- - name: Set up Ruby 2.6
13
- uses: actions/setup-ruby@v1.1.3
11
+ - uses: actions/checkout@v4
12
+ - name: Set up Ruby 3.3
13
+ uses: ruby/setup-ruby@v1.207.0
14
14
  with:
15
- ruby-version: 2.6.x
15
+ ruby-version: 3.3
16
16
  - name: Build and test with Rake
17
17
  run: |
18
18
  gem install bundler
19
19
  bundle install --jobs 2 --retry 1
20
20
  bundle exec rake
21
- build_2_7:
21
+
22
+ build_3_2:
23
+
24
+ runs-on: ubuntu-latest
25
+
26
+ steps:
27
+ - uses: actions/checkout@v4
28
+ - name: Set up Ruby 3.2
29
+ uses: ruby/setup-ruby@v1.207.0
30
+ with:
31
+ ruby-version: 3.2
32
+ - name: Build and test with Rake
33
+ run: |
34
+ gem install bundler
35
+ bundle install --jobs 2 --retry 1
36
+ bundle exec rake
37
+
38
+ build_3_0:
39
+
40
+ runs-on: ubuntu-latest
41
+
42
+ steps:
43
+ - uses: actions/checkout@v4
44
+ - name: Set up Ruby 3.0
45
+ uses: ruby/setup-ruby@v1.207.0
46
+ with:
47
+ ruby-version: 3.0
48
+ - name: Build and test with Rake
49
+ run: |
50
+ gem install bundler
51
+ bundle install --jobs 2 --retry 1
52
+ bundle exec rake
53
+
54
+ build_3_1:
22
55
 
23
56
  runs-on: ubuntu-latest
24
57
 
25
58
  steps:
26
- - uses: actions/checkout@v2.3.4
27
- - name: Set up Ruby 2.7
28
- uses: actions/setup-ruby@v1.1.3
59
+ - uses: actions/checkout@v4
60
+ - name: Set up Ruby 3.1
61
+ uses: ruby/setup-ruby@v1.207.0
29
62
  with:
30
- ruby-version: 2.7.x
63
+ ruby-version: 3.1
31
64
  - name: Build and test with Rake
32
65
  run: |
33
66
  gem install bundler
data/README.md CHANGED
@@ -1,15 +1,21 @@
1
1
  # Ruby Statistics
2
+ ![](https://github.com/estebanz01/ruby-statistics/actions/workflows/ruby.yml/badge.svg)
2
3
 
3
- ![](https://travis-ci.org/estebanz01/ruby-statistics.svg?branch=master)
4
+ ## Note regarding Versions 3.x and 4.x
5
+ Hola! :wave: right now the gem got an update that introduced a breaking change in master where the Outermost namespace was changed to be `ruby-statistics` instead of `statistics`. This change is in _master_ already and released in version 4.0. If you're relying on branch instead of gem version, you can checkout branch `3.x`. This branch will be supported but no additional features will be added.
6
+
7
+ ## Note regarding Big Decimal support
8
+ This gem also provides basic support for `BigDecimal` and the gem is listed as an optional `development` dependency on the gemspec. Since it's no longer supported as a bundled feature since 3.4.0, the functionality around this gem is going to be limited as well. It is not a required dependency to run or use any of the features developed so far. :grin:
9
+
10
+ ---
4
11
 
5
12
  A basic ruby gem that implements some statistical methods, functions and concepts to be used in any ruby environment without depending on any mathematical software like `R`, `Matlab`, `Octave` or similar.
6
13
 
7
14
  Unit test runs under the following ruby versions:
8
- * Ruby 2.5.1.
9
- * Ruby 2.6.0.
10
- * Ruby 2.6.3.
11
- * Ruby 2.6.5.
12
- * Ruby 2.7.
15
+ * Ruby 3.0.
16
+ * Ruby 3.1.
17
+ * Ruby 3.2.
18
+ * Ruby 3.3.
13
19
 
14
20
  We got the inspiration from the folks at [JStat](https://github.com/jstat/jstat) and some interesting lectures about [Keystroke dynamics](http://www.biometric-solutions.com/keystroke-dynamics.html).
15
21
 
@@ -34,22 +40,22 @@ Or install it yourself as:
34
40
 
35
41
  ## Basic Usage
36
42
 
37
- just require the `statistics` gem in order to load it. If you don't have defined the `Distribution` namespace, the gem will assign an alias, reducing the number of namespaces needed to use a class.
43
+ just require the `ruby-statistics` gem in order to load it. If you don't have defined the `Distribution` namespace, the gem will assign an alias, reducing the number of namespaces needed to use a class.
38
44
 
39
45
  Right now you can load:
40
46
 
41
- * The whole statistics gem. `require 'statistics'`
42
- * A namespace. `require 'statistics/distribution'`
43
- * A class. `require 'statistics/distribution/normal'`
47
+ * The whole statistics gem. `require 'ruby-statistics'`
48
+ * A namespace. `require 'ruby-statistics/distribution'`
49
+ * A class. `require 'ruby-statistics/distribution/normal'`
44
50
 
45
51
  Feel free to use the one that is more convenient to you.
46
52
 
47
53
  ### Hello-World Example
48
54
  ```ruby
49
- require 'statistics'
55
+ require 'ruby-statistics'
50
56
 
51
57
  poisson = Distribution::Poisson.new(l) # Using Distribution alias.
52
- normal = Statistics::Distribution::StandardNormal.new # Using all namespaces.
58
+ normal = RubyStatistics::Distribution::StandardNormal.new # Using all namespaces.
53
59
  ```
54
60
 
55
61
  ## Documentation
@@ -71,7 +77,7 @@ The gem is available as open source under the terms of the [MIT License](http://
71
77
 
72
78
  ## Code of Conduct
73
79
 
74
- Everyone interacting in the Statistics project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/estebanz01/ruby-statistics/blob/master/CODE_OF_CONDUCT.md).
80
+ Everyone interacting in the RubyStatistics project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/estebanz01/ruby-statistics/blob/master/CODE_OF_CONDUCT.md).
75
81
 
76
82
  ## Contact
77
83
 
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "statistics"
4
+ require "ruby-statistics"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
data/lib/math.rb CHANGED
@@ -45,12 +45,65 @@ module Math
45
45
  end
46
46
 
47
47
  def self.lower_incomplete_gamma_function(s, x)
48
+ base_iterator = x.round(1)
49
+ base_iterator += 1 if x < 1.0 && !x.zero?
50
+
48
51
  # The greater the iterations, the better. That's why we are iterating 10_000 * x times
49
- self.simpson_rule(0, x.to_r, (10_000 * x.round).round) do |t|
52
+ iterator = (10_000 * base_iterator).round
53
+ iterator = 100_000 if iterator.zero?
54
+
55
+ self.simpson_rule(0, x.to_r, iterator) do |t|
50
56
  (t ** (s - 1)) * Math.exp(-t)
51
57
  end
52
58
  end
53
59
 
60
+ # Algorithm implementation translated from the ASA147 C++ version https://people.sc.fsu.edu/~jburkardt/cpp_src/asa147/asa147.html
61
+ # translated from FORTRAN by John Burkardt. Original algorithm written by Chi Leung Lau.
62
+ # It contains a modification on the error and underflow parameters to use maximum available float number
63
+ # and it performs the series using `Rational` objects to avoid memory exhaustion and reducing precision errors.
64
+ #
65
+ # This algorithm is licensed with MIT license.
66
+ #
67
+ # Reference:
68
+ #
69
+ # Chi Leung Lau,
70
+ # Algorithm AS 147:
71
+ # A Simple Series for the Incomplete Gamma Integral,
72
+ # Applied Statistics,
73
+ # Volume 29, Number 1, 1980, pages 113-114.
74
+ def self.normalised_lower_incomplete_gamma_function(s, x)
75
+ return 0.0 if s.negative? || x.zero? || x.negative?
76
+
77
+ # e = 1.0e-308
78
+ # uflo = 1.0e-47
79
+ e = Float::MIN
80
+ uflo = Float::MIN
81
+
82
+ lgamma, sign = Math.lgamma(s + 1.0)
83
+ arg = s * Math.log(x) - (sign * lgamma) - x
84
+
85
+ return 0.0 if arg < Math.log(uflo)
86
+
87
+ f = Math.exp(arg).to_r
88
+
89
+ return 0.0 if f.zero?
90
+
91
+ c = 1r
92
+ value = 1r
93
+ a = s.to_r
94
+ rational_x = x.to_r
95
+
96
+ loop do
97
+ a += 1r
98
+ c = c * (rational_x / a)
99
+ value += c
100
+
101
+ break if c <= (e * value)
102
+ end
103
+
104
+ (value * f).to_f
105
+ end
106
+
54
107
  def self.beta_function(x, y)
55
108
  return 1 if x == 1 && y == 1
56
109
 
@@ -101,9 +154,9 @@ module Math
101
154
 
102
155
  d = 1.0 + numerator * d
103
156
  d = tiny if d.abs < tiny
104
- d = 1.0 / d
157
+ d = 1.0 / d.to_r
105
158
 
106
- c = 1.0 + numerator / c
159
+ c = 1.0 + numerator / c.to_r
107
160
  c = tiny if c.abs < tiny
108
161
 
109
162
  cd = (c*d).freeze
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Bernoulli
4
4
  def self.density_function(n, p)
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Beta
4
4
  attr_accessor :alpha, :beta
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Binomial
4
4
  attr_accessor :number_of_trials, :probability_per_trial
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class ChiSquared
4
4
  attr_accessor :degrees_of_freedom
@@ -10,8 +10,14 @@ module Statistics
10
10
  end
11
11
 
12
12
  def cumulative_function(value)
13
- k = degrees_of_freedom/2.0
14
- Math.lower_incomplete_gamma_function(k, value/2.0)/Math.gamma(k)
13
+ if degrees_of_freedom == 2
14
+ # Special case where DF = 2 https://en.wikipedia.org/wiki/Chi-squared_distribution#Cumulative_distribution_function
15
+ 1.0 - Math.exp((-1.0 * value / 2.0))
16
+ else
17
+ k = degrees_of_freedom/2.0
18
+ # Math.lower_incomplete_gamma_function(k, value/2.0)/Math.gamma(k)
19
+ Math.normalised_lower_incomplete_gamma_function(k, value / 2.0)
20
+ end
15
21
  end
16
22
 
17
23
  def density_function(value)
@@ -22,7 +28,7 @@ module Statistics
22
28
  left_down = (2 ** common) * Math.gamma(common)
23
29
  right = (value ** (common - 1)) * Math.exp(-(value/2.0))
24
30
 
25
- (1.0/left_down) * right
31
+ right / left_down
26
32
  end
27
33
 
28
34
  def mode
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Empirical
4
4
  attr_accessor :samples
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class F
4
4
  attr_accessor :d1, :d2 # Degrees of freedom #1 and #2
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyStatistics
4
+ module Distribution
5
+ class Gamma
6
+ attr_reader :shape, :scale, :rate
7
+
8
+ def initialize(shape:, scale: nil)
9
+ @shape = shape
10
+ @scale = scale
11
+
12
+ # If the scale is nil, it means we want the distribution to behave with a rate parameter
13
+ # instead of a scale parameter
14
+ @rate = if scale.nil?
15
+ 1.0 / shape
16
+ else
17
+ nil
18
+ end
19
+ end
20
+
21
+ def as_rate?
22
+ scale.nil?
23
+ end
24
+
25
+ def mean
26
+ if as_rate?
27
+ self.shape / self.rate
28
+ else
29
+ self.shape * self.scale
30
+ end
31
+ end
32
+
33
+ def mode
34
+ return 0.0 if self.shape < 1.0
35
+
36
+ if as_rate?
37
+ (self.shape - 1.0) / self.rate
38
+ else
39
+ (self.shape - 1.0) * self.scale
40
+ end
41
+ end
42
+
43
+ def variance
44
+ if as_rate?
45
+ self.shape / (self.rate ** 2.0)
46
+ else
47
+ self.shape * (self.scale ** 2.0)
48
+ end
49
+ end
50
+
51
+ def skewness
52
+ 2.0 / Math.sqrt(self.shape)
53
+ end
54
+
55
+ def density_function(x)
56
+ euler = if as_rate?
57
+ Math.exp(- self.rate * x)
58
+ else
59
+ Math.exp(-x / self.scale.to_r)
60
+ end
61
+
62
+ left = if as_rate?
63
+ (self.rate ** self.shape).to_r / Math.gamma(self.shape).to_r
64
+ else
65
+ 1r / (Math.gamma(self.shape).to_r * (self.scale ** self.shape).to_r)
66
+ end
67
+
68
+ left * (x ** (self.shape - 1)) * euler
69
+ end
70
+
71
+ def cumulative_function(x)
72
+ upper = if as_rate?
73
+ self.rate * x.to_r
74
+ else
75
+ x / self.scale.to_r
76
+ end
77
+
78
+ # left = 1.0 / Math.gamma(self.shape)
79
+ # right = Math.lower_incomplete_gamma_function(self.shape, upper)
80
+ # left * right
81
+ Math.normalised_lower_incomplete_gamma_function(self.shape, upper)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Geometric
4
4
  attr_accessor :probability_of_success, :always_success_allowed
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class LogSeries
4
4
  def self.density_function(k, p)
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class NegativeBinomial
4
4
  attr_accessor :number_of_failures, :probability_per_trial
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Normal
4
4
  attr_accessor :mean, :standard_deviation, :variance
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Poisson
4
4
  attr_accessor :expected_number_of_occurrences
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class TStudent
4
4
  attr_accessor :degrees_of_freedom
@@ -0,0 +1,88 @@
1
+ module RubyStatistics
2
+ module Distribution
3
+ module Tables
4
+ class ChiSquared
5
+ # Values retrieved from the following table provided by the University of Arizona.
6
+ # https://math.arizona.edu/~jwatkins/chi-square-table.pdf
7
+ TABLE = [
8
+ [0.000, 0.000, 0.001, 0.004, 0.016, 2.706, 3.841, 5.024, 6.635, 7.879],
9
+ [0.010, 0.020, 0.051, 0.103, 0.211, 4.605, 5.991, 7.378, 9.210, 10.597],
10
+ [0.072, 0.115, 0.216, 0.352, 0.584, 6.251, 7.815, 9.348, 11.345, 12.838],
11
+ [0.207, 0.297, 0.484, 0.711, 1.064, 7.779, 9.488, 11.143, 13.277, 14.860],
12
+ [0.412, 0.554, 0.831, 1.145, 1.610, 9.236, 11.070, 12.833, 15.086, 16.750],
13
+ [0.676, 0.872, 1.237, 1.635, 2.204, 10.645, 12.592, 14.449, 16.812, 18.548],
14
+ [0.989, 1.239, 1.690, 2.167, 2.833, 12.017, 14.067, 16.013, 18.475, 20.278],
15
+ [1.344, 1.646, 2.180, 2.733, 3.490, 13.362, 15.507, 17.535, 20.090, 21.955],
16
+ [1.735, 2.088, 2.700, 3.325, 4.168, 14.684, 16.919, 19.023, 21.666, 23.589],
17
+ [2.156, 2.558, 3.247, 3.940, 4.865, 15.987, 18.307, 20.483, 23.209, 25.188],
18
+ [2.603, 3.053, 3.816, 4.575, 5.578, 17.275, 19.675, 21.920, 24.725, 26.757],
19
+ [3.074, 3.571, 4.404, 5.226, 6.304, 18.549, 21.026, 23.337, 26.217, 28.300],
20
+ [3.565, 4.107, 5.009, 5.892, 7.042, 19.812, 22.362, 24.736, 27.688, 29.819],
21
+ [4.075, 4.660, 5.629, 6.571, 7.790, 21.064, 23.685, 26.119, 29.141, 31.319],
22
+ [4.601, 5.229, 6.262, 7.261, 8.547, 22.307, 24.996, 27.488, 30.578, 32.801],
23
+ [5.142, 5.812, 6.908, 7.962, 9.312, 23.542, 26.296, 28.845, 32.000, 34.267],
24
+ [5.697, 6.408, 7.564, 8.672, 10.085, 24.769, 27.587, 30.191, 33.409, 35.718],
25
+ [6.265, 7.015, 8.231, 9.390, 10.865, 25.989, 28.869, 31.526, 34.805, 37.156],
26
+ [6.844, 7.633, 8.907, 10.117, 11.651, 27.204, 30.144, 32.852, 36.191, 38.582],
27
+ [7.434, 8.260, 9.591, 10.851, 12.443, 28.412, 31.410, 34.170, 37.566, 39.997],
28
+ [8.034, 8.897, 10.283, 11.591, 13.240, 29.615, 32.671, 35.479, 38.932, 41.401],
29
+ [8.643, 9.542, 10.982, 12.338, 14.041, 30.813, 33.924, 36.781, 40.289, 42.796],
30
+ [9.260, 10.196, 11.689, 13.091, 14.848, 32.007, 35.172, 38.076, 41.638, 44.181],
31
+ [9.886, 10.856, 12.401, 13.848, 15.659, 33.196, 36.415, 39.364, 42.980, 45.559],
32
+ [10.520, 11.524, 13.120, 14.611, 16.473, 34.382, 37.652, 40.646, 44.314, 46.928],
33
+ [11.160, 12.198, 13.844, 15.379, 17.292, 35.563, 38.885, 41.923, 45.642, 48.290],
34
+ [11.808, 12.879, 14.573, 16.151, 18.114, 36.741, 40.113, 43.195, 46.963, 49.645],
35
+ [12.461, 13.565, 15.308, 16.928, 18.939, 37.916, 41.337, 44.461, 48.278, 50.993],
36
+ [13.121, 14.256, 16.047, 17.708, 19.768, 39.087, 42.557, 45.722, 49.588, 52.336],
37
+ [13.787, 14.953, 16.791, 18.493, 20.599, 40.256, 43.773, 46.979, 50.892, 53.672],
38
+ [20.707, 22.164, 24.433, 26.509, 29.051, 51.805, 55.758, 59.342, 63.691, 66.766],
39
+ [27.991, 29.707, 32.357, 34.764, 37.689, 63.167, 67.505, 71.420, 76.154, 79.490],
40
+ [35.534, 37.485, 40.482, 43.188, 46.459, 74.397, 79.082, 83.298, 88.379, 91.952],
41
+ [43.275, 45.442, 48.758, 51.739, 55.329, 85.527, 90.531, 95.023, 100.425, 104.215],
42
+ [51.172, 53.540, 57.153, 60.391, 64.278, 96.578, 101.879, 106.629, 112.329, 116.321],
43
+ [59.196, 61.754, 65.647, 69.126, 73.291, 107.565, 113.145, 118.136, 124.116, 128.299],
44
+ [67.328, 70.065, 74.222, 77.929, 82.358, 118.498, 124.342, 129.561, 135.807, 140.169]
45
+ ].freeze
46
+
47
+ ALPHA_HEADER =
48
+ {
49
+ 0.995 => 0,
50
+ 0.990 => 1,
51
+ 0.975 => 2,
52
+ 0.95 => 3,
53
+ 0.9 => 4,
54
+ 0.1 => 5,
55
+ 0.05 => 6,
56
+ 0.025 => 7,
57
+ 0.01 => 8,
58
+ 0.005 => 9
59
+ }.freeze
60
+
61
+ DEGREES_OF_FREEDOM = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 40, 50, 60, 70, 80, 90, 100].freeze
62
+
63
+ # return an array of the alpha values in correct order
64
+ # i.e. 0.995 -> 0, 0.990 -> 1 etc
65
+ def self.alpha_values
66
+ ALPHA_HEADER.keys
67
+ end
68
+
69
+ # Checks if a valid alpha value is passed to look up values
70
+ def self.valid_alpha?(alpha)
71
+ self.alpha_values.include?(alpha)
72
+ end
73
+
74
+ # Return a whole column of the distribution table for a certain alpha value
75
+ def self.alpha_column(alpha)
76
+ raise "Undefined alpha value." unless self.valid_alpha?(alpha)
77
+
78
+ # return an array of hashes for an alpha value
79
+ # including the degree of freedom for each critical value
80
+ # i.e. [{df: 1, critival_value: x},...]
81
+ TABLE.map.with_index do |row, index|
82
+ { df: DEGREES_OF_FREEDOM[index], critical_value: row[ALPHA_HEADER[alpha]] }
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Uniform
4
4
  attr_accessor :left, :right
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module Distribution
3
3
  class Weibull
4
4
  attr_accessor :shape, :scale # k and lambda
@@ -1,6 +1,6 @@
1
1
  Dir[File.dirname(__FILE__) + '/distribution/**/*.rb'].each {|file| require file }
2
2
 
3
- module Statistics
3
+ module RubyStatistics
4
4
  module Distribution
5
5
  end
6
6
  end
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  class SpearmanRankCoefficient
3
3
  def self.rank(data:, return_ranks_only: true)
4
4
  descending_order_data = data.sort { |a, b| b <=> a }
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module StatisticalTest
3
3
  class ChiSquaredTest
4
4
  def self.chi_statistic(expected, observed)
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module StatisticalTest
3
3
  class FTest
4
4
  # This method calculates the one-way ANOVA F-test statistic.
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module StatisticalTest
3
3
  class KolmogorovSmirnovTest
4
4
  # Common alpha, and critical D are calculated following formulas from: https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test#Two-sample_Kolmogorov%E2%80%93Smirnov_test
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module StatisticalTest
3
3
  class TTest
4
4
  # Errors for Zero std
@@ -74,10 +74,14 @@ module Statistics
74
74
 
75
75
  t_score = (differences.mean - 0)/down.to_r
76
76
 
77
- probability = Distribution::TStudent.new(degrees_of_freedom).cumulative_function(t_score)
77
+ t_distribution = Distribution::TStudent.new(degrees_of_freedom)
78
+ probability = t_distribution.cumulative_function(t_score)
78
79
 
79
- p_value = 1 - probability
80
- p_value *= 2 if tails == :two_tail
80
+ p_value = if tails == :two_tail
81
+ 2 * (1 - t_distribution.cumulative_function(t_score.abs))
82
+ else
83
+ 1 - probability
84
+ end
81
85
 
82
86
  { t_score: t_score,
83
87
  probability: probability,
@@ -1,4 +1,4 @@
1
- module Statistics
1
+ module RubyStatistics
2
2
  module StatisticalTest
3
3
  class WilcoxonRankSumTest
4
4
  def rank(elements)
@@ -1,11 +1,11 @@
1
1
  Dir[File.dirname(__FILE__) + '/statistical_test/**/*.rb'].each {|file| require file }
2
2
 
3
- module Statistics
3
+ module RubyStatistics
4
4
  module StatisticalTest
5
5
  end
6
6
  end
7
7
 
8
8
  # If StatisticalTest is not defined, setup alias.
9
- if defined?(Statistics) && !(defined?(StatisticalTest))
10
- StatisticalTest = Statistics::StatisticalTest
9
+ if defined?(RubyStatistics) && !(defined?(StatisticalTest))
10
+ StatisticalTest = RubyStatistics::StatisticalTest
11
11
  end
@@ -0,0 +1,3 @@
1
+ module RubyStatistics
2
+ VERSION = "4.1.0"
3
+ end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/enumerable'
2
2
  require File.dirname(__FILE__) + '/math'
3
- Dir[ File.dirname(__FILE__) + '/statistics/**/*.rb'].each {|file| require file }
3
+ Dir[ File.dirname(__FILE__) + '/ruby-statistics/**/*.rb'].each {|file| require file }
4
4
 
5
- module Statistics
5
+ module RubyStatistics
6
6
  # Your code goes here...
7
7
  end
@@ -1,13 +1,13 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "statistics/version"
4
+ require "ruby-statistics/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "ruby-statistics"
8
- spec.version = Statistics::VERSION
8
+ spec.version = RubyStatistics::VERSION
9
9
  spec.authors = ["esteban zapata"]
10
- spec.email = ["estebanz01@outlook.com"]
10
+ spec.email = ["ruby@estebanz.email"]
11
11
 
12
12
  spec.summary = %q{A ruby gem for som specific statistics. Inspired by the jStat js library.}
13
13
  spec.description = %q{This gem is intended to accomplish the same purpose as jStat js library:
@@ -27,9 +27,12 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_development_dependency "rake", '>= 12.0.0', '~> 13.0'
31
- spec.add_development_dependency "rspec", '>= 3.6.0'
32
- spec.add_development_dependency "grb", '~> 0.4.1', '>= 0.4.1'
33
- spec.add_development_dependency 'byebug', '>= 9.1.0'
34
- spec.add_development_dependency 'pry'
30
+ # Minimum required ruby version
31
+ spec.required_ruby_version = '>= 3.0'
32
+
33
+ spec.add_development_dependency "rake", '~> 13.0', '>= 12.0.0'
34
+ spec.add_development_dependency "rspec", '~> 3.13', '>= 3.10.0'
35
+ spec.add_development_dependency 'byebug', '~> 11.1', '>= 11.1.0'
36
+ spec.add_development_dependency 'pry', '~> 0.14', '>= 0.14.0'
37
+ spec.add_development_dependency 'bigdecimal', '~> 3.1', '>= 3.1.9'
35
38
  end
metadata CHANGED
@@ -1,97 +1,115 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-statistics
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - esteban zapata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-08 00:00:00.000000000 Z
11
+ date: 2025-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 12.0.0
20
17
  - - "~>"
21
18
  - !ruby/object:Gem::Version
22
19
  version: '13.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 12.0.0
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 12.0.0
30
27
  - - "~>"
31
28
  - !ruby/object:Gem::Version
32
29
  version: '13.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 12.0.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rspec
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.13'
37
40
  - - ">="
38
41
  - !ruby/object:Gem::Version
39
- version: 3.6.0
42
+ version: 3.10.0
40
43
  type: :development
41
44
  prerelease: false
42
45
  version_requirements: !ruby/object:Gem::Requirement
43
46
  requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.13'
44
50
  - - ">="
45
51
  - !ruby/object:Gem::Version
46
- version: 3.6.0
52
+ version: 3.10.0
47
53
  - !ruby/object:Gem::Dependency
48
- name: grb
54
+ name: byebug
49
55
  requirement: !ruby/object:Gem::Requirement
50
56
  requirements:
51
57
  - - "~>"
52
58
  - !ruby/object:Gem::Version
53
- version: 0.4.1
59
+ version: '11.1'
54
60
  - - ">="
55
61
  - !ruby/object:Gem::Version
56
- version: 0.4.1
62
+ version: 11.1.0
57
63
  type: :development
58
64
  prerelease: false
59
65
  version_requirements: !ruby/object:Gem::Requirement
60
66
  requirements:
61
67
  - - "~>"
62
68
  - !ruby/object:Gem::Version
63
- version: 0.4.1
69
+ version: '11.1'
64
70
  - - ">="
65
71
  - !ruby/object:Gem::Version
66
- version: 0.4.1
72
+ version: 11.1.0
67
73
  - !ruby/object:Gem::Dependency
68
- name: byebug
74
+ name: pry
69
75
  requirement: !ruby/object:Gem::Requirement
70
76
  requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.14'
71
80
  - - ">="
72
81
  - !ruby/object:Gem::Version
73
- version: 9.1.0
82
+ version: 0.14.0
74
83
  type: :development
75
84
  prerelease: false
76
85
  version_requirements: !ruby/object:Gem::Requirement
77
86
  requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.14'
78
90
  - - ">="
79
91
  - !ruby/object:Gem::Version
80
- version: 9.1.0
92
+ version: 0.14.0
81
93
  - !ruby/object:Gem::Dependency
82
- name: pry
94
+ name: bigdecimal
83
95
  requirement: !ruby/object:Gem::Requirement
84
96
  requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '3.1'
85
100
  - - ">="
86
101
  - !ruby/object:Gem::Version
87
- version: '0'
102
+ version: 3.1.9
88
103
  type: :development
89
104
  prerelease: false
90
105
  version_requirements: !ruby/object:Gem::Requirement
91
106
  requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.1'
92
110
  - - ">="
93
111
  - !ruby/object:Gem::Version
94
- version: '0'
112
+ version: 3.1.9
95
113
  description: |-
96
114
  This gem is intended to accomplish the same purpose as jStat js library:
97
115
  to provide ruby with statistical capabilities without the need
@@ -99,7 +117,7 @@ description: |-
99
117
  and capabilities are an implementation from other authors and are
100
118
  referenced properly in the class/method.
101
119
  email:
102
- - estebanz01@outlook.com
120
+ - ruby@estebanz.email
103
121
  executables: []
104
122
  extensions: []
105
123
  extra_rdoc_files: []
@@ -120,30 +138,32 @@ files:
120
138
  - bin/setup
121
139
  - lib/enumerable.rb
122
140
  - lib/math.rb
123
- - lib/statistics.rb
124
- - lib/statistics/distribution.rb
125
- - lib/statistics/distribution/bernoulli.rb
126
- - lib/statistics/distribution/beta.rb
127
- - lib/statistics/distribution/binomial.rb
128
- - lib/statistics/distribution/chi_squared.rb
129
- - lib/statistics/distribution/empirical.rb
130
- - lib/statistics/distribution/f.rb
131
- - lib/statistics/distribution/geometric.rb
132
- - lib/statistics/distribution/logseries.rb
133
- - lib/statistics/distribution/negative_binomial.rb
134
- - lib/statistics/distribution/normal.rb
135
- - lib/statistics/distribution/poisson.rb
136
- - lib/statistics/distribution/t_student.rb
137
- - lib/statistics/distribution/uniform.rb
138
- - lib/statistics/distribution/weibull.rb
139
- - lib/statistics/spearman_rank_coefficient.rb
140
- - lib/statistics/statistical_test.rb
141
- - lib/statistics/statistical_test/chi_squared_test.rb
142
- - lib/statistics/statistical_test/f_test.rb
143
- - lib/statistics/statistical_test/kolmogorov_smirnov_test.rb
144
- - lib/statistics/statistical_test/t_test.rb
145
- - lib/statistics/statistical_test/wilcoxon_rank_sum_test.rb
146
- - lib/statistics/version.rb
141
+ - lib/ruby-statistics.rb
142
+ - lib/ruby-statistics/distribution.rb
143
+ - lib/ruby-statistics/distribution/bernoulli.rb
144
+ - lib/ruby-statistics/distribution/beta.rb
145
+ - lib/ruby-statistics/distribution/binomial.rb
146
+ - lib/ruby-statistics/distribution/chi_squared.rb
147
+ - lib/ruby-statistics/distribution/empirical.rb
148
+ - lib/ruby-statistics/distribution/f.rb
149
+ - lib/ruby-statistics/distribution/gamma.rb
150
+ - lib/ruby-statistics/distribution/geometric.rb
151
+ - lib/ruby-statistics/distribution/logseries.rb
152
+ - lib/ruby-statistics/distribution/negative_binomial.rb
153
+ - lib/ruby-statistics/distribution/normal.rb
154
+ - lib/ruby-statistics/distribution/poisson.rb
155
+ - lib/ruby-statistics/distribution/t_student.rb
156
+ - lib/ruby-statistics/distribution/tables/chi_squared.rb
157
+ - lib/ruby-statistics/distribution/uniform.rb
158
+ - lib/ruby-statistics/distribution/weibull.rb
159
+ - lib/ruby-statistics/spearman_rank_coefficient.rb
160
+ - lib/ruby-statistics/statistical_test.rb
161
+ - lib/ruby-statistics/statistical_test/chi_squared_test.rb
162
+ - lib/ruby-statistics/statistical_test/f_test.rb
163
+ - lib/ruby-statistics/statistical_test/kolmogorov_smirnov_test.rb
164
+ - lib/ruby-statistics/statistical_test/t_test.rb
165
+ - lib/ruby-statistics/statistical_test/wilcoxon_rank_sum_test.rb
166
+ - lib/ruby-statistics/version.rb
147
167
  - ruby-statistics.gemspec
148
168
  homepage: https://github.com/estebanz01/ruby-statistics
149
169
  licenses:
@@ -157,14 +177,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
157
177
  requirements:
158
178
  - - ">="
159
179
  - !ruby/object:Gem::Version
160
- version: '0'
180
+ version: '3.0'
161
181
  required_rubygems_version: !ruby/object:Gem::Requirement
162
182
  requirements:
163
183
  - - ">="
164
184
  - !ruby/object:Gem::Version
165
185
  version: '0'
166
186
  requirements: []
167
- rubygems_version: 3.1.4
187
+ rubygems_version: 3.5.11
168
188
  signing_key:
169
189
  specification_version: 4
170
190
  summary: A ruby gem for som specific statistics. Inspired by the jStat js library.
@@ -1,3 +0,0 @@
1
- module Statistics
2
- VERSION = "3.0.0"
3
- end