diff --git a/README.md b/README.md index 704a925..6bef186 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # stats An expressive implementation of statistical distributions. -Compatible with crystal v0.27.0. +Compatible with crystal v0.34.0. ## Installation @@ -94,8 +94,6 @@ Math.factorial(4) # => 24 ### Quartiles & Boxplot -*Note: not big compatible yet* - ```crystal [1, 3, 5].first_quartile # => 2.0 (alias of lower_quartile) [1, 3, 5].second_quartile # => 3.0 (alias of median) @@ -144,3 +142,4 @@ arr.upper_outliers(3) # => [1337] ## Contributors - [Nephos](https://github.com/Nephos) Arthur Poulet - creator, maintainer +- [pyrokar](https://github.com/pyrokar) Gunter Solf - contributer diff --git a/shard.yml b/shard.yml index faf34fb..d849b2b 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: stats -version: 0.3.0 +version: 0.3.1 authors: - Arthur Poulet diff --git a/spec/math/quartile.cr b/spec/math/quartile.cr index 57a22ff..4476cc7 100644 --- a/spec/math/quartile.cr +++ b/spec/math/quartile.cr @@ -13,18 +13,17 @@ module Math::Quartile arr.iqr.should eq 2.0 end - # TODO - # it "big" do - # arr = [BigInt.new(1), BigFloat.new(3), 5] - # - # arr.first_quartile.should eq 2.0 - # arr.second_quartile.should eq 3.0 - # arr.third_quartile.should eq 4.0 - # - # arr.quartiles.should eq [2.0, 3.0, 4.0] - # - # arr.iqr.should eq 2.0 - # end + it "big" do + arr = [BigInt.new(1), BigFloat.new(3), 5] + + arr.first_quartile.should eq 2.0 + arr.second_quartile.should eq 3.0 + arr.third_quartile.should eq 4.0 + + arr.quartiles.should eq [2.0, 3.0, 4.0] + + arr.iqr.should eq 2.0 + end it "odd size input" do arr = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49] diff --git a/src/lib/math/factorial.cr b/src/lib/math/factorial.cr index da0d7fb..b27773d 100644 --- a/src/lib/math/factorial.cr +++ b/src/lib/math/factorial.cr @@ -4,7 +4,7 @@ module Math def factorial(n : Number) raise Math::DomainError.new "The argument must be a natural (out of domain -- factorial)" if n < 0 return (typeof(n)).new(1) if n == 0 - return (1..n).to_a.reduce((typeof(n)).new(1)) { |a, b| a * b } + return (1..n).to_a.reduce((typeof(n)).new(1)) { |a, b| a &* b } end end end diff --git a/src/lib/math/macd.cr b/src/lib/math/macd.cr index 4f90e9e..189bbce 100644 --- a/src/lib/math/macd.cr +++ b/src/lib/math/macd.cr @@ -14,9 +14,9 @@ module Math::MACD # MACD computation if n is odd private def macd_odd(n) map_with_index do |_, i| - next if i < n / 2 - next if i + n / 2 >= size - range = (i - n / 2)..(i + n / 2) + next if i < n // 2 + next if i + n // 2 >= size + range = (i - n // 2)..(i + n // 2) self[range].mean end end diff --git a/src/lib/math/median.cr b/src/lib/math/median.cr index 0118c4b..6d25827 100644 --- a/src/lib/math/median.cr +++ b/src/lib/math/median.cr @@ -1,10 +1,10 @@ module Math::Median - def median + def median : Number return 0.0_f64 if empty? sorted = sort size = size() - return sorted[(size - 1) / 2] / 1.0 if size.odd? - (sorted[(size / 2) - 1] + sorted[size / 2]) / 2.0 + return sorted[(size - 1) // 2] / 1.0 if size.odd? + (sorted[(size // 2) - 1] + sorted[size // 2]) / 2.0 end end diff --git a/src/lib/math/quartile.cr b/src/lib/math/quartile.cr index a92f56c..5f02d55 100644 --- a/src/lib/math/quartile.cr +++ b/src/lib/math/quartile.cr @@ -4,57 +4,57 @@ # https://en.wikipedia.org/wiki/Quartile#Method_2 # module Math::Quartile - def lower_quartile : Float64 - return 0.0_f64 if empty? + def lower_quartile + return 0 if empty? m = self.median lower_half = self.select { |i| i <= m } lower_half.median end # alias - def first_quartile : Float64 + def first_quartile lower_quartile end # alias - def second_quartile : Float64 + def second_quartile median end - def upper_quartile : Float64 - return 0.0_f64 if empty? + def upper_quartile + return 0 if empty? m = self.median upper_half = self.select { |i| i >= m } upper_half.median end # alias - def third_quartile : Float64 + def third_quartile upper_quartile end - def quartiles : Array(Float64) + def quartiles [first_quartile, second_quartile, third_quartile] end - def iqr : Float64 + def iqr third_quartile - first_quartile end # alias - def interquartile_range : Float64 + def interquartile_range iqr end - def lower_fence(k : Number = 1.5) : Float64 + def lower_fence(k : Number = 1.5) lower_quartile - k * iqr end - def upper_fence(k : Number = 1.5) : Float64 + def upper_fence(k : Number = 1.5) upper_quartile + k * iqr end - def lower_outliers(k : Number = 1.5) : Array + def lower_outliers(k : Number = 1.5) lf = lower_fence k self.select { |i| i < lf } end