Here is a practical example of how you could implement a running standard deviation with python and numpy
:
a = np.arange(1, 10)
s = 0
s2 = 0
for i in range(0, len(a)):
s += a[i]
s2 += a[i] ** 2
n = (i + 1)
m = s / n
std = np.sqrt((s2 / n) - (m * m))
print(std, np.std(a[:i + 1]))
This will print out the calculated standard deviation and a check standard deviation calculated with numpy:
0.0 0.0 0.5 0.5 0.8164965809277263 0.816496580927726 1.118033988749895 1.118033988749895 1.4142135623730951 1.4142135623730951 1.707825127659933 1.707825127659933 2.0 2.0 2.29128784747792 2.29128784747792 2.5819888974716116 2.581988897471611
I am just using the formula described in this thread:
stdev = sqrt((sum_x2 / n) - (mean * mean))