Skip to content

Instantly share code, notes, and snippets.

@wishdev
Created June 18, 2009 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wishdev/132285 to your computer and use it in GitHub Desktop.
Save wishdev/132285 to your computer and use it in GitHub Desktop.
require 'pp' #lets make things look pretty shall we
days=5
dataset=[25.0000,24.8750,24.7813,24.5938,24.5000,24.6250,25.2188,27.2500,26.2500,26.5938]
weighted_avgs = []
#lets break the data down to "days" size blocks - we'll only do calculations on full sets
dataset.each_cons(days) { |block|
puts "Working with "
pp block
puts "Beginning iteration"
tot = 0
#we reverse here to make it easier to use index + 1 as the multiplier
block.reverse.each_with_index { |daily_val, day|
multiplier = day + 1
weighted_val = daily_val * multiplier
tot += weighted_val
pp [daily_val, multiplier, weighted_val, tot]
}
puts "End iteration"
weighted_avg = tot / (days * (days + 1) / 2 )
puts "Weighted average for block is #{weighted_avg}"
weighted_avgs << weighted_avg
}
puts "All done - here are the weighted averages"
pp weighted_avgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment