This is to add to @chmike answer.
The method appears to be similar to B. P. Welford’s online algorithm for standard deviation which also calculates the mean. John Cook gives a good explanation here. Tony Finch in 2009 provides a method for an exponential moving average and standard deviation:
diff := x – mean
incr := alpha * diff
mean := mean + incr
variance := (1 - alpha) * (variance + diff * incr)
Peering at the previously posted answer and expanding upon it to include a exponential moving window:
init():
meanX = 0, meanY = 0, varX = 0, covXY = 0, n = 0,
meanXY = 0, varY = 0, desiredAlpha=0.01 #additional variables for correlation
update(x,y):
n += 1
alpha=max(desiredAlpha,1/n) #to handle initial conditions
dx = x - meanX
dy = y - meanY
dxy = (x*y) - meanXY #needed for cor
varX += ((1-alpha)*dx*dx - varX)*alpha
varY += ((1-alpha)*dy*dy - varY)*alpha #needed for corXY
covXY += ((1-alpha)*dx*dy - covXY)*alpha
#alternate method: varX = (1-alpha)*(varX+dx*dx*alpha)
#alternate method: varY = (1-alpha)*(varY+dy*dy*alpha) #needed for corXY
#alternate method: covXY = (1-alpha)*(covXY+dx*dy*alpha)
meanX += dx * alpha
meanY += dy * alpha
meanXY += dxy * alpha
getA(): return covXY/varX
getB(): return meanY - getA()*meanX
corXY(): return (meanXY - meanX * meanY) / ( sqrt(varX) * sqrt(varY) )
In the above "code", desiredAlpha could be set to 0 and if so, the code would operate without exponential weighting. It can be suggested to set desiredAlpha to 1/desiredWindowSize as suggested by Modified_moving_average for a moving window size.
Side question: of the alternative calculations above, any comments on which is better from a precision standpoint?
References:
chmike (2013) https://stats.stackexchange.com/a/79845/70282
Cook, John (n.d.) Accurately computing running variance http://www.johndcook.com/blog/standard_deviation/
Finch, Tony. (2009) Incremental calculation of weighted mean and variance. https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf
Wikipedia. (n.d) Welford’s online algorithm https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm