2009/6/18 John W Higgins <wishdev / gmail.com>:
> On Wed, Jun 17, 2009 at 3:33 PM, David Newton
> <info / freesportsarbitrage.com>wrote:
>
>> Hello all,
>>
>> I am a ruby novice who would really appreciate some guidance with the
>> following:
>>
>> I am trying to calculate a 5 day weighted moving average based on a set
>> of numbers in a data series. The most recent is given a * 5 weight, the
>> next * 4 etc etc. with the oldest (the 5th number) * 1.
>>
>
> This should do the trick for you
>
> http://gist.github.com/131578

Here's my suggestion

weights = (1..5).to_a
div = weights.inject(0){|s,x| s + x}.to_f

data = [
  25.0000,
  24.8750,
  24.7813,
  24.5938,
  24.5000,
  24.6250,
  25.2188,
  27.2500,
  26.2500,
  26.5938,
]

data.each_cons(weights.size).each_with_index do |c,i|
  num = 0
  c.zip(weights) {|a,b| num += a * b}
  printf "period %2d: %8.3f\n", i, num / div
end

You only need to change 'weights' and the calculation will still.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/