Browse Source

Improve a bit the documentation, specs, minor bugfix

pull/2/head
Arthur POULET 4 years ago
parent
commit
8d37e7f5d6
No known key found for this signature in database GPG Key ID: FC154EA63FF5D8BC
  1. 10
      spec/math/mean.cr
  2. 6
      spec/math/standard_deviation.cr
  3. 14
      src/lib/math/macd.cr
  4. 10
      src/lib/math/mean.cr
  5. 5
      src/lib/math/std_dev.cr

10
spec/math/mean.cr

@ -1,12 +1,18 @@
describe Math::Mean do
it "test several basic mean special case" do
arr = ([] of Int32)
arr.mean.should eq 0.0
arr.quadratic_mean.should eq 0.0
arr.harmonic_mean.should eq 0.0
arr.geometric_mean.should eq 0.0
end
it "test mean on Array(Float64)" do
([] of Float64).mean.should eq 0.0
[1.0, 2.0, 3.0].mean.should eq 2.0
[1.0, 2.0, -3.0].mean.should eq 0.0
end
it "test mean on Array(Int32)" do
([] of Int32).mean.should eq 0.0
[1, 2, 3].mean.should eq 2.0
[1, 2, -3].mean.should eq 0.0
end

6
spec/math/standard_deviation.cr

@ -18,4 +18,10 @@ describe Math::StandardDeviation do
[1.0, 2.0, 3.0].variance.round(4).should eq(0.6667)
[1.0, 2.0, 3.0].standard_deviation.round(4).should eq(0.8165)
end
it "test several special case" do
arr = [] of Int32
arr.variance.should eq 0.0
arr.standard_deviation.should eq 0.0
end
end

14
src/lib/math/macd.cr

@ -1,8 +1,17 @@
module Math::MACD
# Computes the MACD (sliding mean) over N values.
# It will return another list of *size - n + 1* elements, because it is not
# possible to computes the n/2 first and lasts means.
# NOTE for now it is impossible to computes a MACD with a pair window (n should be odd)
#
# TODO: maybe computing a reduced MACD is good solution against reduced returns ?
def macd(n : Int)
((n.odd?) ? macd_odd(n) : macd_pair(n)).compact
end
# :nodoc:
#
# MACD computation if n is odd
private def macd_odd(n)
map_with_index do |_, i|
next if i < n / 2
@ -12,8 +21,11 @@ module Math::MACD
end
end
# :nodoc:
#
# MACD computation if n is pair
private def macd_pair(n)
raise ArgumentError.new "The MACD should be computed for an odd window"
raise ArgumentError.new "The MACD should be computed for an odd window (you should retry with #{n + 1})"
end
end

10
src/lib/math/mean.cr

@ -6,17 +6,25 @@ module Math::Mean
sum.to_f64 / size.to_f64
end
# The square root of mean square
# The root square mean of the list.
# TODO: Handle big Float/Int
def quadratic_mean : Float64
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
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
return 0.0_f64 if empty?
size.to_f64 / map { |e| 1.0 / e }.sum
end
end

5
src/lib/math/std_dev.cr

@ -1,13 +1,14 @@
require "./mean"
module Math::StandardDeviation
# Squared deviation from the mean
# Squared deviation from the mean.
def variance
return 0.0_f64 if empty?
mean = mean()
self.map { |e| (e - mean)**2 }.mean
end
# Population standard deviation
# Population standard deviation.
def standard_deviation
Math.sqrt variance
end

Loading…
Cancel
Save