Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crystal v0.34 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# stats

An expressive implementation of statistical distributions.
Compatible with crystal v0.27.0.
Compatible with crystal v0.34.0.

## Installation

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: stats
version: 0.3.0
version: 0.3.1

authors:
- Arthur Poulet <[email protected]>
Expand Down
23 changes: 11 additions & 12 deletions spec/math/quartile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/math/factorial.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/lib/math/macd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/lib/math/median.cr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 13 additions & 13 deletions src/lib/math/quartile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down