1
0
forked from cheng/wallet
wallet/docs/running_average.md

33 lines
1.2 KiB
Markdown
Raw Normal View History

---
# katex
title: running average
---
The running average $a_n$ of a series $R_n$
$$a_n =\frac{a\_{n-1}\times u+R_n\times v}{u+v}$$
The larger $u$ and the smaller $v$, the longer the period over which the running average is taken.
We dont want to do floating point arithmetic, because different peers
might get different answers.
It is probably harmless if they get different answers, provided that this
happens rarely, but easier to ensure that this never happens than to try to
think about all the possible circumstances where this could become a
problem, where malicious people could contrive inputs to make sure it
becomes a problem.
Unsigned integer arithmetic is guaranteed to give the same answers on all
hardware under all compilers. So, when we can, use unsigned integers.
$R_n$is a very small integer where we are worried about very small
differences, so we rescale to represent arithmetic with eight or so bits after
the binary point in integer arithmetic.
$\widetilde {a_n}$represents the rescaled running average $a_n$
$$\widetilde {a_n}=\frac{u\widetilde{a_{n-1}} +v{R_n}\times 2^8}{u+v}$$
$$a_n = \bigg\lfloor\frac{{\widetilde {a_n}}+2^7}{2^8}\bigg\rfloor$$