Definitions:
\[
u_n = \frac{\sum_{i=1}^n x_i}{n} \\
u_{n+k} = \frac{\sum_{i=1}^{n+k} x_i}{n+k}\\
\]
Problem:
We can decompose \(u_{n+k}\) as:
\[
u_{n+k} = u_{n+k} + u_n - u_n = u_n + (u_{n+k}-u_n)
\]
If we had an easy expressión for \(u_{n+k}-u_n\), we could use the old value of \(u_n\) to efficiently calculate \(u_{n+k}\). Let's try some algebra to simplify \(u_{n+k}-u_n\):
\[ u_{n+k}-u_n = \frac{\sum_{i=1}^{n+k} x_i}{n+k} - \frac{\sum_{i=1}^n x_i}{n}\\ =\frac{ n \sum_{i=1}^{n+k} x_i}{(n+k)n} - (n+k) \frac{\sum_{i=1}^n x_i}{(n+k)n}\\ =\frac{ n \sum_{i=1}^{n+k} x_i - (n+k) \sum_{i=1}^n x_i}{(n+k)n} \\ =\frac{ n \sum_{i=1}^{n} x_i + n \sum_{i=n+1}^{n+k} x_i - (n+k) \sum_{i=1}^n x_i}{(n+k)n}\\ =\frac{ n \sum_{i=n+1}^{n+k} x_i - k \sum_{i=1}^n x_i}{(n+k)n}\\ =\frac{ \sum_{i=n+1}^{n+k} x_i - k u_n} {(n+k)}\\ \]
That seems simple enough:
Therefore, our algorithm is:
\[ u_{n+k} = u_{n} + (u_{n+k}-u_n) \\ u_{n+k} = u_n + \frac{ (\sum_{i=n+1}^{n+k} x_i) - k u_n} {(n+k)} \quad \textit{(more efficient)}\\ u_{n+k} = u_n + \frac{ \sum_{i=n+1}^{n+k} (x_i - u_n)} {(n+k)} \quad \textit{(more precise)}\\ \]
We can use a class in Python to implement this algorithm for numpy arrays with batches \(x\) of \(k\) numbers:
class RunningMean:
def __init__(self):
self.n=0
def update(self,x:np.ndarray):
k = x.shape[0]
if self.n==0:
self.n=k
self.mean = x.mean(axis=0)
else:
self.n+=k
diff = x.sum(axis=0) - k * self.mean
# alternative, less efficient, better precision
# diff = (x - self.mean).sum(axis=0)
self.mean = self.mean + diff / self.n
Welford's method is a stable method for computing a running mean. It is generally derived for cases where the updates happen only for a single new sample.
Note that the previous is a generalization of Welford's method for k=1, since:
\[ u_{n+k} = u_n + \frac{ (\sum_{i=n+1}^{n+k} x_i) - k u_n} {(n+k)} \\ \text{Replacing $k=1$:}\\ u_{n+1} = u_n + \frac{ x_{n+1} -u_n} {(n+1)} \\ \]
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing