Moving Mean and Variance

posted in: Cool Stuff | 0

Algorithms to calculate incremental mean and variance.

Moving Mean

Algorithm

# Initialize
iter = 1
mean = 0 # initial mean

# Loop (sample: x)
mean = (x + iter * mean) / (iter + 1)
iter = iter + 1

Example

[code]

Moving Variance

Algorithm

# Initialize
iter = 1
mean = 0 # initial mean
last = 0
vrnc = 0 # initial variance

# Loop (sample: x)
mean = (x + iter * mean) / (iter + 1)
last = mean
vrnc = (iter * vrnc + (x - last) * (x - mean)) / (iter + 1)
iter = iter + 1

Example

[code]

Exponentially Weighted Moving Mean and Variance

Algorithm

# Initialize
alph = 0.01 # forgetting factor [small update >> 0, strong update >> 1]
mean = 0    # initial mean
vrnc = 0    # initial variance

# Loop (sample: x)
diff = x - mean
incr = alph * diff
mean = mean + incr
vrnc = (1 - alph) * (vrnc + diff * incr)

Example

[code]