Skip to content

Rewrite Gini coefficient in O(n log n) - #937

Open
russlan23 wants to merge 1 commit into
QuantEcon:mainfrom
russlan23:agent/rewrite-gini-nlogn
Open

Rewrite Gini coefficient in O(n log n)#937
russlan23 wants to merge 1 commit into
QuantEcon:mainfrom
russlan23:agent/rewrite-gini-nlogn

Conversation

@russlan23

Copy link
Copy Markdown

Replaces the quadratic pairwise implementation with the sorted closed-form calculation requested in #926. This removes the module's only prange use and the parallel=True compilation 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 passed
  • flake8 --select=F401,F405,E231 quantecon
  • Independent agreement checks against the pairwise definition for n = 4, 10, 100, and 1,000

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

@mmcky

mmcky commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

thanks for your PR @russlan23

@kp992 can you please review this as part of the WASM project

@mmcky mmcky added the review label Aug 14, 2026
@mmcky
mmcky requested a balanced review from Copilot August 20, 2026 11:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 abs double loop with the sorted formula 2·Σ(i·y_i)/(n·Σy) − (n+1)/n, and dropped the parallel=True flag and prange.
  • 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.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 90.608% (+0.04%) from 90.57% — russlan23:agent/rewrite-gini-nlogn into QuantEcon:main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WASM: remove @njit(parallel=True) from gini_coefficient via an O(n log n) rewrite

5 participants