Mercurial > hg > tvii
changeset 61:0e6743d5416d
notes on averaging
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 18:18:32 -0800 |
parents | 38fe99071b11 |
children | ff7719f05f0d |
files | tvii/average.py |
diffstat | 1 files changed, 22 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tvii/average.py Sun Dec 10 18:18:32 2017 -0800 @@ -0,0 +1,22 @@ +""" +exponentially weighted averages + +v[0] = theta[0] +v[t] = beta*v[t-1] + (1-beta)*theta[t] + +=> +v[t] as approximately averaged over 1/(1-beta) counts + +Bias correction: +v[t] / (1 - beta**t) +""" + +def exponential_weighted_average(theta, beta=0.9): + """a form of moving window""" + # TODO: consider a generator; there isn't much state here + # v = theta[0] + # v = beta*v (1-beta)*theta[1] + retval = [theta[0]] + for value in theta[1:]: + retval.append(beta*retval[-1] + (1.-beta)*value) + return retval