I have coded and benchmarked 8 different running mean algorithms.
In the presence of inexact numbers it is not as accurate as
redoing the sums, but it's pretty close, and it's fast.
If "width" is not an integer or is out of range, an error
will be reported by #new: or #at:[put:].  It's based on Welford's
stable update.

Of course this approach does NOT work for trimmed or Winsorised
means or for medians or any kind of robust estimate of location.

SequenceableCollection
  methods for: 'summarising'
    runningMeans: width
      |a m d|
      a := Array new: self size - width + 1.
      m := 0.
      1 to: width do: [:i |
        m := (self at: i) + m].
      m := m / width.
      d := 1.
      a at: d put: m.
      width + 1 to: self size do: [:i |
        m := ((self at: i) - (self at: d)) / width + m.
        d := d + 1.
        a at: d put: m].
      ^a

Reply via email to