Last active
July 15, 2018 16:00
-
-
Save mikesmullin/6043986 to your computer and use it in GitHub Desktop.
Cumulative Moving Average (lets you calculate average in the middle of a stream of numbers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avg = (a) -> | |
sum = 0 | |
for v in a | |
sum += v | |
sum / a.length | |
cavg = (v, av, c) -> | |
av + ((v - av) / c) | |
console.log '---' | |
console.log avg [1,2,3,4] | |
console.log avg [1,2,3,4,5] | |
console.log cavg 5, 2.5, 5 # notice cavg() == avg() above | |
# see also: | |
# https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment