kups.core.utils.kahan
¶
Compensated summation as a composable accumulator.
This module wraps Kahan's compensated summation in a small value type. Adding
to a KahanSummand returns a new summand carrying both the running sum and
the rounding error lost so far, reducing floating-point drift when many values
are accumulated.
KahanSummand
¶
Numerically stable accumulator for repeated addition.
Holds a running sum together with a compensation term that captures the
low-order bits dropped by rounding at each step. The + operator (and
+=) accumulates a value and returns a new KahanSummand, so it can be
folded over an iterable or passed to sum like a plain number.
Values may be arbitrary PyTrees, mirroring kahan_summation.
An infinite value carries a zero compensation, so an infinite total stays infinite rather than becoming NaN.
Attributes:
| Name | Type | Description |
|---|---|---|
value |
T
|
Current running sum as a PyTree. |
compensate |
T
|
Accumulated rounding error; the best estimate of the true
sum is |
Example
Source code in src/kups/core/utils/kahan.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
total
property
¶
Best single-value estimate of the sum, value - compensate.
__add__(other)
¶
Accumulate a value or another summand.
Adding a plain value applies the compensation to the addend, which keeps the running sum close to the true total when many small values are accumulated. Adding another summand instead adds the two compensations to each other and folds in the exact rounding error of the value addition: the addend is then of comparable magnitude, and subtracting a compensation from it would round the compensation away entirely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
KahanSummand[T] | T
|
A PyTree to add, or another |
required |
Returns:
| Type | Description |
|---|---|
KahanSummand[T]
|
A new summand with the updated running sum and compensation. |
Source code in src/kups/core/utils/kahan.py
__mul__(other)
¶
Scale the running sum and its compensation by other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
float
|
Scalar factor. |
required |
Returns:
| Type | Description |
|---|---|
KahanSummand[T]
|
A summand representing |
Source code in src/kups/core/utils/kahan.py
__radd__(other)
¶
Accumulate from the left so sum and other + summand work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
KahanSummand[T] | T
|
A value to add from the left, e.g. the |
required |
Returns:
| Type | Description |
|---|---|
KahanSummand[T]
|
A new summand with the updated running sum and compensation. |
Source code in src/kups/core/utils/kahan.py
__rmul__(other)
¶
difference(other)
¶
Difference self - other of two accumulators.
Subtracts the running sums and the compensations separately before
combining them. For accumulators that differ by a small increment this
recovers the increment to full precision: the running sums cancel
exactly, and the low-order bits each of them dropped are carried by the
compensations. Folding the compensations in first (as + does) would
instead annihilate them against the much larger running sums.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
KahanSummand[T]
|
Accumulator to subtract. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The difference as a PyTree, without compensation. |
Source code in src/kups/core/utils/kahan.py
init(value)
classmethod
¶
Create a summand seeded with value and zero compensation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
Initial running sum as a PyTree. |
required |
Returns:
| Type | Description |
|---|---|
KahanSummand[T]
|
A summand starting from |