Repository navigation
Rewrite Gini coefficient in O(n log n) - #937
Conversation
|
thanks for your PR @russlan23 @kp992 can you please review this as part of the WASM project |
There was a problem hiding this comment.
Pull request overview
This PR rewrites gini_coefficient in quantecon/_inequality.py from a quadratic pairwise double-loop into the O(n log n) sorted closed-form calculation. This removes the module's only use of Numba's @njit(parallel=True)/prange, which is the ParallelAccelerator path that fails in the single-threaded in-browser (emscripten) Numba runtime, while also delivering a large algorithmic speedup. It addresses issue #926 as part of the Phase 1 WASM/browser-compliance work.
Changes:
- Replaced the pairwise
absdouble loop with the sorted formula2·Σ(i·y_i)/(n·Σy) − (n+1)/n, and dropped theparallel=Trueflag andprange. - Narrowed the Numba import to
from numba import njit. - Added a fixed-array regression test pinning the closed form against the pairwise definition.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
quantecon/_inequality.py |
Rewrites gini_coefficient to the O(n log n) sorted closed form and removes the prange/parallel=True usage and import. |
quantecon/tests/test_inequality.py |
Adds a regression test asserting agreement with the pairwise Gini definition on a small fixed array. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Thanks @russlan23 for your contribution and AI disclosure (greatly appreciated). Verified the new closed form against the old pairwise implementation at the current head (worst absolute difference 1.2e-15 across Pareto, lognormal, all-zero, all-equal, mixed-sign and integer inputs), CI is green across the 3.12/3.13/3.14 matrix — marking ready. |
Catches the @-operator migration up to main (#831, #937, #947, #949). Conflict in quantecon/tests/test_matrix_eqn.py: #831 appended test_solve_discrete_riccati_system_beta_ge_one_failure_message directly after the assert this branch rewrote to the @ operator. Kept both sides. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Replaces the quadratic pairwise implementation with the sorted closed-form calculation requested in #926. This removes the module's only
prangeuse and theparallel=Truecompilation path that blocks the in-browser runtime.The existing Pareto and Weibull assertions remain unchanged. I added a fixed-array regression against the pairwise definition.
Validation:
pytest -q quantecon/tests/test_inequality.py— 4 passedflake8 --select=F401,F405,E231 quanteconThe separate Phase 0 browser deployment in #928 is still open, so that project-level browser check remains outstanding.
Closes #926.
AI assistance: Used for implementation support and test execution; I reviewed the diff and validation results.