Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple best-fit-line algorithm similar to this description.

Without memorizing the points history, it is easy to calculate a rolling best fit line as long as we remember (store) the intermediate values used to calculate the BFL:

sumX,sumY           //  the sum
sumX2, sumY2,       //  sum of squares
sumXY, and count    //  sum of (X*Y), and count

when a new point arrives (Xn,Yn), the line simply update the sums by adding the latest value based on Xn, Yn. then calculate BFL:

XMean = SumX / Count
YMean = SumY / Count
Slope = (SumXY - SumX * YMean) / (SumX2 - SumX * XMean)
YInt = YMean - Slope * XMean

to get

 Y = Slope * X + YInt

However, as the number of points grow, the new point will have less of an effect on the best-fit-line.

is there a way to mathematically reduce this weight? for example when the count reaches 20, modify the intermediate variables to represent a line with a weight of 10 points:

when count = 20 ->

    count = count/2
    sumX = sumX/2    // this will result in the same XMean, but the same line?
    sumY = sumY/2    // same YMean here as well

and the values for sumXY,sumX2,sumY2 are even more perplexing to me.

Specifically, I'm looking for equations for these intermediate values to result to the same line but with less weight.


share|cite|improve this question
You have nowhere defined "weight", so it's hard to answer your question. In any event, if the total number of points is, say, 20, then the 20th point has exactly as much influence on the final answer as the first point, so it's not clear to me that there is actually a problem here. – Gerry Myerson Nov 19 '12 at 22:34
@Gerry Actually, if you measure "influence" of a data point as, say, the derivative of the fitted slope with respect to the point's location, then influences vary--a lot. There also is a fairly current and natural definition of "weight" in this context. – whuber Nov 19 '12 at 22:44
A duplicate of this question appeared two years ago at… and has some answers there. – whuber Nov 19 '12 at 22:46
@Whuber the question is the same indeed. Their answers, however has me specifically no closer to finding answers to this problem. You are correct, as by "weight", I'm referring to influence. if I knew better, I'd state that I'm looking for a limited window regression. One approach would to be to simply have 2 sets of intermediate variable representing k points each (as opposed to 2*K data points collected) and empty one set to increase the influence of the new points and alternate. That way the influence is at least size of one set and at most two set sizes. – MandoMando Nov 19 '12 at 23:10
Actually, "weight" and "influence" are two completely different things. (See inter alia.) You appear to be looking for the regression version of a time-weighted average. This can be done--as I suggest in an answer to that other question--with a suitable updating algorithm. – whuber Nov 19 '12 at 23:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.