Welford’s Algorithm

Computation of Sample Mean and Variance

In HFT, the first and second-order statistics often need to be determined. Necessarily, the algorithm that performs the computation must be O(1) and numerically stable. The sample mean and variance of a signal p are signals given by μn=1n∑i=1..npiσn2=1n∑i=1..n(pi–μn)2Let Sn=nμn and Mn=nσn2. Then Sn–Sn–1=pnμn=n–1nμn–1+1npn=μn–1+1n(pn–μn–1)Also, Mn–Mn–1=∑i=1..n(pi–μn)2–∑i=1..n–1(pi–μn–1)2=∑i=1..n–1[(pi–μn)2–(pi–μn–1)2]+(pn–μn)2=∑i=1..n–1(2pi–μn–μn–1)(μn–1–μn)+(pn–μn)2=(n–1)(μn–1–μn)2+(pn–μn)2=n–1n(pn–μn–1)(μn–μn–1)+(pn–μn)2=(pn–μn)(pn–μn–1)and so

μn=μn–1+1n(pn–μn–1)Mn=Mn–1+(pn–μn)(pn–μn–1)σn2=1nMnThis approach is known as Welford’s online algorithm and is numerically stable, O(1), and compuatble in one pass.

Biasedness of Estimation.

Under weak statistical assumption that the signal is a sequence of random variables with a stationary first moment μ, then Eμn=μ, that is the estimator unbiased. If the random variables are uncorrelated and have a stationary finite variance, then Eσn2=n–1nσ2, which is biased, but an easy fix can be made to obtain an unbiased estimator: sn2=nn–1σn2. This is typically not a serious issue since the estimators are all asymptotically unbiased. To see this, note that EMn=E∑i=1..n(pi–μn)2=E∑i=1..n(pi–μ+μ–μn)2=E∑i=1..n[(pi–μ)2+2(pi–μ)(μ–μn)+(μ–μn)2]=nσ2–2n∑i=1..n∑j–1..nE(pi–μ)(pj–μ)+σ2=nσ2–2nnσ2+σ2=(n–1)σ2

Navigation

About

Raedwulf ….