Why Summation Matters in GLM Computation
A GLM objective is assembled from contributions: observation-level log-likelihood terms are added, score contributions are summed into a vector, and information contributions are summed into a matrix. In exact arithmetic, the order of addition does not matter. In floating-point arithmetic, each addition may round, so changing the order can change the result. The effect is especially noticeable when many terms are accumulated or when large positive and negative contributions nearly cancel.
A small summation error can affect more than the displayed objective. An inaccurate score can make a computed update appear too small or point in a slightly different direction; inaccurate information entries can alter later calculations. Compensated summation reduces the error caused by accumulation. It does not change the GLM or correct errors already present in the individual contributions.
A Bound for Sequential Summation
Use the standard floating-point model for an addition: if the exact result is finite and normal, then its computed value is \(\operatorname{fl}(a+b)=(a+b)(1+\delta)\), where \(|\delta|\leq u\) and \(u\) is the unit roundoff. Assume here that no overflow or underflow occurs. Define \(\gamma_k=ku/(1-ku)\) for \(ku<1\), and let \(\gamma_0=0\).
Proof. Write \(s_i=(s_{i-1}+x_i)(1+\delta_i)\), with \(|\delta_i|\leq u\). Since \(x_1\) is itself a floating-point number, the first addition is exact: \(\operatorname{fl}(0+x_1)=x_1\). Thus \(\delta_1=0\). Expanding the recurrence gives \[ s_n=x_1\prod_{i=2}^n(1+\delta_i)+\sum_{j=2}^n x_j\prod_{i=j}^n(1+\delta_i). \] The coefficient of \(x_1\) therefore has at most \(n-1\) nontrivial factors; the coefficient of \(x_j\), for \(j\geq 2\), has \(n-j+1\leq n-1\) factors. This factor count uses the exact first addition; without \(\delta_1=0\), the coefficient of \(x_1\) would include another factor.
For any product of \(k\) factors \(1+\delta_i\), with \(ku<1\), its distance from 1 is at most \(\gamma_k\). Indeed, the product is at most \((1+u)^k\leq 1/(1-ku)\), and at least \((1-u)^k\geq 1-ku\). These bounds imply that the absolute difference from 1 is at most \(ku/(1-ku)\). Since \(\gamma_k\leq\gamma_{n-1}\), subtracting the exact sum from the expanded expression and applying the triangle inequality gives the stated bound. \(\square\)
Worked Example: When the Error Bound Is Large
Consider the floating-point inputs \(10^{16}\), \(1\), and \(-10^{16}\) in IEEE double precision. Their exact sum is \(1\). At \(10^{16}\), adjacent representable numbers are 2 apart, so \(10^{16}+1\) is exactly halfway between two representable values. Round-to-nearest, ties-to-even gives \(10^{16}\). Sequential addition therefore gives \[ \operatorname{fl}\!\left(\operatorname{fl}(10^{16}+1)-10^{16}\right) =\operatorname{fl}(10^{16}-10^{16})=0. \] The contribution \(1\) disappears.
Here \(\sum |x_i|=2\cdot 10^{16}+1\), while the exact sum is only 1. The ratio \(\sum |x_i|/|\sum x_i|\), called the condition number of this sum, is approximately \(2\cdot 10^{16}\). The bound from the theorem is an absolute bound, but dividing it by the magnitude of the exact sum gives a potentially enormous relative bound in this example. This reflects the sensitivity of a nearly cancelling sum; it does not mean that every computation of it must attain the bound.
Compensated Summation
Compensated summation keeps track of low-order information that ordinary addition would discard. One practical variant is Neumaier summation. It maintains a running sum \(s\) and a correction \(c\). For each input \(x\), it computes \(t=\operatorname{fl}(s+x)\), estimates the part lost in forming \(t\), and adds that residual to \(c\). A final result is formed from \(s+c\).
Set \(s=0\) and \(c=0\).
For each \(x\), compute \(t=\operatorname{fl}(s+x)\).
If \(|s|\geq |x|\), compute \(e=\operatorname{fl}((s-t)+x)\); otherwise compute \(e=\operatorname{fl}((x-t)+s)\). Then update \(c=\operatorname{fl}(c+e)\) and set \(s=t\).
After all inputs, return \(\operatorname{fl}(s+c)\).
The branch chooses an ordering that helps estimate the rounding residual of the addition; \(c\) accumulates those residual estimates. In common floating-point settings this can substantially improve accuracy, particularly when small terms are added to a much larger running sum. It is not a promise of an exact answer for every input: the residual and the correction are themselves computed in floating-point arithmetic.
Worked Example: Recovering a Lost Unit
Apply Neumaier summation to \(10^{16},1,-10^{16}\), in that order, using the same double-precision rounding behavior as above. Initially \(s=0\), \(c=0\).
For \(x=10^{16}\), \(t=10^{16}\), and the residual is 0. Thus \(s=10^{16}\) and \(c=0\).
For \(x=1\), \(t=\operatorname{fl}(10^{16}+1)=10^{16}\). Since \(|s|\geq|x|\), the residual estimate is \[ e=\operatorname{fl}((10^{16}-10^{16})+1)=1. \] Now \(s=10^{16}\) and \(c=1\).
For \(x=-10^{16}\), \(t=0\). The residual is \[ e=\operatorname{fl}((10^{16}-0)-10^{16})=0. \] The updated values are \(s=0\), \(c=1\), so the corrected result is \(\operatorname{fl}(0+1)=1\), the exact sum. The same sequence that ordinary summation returned as 0 is recovered here because the lost unit was retained in the correction.
What the Condition Number Tells You
For inputs with exact sum \(S=\sum_i x_i\neq 0\), define the summation condition number by \[ \kappa_{\mathrm{sum}}=\frac{\sum_i |x_i|}{|S|}. \] It is at least 1 by the triangle inequality. It is close to 1 when the terms mostly reinforce one another and large when they largely cancel.
Proof. Divide the theorem’s absolute error bound by \(|S|\), which is nonzero. The resulting right-hand side is \(\gamma_{n-1}\sum_i|x_i|/|S|=\gamma_{n-1}\kappa_{\mathrm{sum}}\). \(\square\)
This result separates two sources of difficulty. The factor \(\gamma_{n-1}\) reflects the accumulation process and the number of additions. The condition number reflects the inputs themselves. Compensation can reduce accumulation error, but a sum whose terms nearly cancel remains sensitive to small perturbations in the contributions.
Worked Example: A Gaussian GLM Score
Let independent responses satisfy \(Y_i\sim N(\beta x_i,1)\), with scalar coefficient \(\beta\). Apart from terms not involving \(\beta\), the log-likelihood is \(-\tfrac12\sum_i(y_i-\beta x_i)^2\), so its score is \(\sum_i x_i(y_i-\beta x_i)\). At \(\beta=0\), take three observations:
| Observation | \(x_i\) | \(y_i\) | Score contribution \(x_i y_i\) |
|---|---|---|---|
| 1 | \(10^8\) | \(10^8\) | \(10^{16}\) |
| 2 | 1 | 1 | 1 |
| 3 | \(-10^8\) | \(10^8\) | \(-10^{16}\) |
The exact score is \(10^{16}+1-10^{16}=1\). If the contributions are accumulated in the table’s order using ordinary double-precision addition, the first two sum to \(10^{16}\), and the final addition gives 0. Neumaier summation retains the middle contribution in its correction and returns 1, as in the preceding calculation. In an actual fit, the score is a vector: the same idea is applied separately to the contributions for each component.
Information matrices also involve sums, with one running sum for each matrix entry. Compensating those sums can help when contributions vary greatly in size. It does not fix a poorly conditioned information matrix or make an invalid contribution valid; those are different numerical issues.
Using Compensation in Practice
Compensation is most useful when many contributions are accumulated, their magnitudes differ substantially, or cancellation is expected. For reproducible computation, also keep the summation order fixed: compensation improves accuracy but does not make floating-point addition associative. In vector and matrix calculations, compensation may be applied entry by entry, using an independent running sum and correction for each entry.
A useful implementation practice is to compare ordinary and compensated totals on the same finite contributions. A discrepancy can indicate that ordinary accumulation discarded meaningful low-order terms. It is also useful to inspect the scale of \(\sum_i|x_i|\) relative to the scale of the final sum. These checks help identify a fragile accumulation without claiming that a particular result is wrong merely because the two totals differ.
Common Mistakes
- Assuming the first addition contributes a rounding factor. In the stated recurrence, the inputs are floating-point numbers, so \(\operatorname{fl}(0+x_1)=x_1\). The first addition is exact; this is why the coefficient of \(x_1\) has at most \(n-1\), not \(n\), nontrivial factors.
- Assuming addition order does not matter. Exact addition is associative, but floating-point addition generally is not. Reordering contributions can change the computed total.
- Treating compensation as a cure for every numerical problem. It addresses loss from summation. It cannot recover information lost when a contribution was computed inaccurately, nor can it prevent overflow in that computation.
- Reading an absolute error bound as a useful relative bound. When the exact sum is near zero, the condition number can be large, so a small absolute error may still be a large relative error.
Check Your Understanding
Answer these questions using the definitions and calculations above.
- Why is the first addition in the recurrence \(s_1=\operatorname{fl}(0+x_1)\) exact when \(x_1\) is a floating-point input?
- For the inputs \(10^{16},1,-10^{16}\), what does ordinary double-precision summation return in the stated order, and why?
- What does a large \(\kappa_{\mathrm{sum}}\) indicate about the terms being added?
- In the Gaussian GLM example, write the score contribution for observation \(i\) at a general value of \(\beta\).
- Name one numerical problem that compensated summation does not solve.