Last active
January 13, 2020 14:17
-
-
Save joonas-yoon/f14b6819ca9becd22a7c6d4739f62861 to your computer and use it in GitHub Desktop.
Moving Average
This file contains 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
def moving_average(a): | |
ma = a[0] | |
for i in range(1, len(a)): | |
ma = ma*i/(i+1) + a[i]/(i+1) | |
return ma | |
if __name__ == "__main__": | |
a = [3,1,4,1,5,9,2,6,5,3,5,8] | |
print("Avg.:", sum(a)/len(a)) | |
print("Moving Avg.:", moving_average(a)) | |
# output | |
# Avg.: 4.333333333333333 | |
# Moving Avg.: 4.333333333333333 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment