Skip to content

Commit

Permalink
Merge branch 'fix-big'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nephos committed Nov 29, 2018
2 parents 9739e3d + 0d9304d commit 7c8a0e0
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 11 deletions.
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.2.6
version: 0.3.0

authors:
- Arthur Poulet <arthur.poulet@mailoo.org>
Expand Down
9 changes: 9 additions & 0 deletions spec/math/correlation.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "big"

describe Math::Correlation do
it "test basic confidence interval" do
arr1 = [1, 2, 2.5, 3, 3.5, 3.8, 4, 4.2, 4.4, 4.5]
Expand All @@ -9,4 +11,11 @@ describe Math::Correlation do
arr1.covariance(arr2).round(4).should eq 4.2215
arr1.correlation_coef(arr2).round(4).should eq 0.8906
end

it "test big" do
[BigInt.new(1), 2].correlation_coef([2, 3])
[1, 2].correlation_coef([BigInt.new(2), 3])
[BigInt.new(1), 2].correlation_coef([BigInt.new(2), 3])
[1, 2].correlation_coef([BigInt.new(2), BigInt.new(3)])
end
end
6 changes: 3 additions & 3 deletions spec/math/factorial.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe Math do
Math.factorial(6).should eq 720
end

it "factorial bigint" do
res = (1..20).to_a.reduce(BigInt.new 1) { |e, i| e * BigInt.new(i) }
Math.factorial(BigInt.new 20).should eq res
it "factorial big" do
res = (1..20).to_a.reduce(BigInt.new(1)) { |e, i| e * BigInt.new(i) }
Math.factorial(BigInt.new(20)).should eq res
end
end
6 changes: 6 additions & 0 deletions spec/math/macd.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require "big"

describe Math::MACD do
it "test basic macd" do
[1, 2, 3, 2, 1].macd(3).map { |e| e.round(3) }.should eq [2, 2.333, 2]
end

it "test big basic macd" do
puts [BigInt.new(1), BigInt.new(2), BigInt.new(3), BigInt.new(2), 1].macd(3).map { |e| e.round(3) }.should eq [2, 2.333, 2]
end
end
7 changes: 7 additions & 0 deletions spec/math/mean.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "big"

describe Math::Mean do
it "test several basic mean special case" do
arr = ([] of Int32)
Expand All @@ -17,6 +19,11 @@ describe Math::Mean do
[1, 2, -3].mean.should eq 0.0
end

it "test mean on big" do
[BigInt.new(1), 2, 3].mean.should eq 2.0
[BigInt.new(1), BigInt.new(2), BigFloat.new(-3)].mean.should eq 0.0
end

it "test quadratic mean" do
[1, 2, 3, 2].quadratic_mean.round(4).should eq(2.1213)
[1, 2, 1, 5, 10, 9, 1, -13, 2].quadratic_mean.round(4).should eq(6.549)
Expand Down
7 changes: 7 additions & 0 deletions spec/math/median.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "big"

describe Math::Median do
it "test trivia" do
arr = ([] of Int32)
Expand All @@ -13,4 +15,9 @@ describe Math::Median do

[4, 1, 1, 1, 2].median.should eq 1.0
end

it "test big" do
[BigInt.new(1.0), 2.0].median.should eq 1.5
[BigInt.new(1.0), BigFloat.new(2.0)].median.should eq 1.5
end
end
15 changes: 15 additions & 0 deletions spec/math/quartile.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "big"

module Math::Quartile
it "trivial" do
arr = [1, 3, 5]
Expand All @@ -11,6 +13,19 @@ 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 "odd size input" do
arr = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49]

Expand Down
10 changes: 10 additions & 0 deletions spec/math/standard_deviation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ describe Math::StandardDeviation do
arr.standard_deviation.should eq(standard_deviation)
end

it "test big" do
arr = [BigInt.new(1), BigFloat.new(2), 3, 4.0, 4]
mean = (4 + 4 + 3 + 2 + 1) / 5.0
variance = ((4 - mean)**2 + (4 - mean)**2 + (3 - mean)**2 + (2 - mean)**2 + (1 - mean)**2) / 5.0
standard_deviation = Math.sqrt(variance)
arr.mean.should eq(mean)
arr.variance.should eq(variance)
arr.standard_deviation.should eq(standard_deviation)
end

it "test standard deviation without explanations" do
arr = [1, 5, 23, 2, 0, 0, 1]
arr.mean.round(2).should eq 4.57
Expand Down
8 changes: 4 additions & 4 deletions src/lib/math/mean.cr
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
module Math::Mean
# Standard arithmetic mean
# TODO: Handle big Float/Int
def mean : Float64
def mean
return 0.0_f64 if empty?
sum.to_f64 / size.to_f64
end

# The root square mean of the list.
# TODO: Handle big Float/Int
def quadratic_mean : Float64
def quadratic_mean
return 0.0_f64 if empty?
Math.sqrt map { |e| e ** 2 }.mean
end

# The geometric mean of the list.
# For [a, b], a/c = c/b; c**2 = a*b
# TODO: Handle big Float/Int
def geometric_mean : Float64
def geometric_mean
return 0.0_f64 if empty?
reduce { |l, r| l * r } ** (1.0 / size.to_f64)
end

# The harmonic mean of the list.
# TODO: Handle big Float/Int
def harmonic_mean : Float64
def harmonic_mean
return 0.0_f64 if empty?
size.to_f64 / map { |e| 1.0 / e }.sum
end
Expand Down
4 changes: 2 additions & 2 deletions src/lib/math/median.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Math::Median
def median : Float64
def median
return 0.0_f64 if empty?
sorted = sort
size = size()
return sorted[(size - 1) / 2].to_f64 if size.odd?
return sorted[(size - 1) / 2] / 1.0 if size.odd?
(sorted[(size / 2) - 1] + sorted[size / 2]) / 2.0
end
end
Expand Down
Empty file removed src/lib/math/numeric_value.cr
Empty file.
2 changes: 1 addition & 1 deletion src/stats/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Stats
VERSION = "0.2"
VERSION = "0.3"
end

0 comments on commit 7c8a0e0

Please sign in to comment.