# incremental $R^2$ update at every new sample [duplicate]

I am sampling from a random process $$X$$ and I would like to calculate $$R^2$$ for the cumulative sum of the samples: $$x_1,..x_n$$ $$y_n=\sum_0^n x_i$$

$$R^2_n=RSQ( [1,2,...n], [y_1,y_2,..,y_n])$$

The calculation becomes increasingly slow as $$n$$ grows. Do you know any incremental way to update $$R^2$$ at every new sample, without recalculating it from the beginning every time?

• You really don't want something as badly behaved as that formula. That's frequently a disastrous way to calculate variance. There are much more stable ways to calculate variance. Note that R^2 can be written as a ratio of two sums of squares – Glen_b Oct 1 '19 at 12:41
• thanks @Glen_b I edited away the analogy of incremental variance calculation – elemolotiv Oct 1 '19 at 12:46
• You could adapt the online updating approach here, but instead of calculating $r$, calculate its square; i.e. $r^2 = \frac{N_{n+1}^2}{D_{n+1}E_{n+1}}$. You can speed it up further than that (e.g. by using ideas from Welford's algorithm; and the equivalent for covariance and taking advantage of the simple form of the 1,2,3... values & hence their mean and sum of squares),...ctd – Glen_b Oct 1 '19 at 13:08
• ctd ... but you'll probably find that first method sufficient just applied directly to the y's and the 1,2,3... values – Glen_b Oct 1 '19 at 13:21
• I'm in two minds whether it counts as effectively a duplicate of that first link or whether there's enough in the special structure of this problem to leave it. – Glen_b Oct 1 '19 at 13:29