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.
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.