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

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 sits near
, 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 ,
,
and
, and each new row adds the rank-1 terms
and
, so the state stays
however long the stream runs. The trap is solving the normal equations from those raw sums, because
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
with
, updated by Givens rotations, instead of the Gram itself. That factor works at
rather than its square and still costs
per row. A forgetting factor
shrinks the old state before each update so the coefficients can follow regime shifts.
(1) The State: Keep , the symmetric Gram (
unique entries),
and
. At
that is 862 numbers, and
yields the residual sum of squares
for free.
(2) Rank-1 Update, Lazy Solve: Adding a row costs , while a fresh Cholesky solve costs about
flops at
. Either solve every few ticks or keep a factor that needs only an
back substitution.
(3) Squared Conditioning: Forming squares the condition number, so a scaled
leaves about 4 of float64’s 16 digits. Past
the computed Gram can stop being positive definite and Cholesky fails outright.
(4) Center Before You Sum: A feature with mean and std
beside an intercept has scaled
, so the 4,500 price gives
and
from the offset alone. Welford co-moments, or subtracting a frozen reference mean, remove it.
(5) Factor, Never Form: Rotating each row into with
Givens rotations is backward stable, costs
, and is the same operation as a Cholesky rank-1 update of the factor.
(6) Inverse Updates Drift: Sherman-Morrison (RLS) keeps current in
, but over millions of updates rounding erodes the symmetry and definiteness of
unless you symmetrize, refactor, or use a square-root form.
Mathematical Formulation:
Where:
and
are the running Gram
and cross-product
after row
, with feature row
and target
; the forgetting factor
equals 1 for plain OLS.
is the coefficient estimate, and
is the 2-norm condition number, the ratio of largest to smallest singular value, quoted here for column-scaled
.
is the effective weight (equal to
when
),
is the deviation from the previous running mean
, and
is the centered co-moment matrix. A parallel update
gives slopes from
and the intercept
.
is the
upper-triangular factor,
its rotated target,
the product of
Givens rotations that zero the appended row, and
the leftover component whose square adds to the RSS; back substitution on
costs
.
The factor wins because , 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
when the residual is small, while Cholesky on the accumulated Gram sits near
, with
in float64. For low-
return regressions the least-squares problem itself carries a
sensitivity, where
is the angle between
and the column space of
, 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.

Figure 1: (a) With noiseless targets, error from solving the accumulated Gram grows like while Givens QR row updates grow like
, roughly six orders of magnitude apart at
. (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, and
. The raw sums are
,
,
,
and
, so the slope is
. Now shift the feature to a price level,
. The denominator becomes
, a difference of two nine-digit numbers. Float32 spaces numbers near
exactly 16 apart, so that 6 cannot survive and the slope is garbage. Welford keeps the running mean instead: the co-moment grows
and the cross-moment
, so the slope is
in any precision and the intercept is
.
| Property | C: Givens-updated factor R | A: Gram sums + normal equations | B: Sherman-Morrison inverse |
|---|---|---|---|
| State stored | R (820 entries at p = 40), rotated target, RSS | XᵀX (820 entries), Xᵀy, yᵀy, n | P = inverse Gram (820 entries), current β |
| Cost per new row | O(p²), p rotations | O(p²) multiply-adds | O(p²), one gain vector |
| Cost to read β | O(p²) back substitution | O(p³) Cholesky, about 21,000 flops at p = 40 | Free, β is updated every row |
| Coefficient error scales with | cond(X) | cond(X) squared | cond(X) squared, plus drift that grows with update count |
| Breaks when | X is numerically rank deficient | cond(X) passes about 10^8, or raw feature means are large | P loses symmetry or definiteness; forgetting with a flat feature winds up |
| Merging shards | QR of the stacked factors (TSQR) | Add the sums, exact and associative | No cheap merge; rebuild from a Gram |
Leave a Reply