Browse Source

Merge branch 'median_quartiles' of git://github.com/pyrokar/stats

master
Arthur POULET 3 years ago
parent
commit
5b1f28512e
No known key found for this signature in database GPG Key ID: FC154EA63FF5D8BC
  1. 1
      README.md
  2. 19
      spec/math/quartile.cr
  3. 7
      src/lib/math/quartile.cr

1
README.md

@ -96,6 +96,7 @@ Math.factorial(4) # => 24
[1, 3, 5].first_quartile # => 2.0 (alias of lower_quartile)
[1, 3, 5].second_quartile # => 3.0 (alias of median)
[1, 3, 5].third_quartile # => 4.0 (alias of upper_quartile)
[1, 3, 5].quartiles # => [2.0, 3.0, 4.0] ([Q1, Q2, Q3])
```
```crystal

19
spec/math/quartile.cr

@ -5,6 +5,9 @@ module Math::Quartile
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
@ -14,6 +17,9 @@ module Math::Quartile
arr.first_quartile.should eq 25.5
arr.second_quartile.should eq 40
arr.third_quartile.should eq 42.5
arr.quartiles.should eq [25.5, 40, 42.5]
arr.iqr.should eq 17.0
end
@ -23,6 +29,9 @@ module Math::Quartile
arr.first_quartile.should eq 15.0
arr.second_quartile.should eq 37.5
arr.third_quartile.should eq 40.0
arr.quartiles.should eq [15, 37.5, 40]
arr.iqr.should eq 25.0
end
@ -32,6 +41,9 @@ module Math::Quartile
arr.first_quartile.should eq 31
arr.second_quartile.should eq 87
arr.third_quartile.should eq 119
arr.quartiles.should eq [31, 87, 119]
arr.iqr.should eq 88
end
@ -42,6 +54,9 @@ module Math::Quartile
arr.first_quartile.should eq 25.5
arr.second_quartile.should eq 40
arr.third_quartile.should eq 42.5
arr.quartiles.should eq [25.5, 40, 42.5]
arr.iqr.should eq 17
end
@ -51,6 +66,9 @@ module Math::Quartile
arr.first_quartile.should eq 3.5
arr.second_quartile.should eq 6.5
arr.third_quartile.should eq 14.5
arr.quartiles.should eq [3.5, 6.5, 14.5]
arr.iqr.should eq 11.0
arr.lower_fence.should eq -13.0
@ -61,5 +79,6 @@ module Math::Quartile
arr.upper_fence(3).should eq 47.5
arr.upper_outliers(3).should eq [1337]
end
end

7
src/lib/math/quartile.cr

@ -1,4 +1,4 @@
# There are several methods for computing the quartiles.
# There are several methods for computing the quartiles of an array.
#
# This library utilizes the method proposed by John Tukey
# https://en.wikipedia.org/wiki/Quartile#Method_2
@ -28,10 +28,15 @@ module Math::Quartile
upper_half.median
end
# alias
def third_quartile : Float64
upper_quartile
end
def quartiles : Array(Float64)
[first_quartile, second_quartile, third_quartile]
end
def iqr : Float64
third_quartile - first_quartile
end

Loading…
Cancel
Save