ML0149 Streaming OLS & Sufficient Statistics

Update OLS coefficients online while storing only O(p^2) state. What sufficient statistics do you accumulate, and how do you keep the Gram update numerically stable?

Three dashed panels. The stream panel: each tick adds one row with 40 features, 5 million ticks per day, 1.6 GB of history per day that can never be revisited, and a state budget of 1,600 numbers. The data panel: a raw mid-price feature with mean 4,500 and standard deviation 5 beside an intercept column, a composite factor that nearly equals the mean of four others giving a condition number near one million, and betas that drift between regimes. The open question panel lists option A, accumulate X-transpose-X and X-transpose-y and solve the normal equations each tick; option B, keep the inverse Gram matrix with Sherman-Morrison updates; option C, keep a triangular factor R and Q-transpose-y and rotate each new row in; and asks which state stays accurate and how it tracks regimes.

The Problem: A desk refits a 40-factor linear signal on 5 million ticks a day and can keep about 1,600 numbers of state instead of 1.6 GB of raw rows. One feature is a raw mid-price with mean 4,500 and std 5, the scaled condition number of X sits near 10^6, betas drift between regimes, and the team must pick which of three candidate states (A, B, C) to stream.

Answer

OLS touches the data only through the sufficient statistics X^\top X, X^\top y, y^\top y and n, and each new row adds the rank-1 terms x_t x_t^\top and y_t x_t, so the state stays O(p^2) however long the stream runs. The trap is solving the normal equations from those raw sums, because \kappa(X^\top X) = \kappa(X)^2 and float64 then loses twice as many of its 16 digits as the problem demands. Stability comes from two changes at the same memory cost. Accumulate centered co-moments with Welford updates so large feature means never enter the Gram, and keep the triangular factor R with Q^\top y, updated by Givens rotations, instead of the Gram itself. That factor works at \kappa(X) rather than its square and still costs O(p^2) per row. A forgetting factor \lambda shrinks the old state before each update so the coefficients can follow regime shifts.

(1) The State: Keep n, the symmetric Gram (p(p+1)/2 unique entries), X^\top y and y^\top y. At p = 40 that is 862 numbers, and y^\top y yields the residual sum of squares y^\top y - b^\top \hat\beta for free.
(2) Rank-1 Update, Lazy Solve: Adding a row costs O(p^2), while a fresh Cholesky solve costs about p^3/3 \approx 21{,}000 flops at p = 40. Either solve every few ticks or keep a factor that needs only an O(p^2) back substitution.
(3) Squared Conditioning: Forming X^\top X squares the condition number, so a scaled \kappa(X) = 10^6 leaves about 4 of float64’s 16 digits. Past \kappa(X) \approx 10^8 the computed Gram can stop being positive definite and Cholesky fails outright.
(4) Center Before You Sum: A feature with mean m and std s beside an intercept has scaled \kappa \approx 2m/s, so the 4,500 price gives \kappa \approx 1800 and \kappa^2 \approx 3.2 \times 10^6 from the offset alone. Welford co-moments, or subtracting a frozen reference mean, remove it.
(5) Factor, Never Form: Rotating each row into R with p Givens rotations is backward stable, costs O(p^2), and is the same operation as a Cholesky rank-1 update of the factor.
(6) Inverse Updates Drift: Sherman-Morrison (RLS) keeps P = (X^\top X)^{-1} current in O(p^2), but over millions of updates rounding erodes the symmetry and definiteness of P unless you symmetrize, refactor, or use a square-root form.

Mathematical Formulation:
A_t = \lambda A_{t-1} + x_t x_t^\top
b_t = \lambda b_{t-1} + y_t x_t
A_t \hat\beta_t = b_t
\kappa(X^\top X) = \kappa(X)^2
W_t = \lambda W_{t-1} + 1
\delta_t = x_t - \bar x_{t-1}
\bar x_t = \bar x_{t-1} + \delta_t / W_t
C_t = \lambda C_{t-1} + \delta_t (x_t - \bar x_t)^\top
\begin{bmatrix} R_t & z_t \\ 0 & e_t \end{bmatrix} = G_t \begin{bmatrix} \sqrt{\lambda} R_{t-1} & \sqrt{\lambda} z_{t-1} \\ x_t^\top & y_t \end{bmatrix}
R_t \hat\beta_t = z_t

Where:

  • A_t and b_t are the running Gram X^\top X and cross-product X^\top y after row t, with feature row x_t \in \mathbb{R}^p and target y_t; the forgetting factor \lambda \in (0, 1] equals 1 for plain OLS.
  • \hat\beta_t is the coefficient estimate, and \kappa(\cdot) is the 2-norm condition number, the ratio of largest to smallest singular value, quoted here for column-scaled X.
  • W_t is the effective weight (equal to t when \lambda = 1), \delta_t is the deviation from the previous running mean \bar x_{t-1}, and C_t is the centered co-moment matrix. A parallel update c_t = \lambda c_{t-1} + \delta_t (y_t - \bar y_t) gives slopes from C_t \beta = c_t and the intercept \bar y_t - \bar x_t^\top \beta.
  • R_t is the p \times p upper-triangular factor, z_t = Q^\top y its rotated target, G_t the product of p Givens rotations that zero the appended row, and e_t the leftover component whose square adds to the RSS; back substitution on R_t \hat\beta_t = z_t costs O(p^2).

The factor wins because R^\top R = X^\top X, so it carries exactly the information of the Gram, yet building it by rotating rows in never forms the product that squares the condition number. A backward-stable QR solve has forward error near \epsilon\,\kappa(X) when the residual is small, while Cholesky on the accumulated Gram sits near \epsilon\,\kappa(X)^2, with \epsilon \approx 1.1 \times 10^{-16} in float64. For low-R^2 return regressions the least-squares problem itself carries a \kappa^2 \tan\theta sensitivity, where \theta is the angle between y and the column space of X, so QR’s lead narrows. It still never breaks down where the Gram’s Cholesky does, and its fitted values stay accurate because the method is backward stable.

Two-panel log-log chart. Left: relative coefficient error versus the condition number of X from 1 to ten million for a planted coefficient vector with noiseless targets; the Gram-sums-plus-Cholesky curve rises with slope two and tracks an epsilon kappa squared reference line, reaching about one percent at ten million, while the Givens QR row-update curve rises with slope one along an epsilon kappa reference line. Right: relative slope error for an intercept plus one feature as the feature's mean-to-std ratio grows from 1 to one million; raw sums in float32 reach order-one error by a ratio of a few hundred, raw sums in float64 climb steadily, and Welford-centered accumulation stays far lower in both precisions; a dotted vertical line marks the mid-price ratio of 900.

Figure 1: (a) With noiseless targets, error from solving the accumulated Gram grows like \epsilon\kappa^2 while Givens QR row updates grow like \epsilon\kappa, roughly six orders of magnitude apart at \kappa = 10^6. (b) At the price feature’s mean-to-std ratio of 900, raw float32 sums miss the slope by tens of percent or more, raw float64 sums lose roughly 6 to 8 digits, and Welford-centered accumulation stays orders of magnitude more accurate in both precisions.

Worked Example:
Take three ticks with an intercept and one feature, x = 1, 2, 3 and y = 2, 3, 5. The raw sums are n = 3, \sum x = 6, \sum x^2 = 14, \sum y = 10 and \sum xy = 23, so the slope is (3 \cdot 23 - 6 \cdot 10)/(3 \cdot 14 - 36) = 9/6 = 1.5. Now shift the feature to a price level, x = 4501, 4502, 4503. The denominator becomes 182{,}412{,}042 - 182{,}412{,}036 = 6, a difference of two nine-digit numbers. Float32 spaces numbers near 1.8 \times 10^8 exactly 16 apart, so that 6 cannot survive and the slope is garbage. Welford keeps the running mean instead: the co-moment grows 0 + 0.5 + 1.5 = 2 and the cross-moment 0 + 0.5 + 2.5 = 3, so the slope is 3/2 = 1.5 in any precision and the intercept is 10/3 - 1.5 \times 4502 \approx -6749.67.

PropertyC: Givens-updated factor RA: Gram sums + normal equationsB: Sherman-Morrison inverse
State storedR (820 entries at p = 40), rotated target, RSSXᵀX (820 entries), Xᵀy, yᵀy, nP = inverse Gram (820 entries), current β
Cost per new rowO(p²), p rotationsO(p²) multiply-addsO(p²), one gain vector
Cost to read βO(p²) back substitutionO(p³) Cholesky, about 21,000 flops at p = 40Free, β is updated every row
Coefficient error scales withcond(X)cond(X) squaredcond(X) squared, plus drift that grows with update count
Breaks whenX is numerically rank deficientcond(X) passes about 10^8, or raw feature means are largeP loses symmetry or definiteness; forgetting with a flat feature winds up
Merging shardsQR of the stacked factors (TSQR)Add the sums, exact and associativeNo cheap merge; rebuild from a Gram

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *