Browse Source

Fix some big test and functions

master
Arthur POULET 2 years ago
parent
commit
15aad6fd73
No known key found for this signature in database GPG Key ID: FC154EA63FF5D8BC
10 changed files with 63 additions and 9 deletions
  1. +9
    -0
      spec/math/correlation.cr
  2. +3
    -3
      spec/math/factorial.cr
  3. +6
    -0
      spec/math/macd.cr
  4. +7
    -0
      spec/math/mean.cr
  5. +7
    -0
      spec/math/median.cr
  6. +15
    -0
      spec/math/quartile.cr
  7. +10
    -0
      spec/math/standard_deviation.cr
  8. +4
    -4
      src/lib/math/mean.cr
  9. +2
    -2
      src/lib/math/median.cr
  10. +0
    -0
      src/lib/math/numeric_value.cr

+ 9
- 0
spec/math/correlation.cr View File

@ -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]
@ -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

+ 3
- 3
spec/math/factorial.cr View File

@ -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
- 0
spec/math/macd.cr View File

@ -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
- 0
spec/math/mean.cr View File

@ -1,3 +1,5 @@
require "big"
describe Math::Mean do
it "test several basic mean special case" do
arr = ([] of Int32)
@ -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)


+ 7
- 0
spec/math/median.cr View File

@ -1,3 +1,5 @@
require "big"
describe Math::Median do
it "test trivia" do
arr = ([] of Int32)
@ -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
- 0
spec/math/quartile.cr View File

@ -1,3 +1,5 @@
require "big"
module Math::Quartile
it "trivial" do
arr = [1, 3, 5]
@ -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]


+ 10
- 0
spec/math/standard_deviation.cr View File

@ -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


+ 4
- 4
src/lib/math/mean.cr View File

@ -1,14 +1,14 @@
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
@ -16,14 +16,14 @@ module Math::Mean
# 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


+ 2
- 2
src/lib/math/median.cr View File

@ -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


+ 0
- 0
src/lib/math/numeric_value.cr View File


Loading…
Cancel
Save