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=1ni=1..npiσn2=1ni=1..n(piμn)2Let Sn=nμn and Mn=nσn2. Then SnSn1=pnμn=n1nμn1+1npn=μn1+1n(pnμn1)Also, MnMn1=i=1..n(piμn)2i=1..n1(piμn1)2=i=1..n1[(piμn)2(piμn1)2]+(pnμn)2=i=1..n1(2piμnμn1)(μn1μn)+(pnμn)2=(n1)(μn1μn)2+(pnμn)2=n1n(pnμn1)(μnμn1)+(pnμn)2=(pnμn)(pnμn1)and so

μn=μn1+1n(pnμn1)Mn=Mn1+(pnμn)(pnμn1)σ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=n1nσ2, which is biased, but an easy fix can be made to obtain an unbiased estimator: sn2=nn1σn2. This is typically not a serious issue since the estimators are all asymptotically unbiased. To see this, note that EMn=Ei=1..n(piμn)2=Ei=1..n(piμ+μμn)2=Ei=1..n[(piμ)2+2(piμ)(μμn)+(μμn)2]=nσ22ni=1..nj1..nE(piμ)(pjμ)+σ2=nσ22nnσ2+σ2=(n1)σ2

Navigation

About

Raedwulf ….