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

Version Path
more_math-1.3.0 lib/more_math/sequence/moving_average.rb
more_math-1.2.2 lib/more_math/sequence/moving_average.rb
more_math-1.2.1 lib/more_math/sequence/moving_average.rb
more_math-1.2.0 lib/more_math/sequence/moving_average.rb
more_math-1.1.0 lib/more_math/sequence/moving_average.rb
more_math-1.0.2 lib/more_math/sequence/moving_average.rb
more_math-1.0.1 lib/more_math/sequence/moving_average.rb
more_math-1.0.0 lib/more_math/sequence/moving_average.rb
more_math-0.4.0 lib/more_math/sequence/moving_average.rb
more_math-0.3.3 lib/more_math/sequence/moving_average.rb
more_math-0.3.2 lib/more_math/sequence/moving_average.rb
more_math-0.3.1 lib/more_math/sequence/moving_average.rb
more_math-0.3.0 lib/more_math/sequence/moving_average.rb