Created
September 9, 2015 20:30
-
-
Save riston/28e8ceb8b9803187afd3 to your computer and use it in GitHub Desktop.
Exponential moving average for streams
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
// Exponential moving average | |
function average (alpha) { | |
var last; | |
alpha = alpha || 3/4; | |
return function (input) { | |
if (!last) { | |
last = input; | |
return last; | |
} | |
last = last + alpha * (input - last); | |
return last; | |
} | |
} | |
var a = average(1/3); | |
a(1); | |
a(2); | |
a(3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment