Fit the simple regression y≈w0+w1x online: data arrive one pair at a time and you may not store them. The batch MLE after n points is
w1=Cxx(n)Cxy(n),w0=yˉ(n)−w1xˉ(n)
with running means xˉ(n),yˉ(n) and (biased, divide-by-n) moments Cxx(n)=n1∑i≤n(xi−xˉ(n))2, Cxy(n)=n1∑i≤n(xi−xˉ(n))(yi−yˉ(n)).
Implement online_simple_regression(xs, ys):
- Process the pairs in order, keeping only O(1) state (counts, means and moments) that you update with each new pair.
- Return an
(N, 2) array whose row n holds (w0,w1) after the first n+1 pairs. Whenever Cxx=0 (for example after one point) report w1=0 and w0=yˉ.
- Your updates must stay accurate when the inputs carry a large offset (say x≈106): avoid formulas that subtract large, nearly equal sums of raw products.