Skip to main content

Rank-Order Segregation Index for Continuous Variables

Use

Python

# pip install segindex
from segindex import estimate_Hp

# Say, each variable is income.
# Inner lists of each area stand for sectors.
# Each area has three sectors in this case.
# How much is each area segregated by sectors in terms of income?
area1 = [[80, 80, 70, 70], [50, 45, 40], [20, 20, 20, 10]]
area2 = [[80, 70, 50], [80, 70, 45, 20, 20], [40, 20, 10]]

print(estimate_Hp(area1))
print(estimate_Hp(area2))

>> 0.7182
>> 0.3191
# area1 is more income-way segregated than area2. In other words, area2 is more mixed.

Rust

The same algorithm is available as a pure Rust crate in rust/. (The demo page's wasm-bindgen bindings live separately in page/wasm/.)

// cargo add segindex
let area1 = vec![
    vec![80.0, 80.0, 70.0, 70.0],
    vec![50.0, 45.0, 40.0],
    vec![20.0, 20.0, 20.0, 10.0],
];
let h_r = segindex::estimate_hp(&area1, None, 14, 4); // 0.7182

Both implementations are pinned to the same golden test values (python/tests/test_segindex.py, rust/tests/golden.rs) and agree within 1e-8.

Description

  • Many kinds of segregation index are used for various purposes like from policies to studies. While there are a wide range of categorical variables like race group to meausre an amount of segregation, continuous values like income are also important but do not fit very well with categorical segregation index.

  • Reardon(2011), Reardon and Bischoff(2011) propsed a rank-order segregation index based on Theil index which is based on the concept of Entropy. This index is widely accepted for practical and academic uses to calculate continuous value based segregation index like in Chetty et al. 2014.

  • The proposed method of them is a bit intricate however and there seems to be no good online library or code that implements it. Therefore, here is one. Python codes inside python/segindex/segregation_index.py implement the Rank-Order Information Theory Index of Reardon(2011); rust/src/lib.rs is the Rust port.

  • Essentials of the Index

    • The inequality index H is an average of each value from a total of K sectors, which is total region's entropy(E) minus each sector's entropy(E_K). It is weighted by each sector's relative popultaion size(t_k/T). Here the entropy stands for how equally variables(ex. income) are distributed over sectors.

    $$ H = \Sigma*{k=1}^{K} \frac{t*{k}}{T} \frac{E-E_{k}}{E} $$

    • Below is an equation to calculate entropy when there is two groups. p is a ratio of a group. As the variable here is continuous not categorical, one needs to integrate the below equation over p with a range of 0≤p≤1. Thus transformation of raw values into rank ordered values is required.

    $$ E(p) = -(p\log*{2}^{p} + (1-p)\log*{2}^{(1-p)}) $$

    • Combining above equations, we can calculate below one to get a Rank-Order Information Theory Index, which is the segregation index for continuous variables. 0 means perfect equality. 1 means perfect segregation.

    $$ H^R = \int_{0}^{1} \frac{E(p)}{\int_{0}^{1} E(q),dq} H(p) ,dp \ = 2 \log_{2}^{\int_{0}^{1} E(p)H(p) ,dp} $$

Repository layout & development

  • python/ — the Python package (pip install segindex). Tests: pip install pytest then pytest at the repo root.
  • rust/ — the Rust crate (pure library, no wasm code). Tests: cargo test at the repo root (cargo workspace: rust/ + page/wasm/).
  • page/ — the demo page: page/wasm/ is a small wasm-bindgen binding crate over rust/, and page/app/ is a SolidJS + Vite app (pnpm). pnpm install && pnpm run build inside page/app/ runs wasm-pack and builds the site into docs/.
  • docs/ — the built demo page, served by GitHub Pages (source: main branch, /docs folder). Do not edit by hand.
  • notes/ — design notes, e.g. notes/weighted-rank-review.md on the v0.2.0 weighted-rank change.

Version Logs

  • v.0.2.0:
    • Python: dropped statsmodels/pandas/scipy dependencies (numpy only), removed a leaked global, vectorized internals — unweighted results are unchanged and pinned by golden tests.
    • Fixed the weighted path (ww_s): percentiles now follow the cumulative weight distribution instead of ranking value×weight products, and sector sizes/shares use weighted sums. Weighted results change; see notes/weighted-rank-review.md.
    • Added a Rust implementation (rust/).
    • Added an interactive demo page (SolidJS + Rust/WASM, built from page/ into docs/ for GitHub Pages).
  • v.0.1.4: minor modification of README file

Metadata

Release files for segindex 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for segindex 0.2.0
File Size Uploaded Provenance
segindex-0.2.0.tar.gz 8.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for segindex 0.2.0
File Interpreter ABI Platform
segindex-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / segindex-0.2.0.tar.gz

Download URL segindex-0.2.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a08fb685d4f8eb387876459ee1aa7c6c7d2c966444f7791f24b9015ca40958f9
BLAKE2b-256 checksum
How to use checksums
36b14e3252ff9d85ed8e07e937f258004c11baa7339e7e9a351f6a8853a1a618
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Release files / segindex-0.2.0-py3-none-any.whl

Download URL segindex-0.2.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1320849229d9eaaf92a8cf7e6afa4610ce5bc5d90edb9f5cc77f45c2835140dd
BLAKE2b-256 checksum
How to use checksums
5ab088f8efd1ad51846ea4ac8666d157fe34f386da0e037c710ffdbfe00f5e1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Security for segindex 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page