Sha256: 8ae038150b6ad4382866aa56a634902b48382041d6e8ad9bfd20a98319971d0d
Contents?: true
Size: 549 Bytes
Versions: 13
Compression:
Stored size: 549 Bytes
Contents
module MoreMath class Sequence module MovingAverage def simple_moving_average(n) n < 1 and raise ArgumentError, 'n < 1, has to be >= 1' n <= @elements.size or raise ArgumentError, 'n > #elements, has to be <= #elements' avg = [] 0.upto(@elements.size - n) do |i| sum = 0.0 i.upto(i + n - 1) do |j| sum += @elements[j].to_f end avg << sum / n end avg end alias moving_average simple_moving_average end end end
Version data entries
13 entries across 13 versions & 1 rubygems