Skip to content

Commit

Permalink
Upgrading ruby supported versions (#81)
Browse files Browse the repository at this point in the history
* ci: Run CI on supported stable ruby versions.

* fix: Coerce some denominators to rational when calculating inverse beta.

This change fixes a segmentation error produced by BigDecimal gem on
ruby 3.1.2, where it fails to calculate a big float. It was "fixed"
in BigDecimal 3.1.2, but this version might not be compatible with older
ruby versions.

Reported here: https://bugs.ruby-lang.org/issues/18604 & here: ruby/bigdecimal#220
Potential fix here: ruby/bigdecimal@e236c2e

* spec: Add missing test for BigDecimal and goodness of fit.
  • Loading branch information
estebanz01 authored Oct 31, 2022
1 parent 5ae8a05 commit 40116ea
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ name: Ruby
on: [push]

jobs:
build:
build: # Latest ruby

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Ruby 2.6
- name: Set up Ruby 3.1
uses: ruby/setup-ruby@v1.120.0
with:
ruby-version: 2.6.8
ruby-version: 3.1.2
- name: Build and test with Rake
run: |
gem install bundler
bundle install --jobs 2 --retry 1
bundle exec rake
build_2_7:

runs-on: ubuntu-latest
Expand All @@ -28,13 +28,13 @@ jobs:
- name: Set up Ruby 2.7
uses: ruby/setup-ruby@v1.120.0
with:
ruby-version: 2.7.4
ruby-version: 2.7.6
- name: Build and test with Rake
run: |
gem install bundler
bundle install --jobs 2 --retry 1
bundle exec rake
build_3_0:

runs-on: ubuntu-latest
Expand All @@ -44,7 +44,7 @@ jobs:
- name: Set up Ruby 3.0
uses: ruby/setup-ruby@v1.120.0
with:
ruby-version: 3.0.2
ruby-version: 3.0.4
- name: Build and test with Rake
run: |
gem install bundler
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
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.

Unit test runs under the following ruby versions:
* Ruby 2.5.1.
* Ruby 2.6.0.
* Ruby 2.6.3.
* Ruby 2.6.5.
* Ruby 2.7.
* Ruby 2.7.6.
* Ruby 3.0.4.
* Ruby 3.1.2.

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).

Expand Down
4 changes: 2 additions & 2 deletions lib/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def self.incomplete_beta_function(x, alp, bet)

d = 1.0 + numerator * d
d = tiny if d.abs < tiny
d = 1.0 / d
d = 1.0 / d.to_r

c = 1.0 + numerator / c
c = 1.0 + numerator / c.to_r
c = tiny if c.abs < tiny

cd = (c*d).freeze
Expand Down
25 changes: 25 additions & 0 deletions spec/statistics/bigdecimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@
expect(result[:null]).to be true
expect(result[:alternative]).to be false
end

# The following test is based on the numbers reported in https://github.com/estebanz01/ruby-statistics/issues/78
# which give us a minimum test case scenario where the integral being solved with simpson's rule
# uses zero iterations, raising errors.
it 'performs a goodness of fit test with values that generates small chi statistics' do
observed_counts = [
BigDecimal(481, 1), BigDecimal(483, 1),
BigDecimal(482, 1), BigDecimal(488, 1),
BigDecimal(478, 1), BigDecimal(471, 1),
BigDecimal(477, 1), BigDecimal(479, 1),
BigDecimal(475, 1), BigDecimal(462, 1)
]

expected = BigDecimal(477, 1)

result = {}

expect do
result = StatisticalTest::ChiSquaredTest.goodness_of_fit(0.01, expected, observed_counts)
end.not_to raise_error

expect(result[:p_value].round(4)).to eq(0.9995)
expect(result[:null]).to be true
expect(result[:alternative]).to be false
end
end

context 'when bigdecimal is used in F tests' do
Expand Down

0 comments on commit 40116ea

Please sign in to comment.