Repository navigation
Description
The survey-weighted Gini in src/policyengine/outputs/inequality.py returns wrong values for income gaps, weight totals, or weighted incomes beyond the float64 maximum, integer weights whose sum wraps, adjacent integer incomes above 2^53, and float32 weights. These cases came up while testing #774, which corrects the estimator and is part of #527.
_gini is private, and its only caller in the package, Inequality.run(), converts incomes and weights to float64 first. So only direct calls to _gini can hit the integer and float32 cases. The float64 cases need an income gap, a weight total, or a weighted income above the float64 maximum, about 1.8e308.
The table compares _gini on main at ddfef2a8 and at the #774 head 728ccf502, run with NumPy 2.1.3 on Python 3.14.7. A run on Python 3.14.10 with the locked dependencies, which pin NumPy 2.5.3, gave the same eight values on the same code. "Exact" is the weighted pairwise Gini in exact arithmetic, rounded to float64.
| Case | Incomes | Weights | Exact | main at ddfef2a | #774 at 728ccf5 |
|---|---|---|---|---|---|
| Float64 income gap | [-9.9792015476736e291, 1.7976931348623157e308] | [1.0, 1.0] | 0.5000000000000001 | 0.5 | inf |
| Float64 weight sum | [0.0, 1.0] | [8.98846567431158e307] × 2 | 0.5 | 1.0 | 0.0 |
| Float64 weighted-income product | [0.0, 4.0] | [4.494232837155791e307] × 2 | 0.5 | nan | 0.0 |
| Int64 weight sum | [0.0, 1.0] | int64 [2^63 - 1, 1] | 1.0 | 2.0 | -1.0 |
| Uint64 weight sum | [0.0, 1.0] | uint64 [2^64 - 1, 1] | 1.0 | 0.0 | 0.0 |
| Float32 weight fractions | [0.0, 1.0] | float32 [0.125, 0.25] | 0.3333333333333333 | 0.2222222155994844 | 0.3333333432674408 |
| Int64 income gap | int64 [2^53, 2^53 + 1] | [1.0, 1.0] | 2.7755575615628914e-17 | 0.0 | 0.0 |
| Uint64 income gap | uint64 [2^53, 2^53 + 1] | [1.0, 1.0] | 2.7755575615628914e-17 | 0.0 | 0.0 |
Seven cases already fail on main. The float64 income gap is new in #774: main returns 0.5, one float step from exact, and #774 returns inf. The weighted-income product case also changes form: main returns nan, a visible failure, and #774 returns 0.0.
Where each case fails at 728ccf5
- Float64 income gap:
np.diff(sorted_values)overflows to inf, and inf divided by the finite weighted total stays inf. - Float64 weight sum:
np.cumsum(sorted_weights)overflows, so the total weight is inf. Every lower-weight fraction becomes 0, and so does the result. - Float64 weighted-income product:
values * weightsoverflows, so_weighted_income_totalreturns inf without its exact fallback. Finite gaps divided by inf give 0.0. - Int64 weight sum:
weights.sum()andnp.cumsumwrap to -2^63, so the lower-weight fraction is -1 and the result is -1.0. - Uint64 weight sum:
weights.sum()wraps to 0, so the zero-weight check returns 0.0. - Float32 weight fractions:
cumulative_weights[:-1] / total_weightstays in float32 and rounds 1/3 to 0.3333333432674408. - Int64 and uint64 income gaps: the cast to float64 rounds 2^53 + 1 to 2^53, so the gap is 0 and the result is 0.0.
Reproducer
import numpy as np
from policyengine.outputs.inequality import _gini
cases = [
("Float64 income gap", [-9.9792015476736e291, 1.7976931348623157e308],
[1.0, 1.0], 0.5000000000000001),
("Float64 weight sum", [0.0, 1.0], [8.98846567431158e307] * 2, 0.5),
("Float64 weighted-income product", [0.0, 4.0], [4.494232837155791e307] * 2, 0.5),
("Int64 weight sum", [0.0, 1.0], np.array([2**63 - 1, 1], dtype=np.int64), 1.0),
("Uint64 weight sum", [0.0, 1.0], np.array([2**64 - 1, 1], dtype=np.uint64), 1.0),
("Float32 weight fractions", [0.0, 1.0], np.array([0.125, 0.25], dtype=np.float32), 1 / 3),
]
for dtype in (np.int64, np.uint64):
cases.append((
f"{dtype.__name__} income gap",
np.array([2**53, 2**53 + 1], dtype=dtype),
[1.0, 1.0],
2.7755575615628914e-17,
))
with np.errstate(over="ignore", invalid="ignore"):
for label, values, weights, expected in cases:
print(label, _gini(np.asarray(values), np.asarray(weights)), expected)Each line prints the case, the returned coefficient, and the exact value.
Done when
- The float64 cases return finite results within float rounding of the exact value.
- Integer weights don't wrap around.
- Adjacent integer incomes above 2^53 stay distinct.
- Float32 weight fractions come out at float64 accuracy.
- Each case has a regression test.
Activity