Method: Daru::Maths::Statistics::Vector#rolling

Defined in:
lib/daru/maths/statistics/vector.rb

#rolling(function, n = 10) ⇒ Daru::Vector

Calculate the rolling function for a loopback value.

Examples:

Using #rolling

ts = Daru::Vector.new((1..100).map { rand })
         # => [0.69, 0.23, 0.44, 0.71, ...]
# first 9 observations are nil
ts.rolling(:mean)    # => [ ... nil, 0.484... , 0.445... , 0.513 ... , ... ]

Parameters:

  • function (Symbol)

    The rolling function to be applied. Can be any function applicatble to Daru::Vector (:mean, :median, :count, :min, :max, etc.)

  • n (Integer) (defaults to: 10)

    (10) A non-negative value which serves as the loopback length.

Returns:



692
693
694
695
696
697
698
699
# File 'lib/daru/maths/statistics/vector.rb', line 692

def rolling function, n=10
  Daru::Vector.new(
    [nil] * (n - 1) +
    (0..(size - n)).map do |i|
      Daru::Vector.new(@data[i...(i + n)]).send(function)
    end, index: @index
  )
end