A nonparametric index of stratification (Zhou 2012): Python port of the R package 'strat'
Project description
stratindex
A nonparametric index of stratification (Zhou 2012) — a Python port of the R package strat by Xiang Zhou.
Documentation: https://promsoft.github.io/stratindex/
The index measures how strongly a set of strata (social classes, occupations, schools, …) stratifies a numeric outcome (income, test scores, …). It is the weighted excess of concordant over discordant pairs among all pairs of observations drawn from different strata with distinct outcomes — an analogue of Somers' D where strata are ordered by the average percentile rank of their members (or taken as pre-ordered). The index lies in [-1, 1]: 1 means perfect stratification (every member of a higher stratum outranks every member of a lower one), 0 means no stratification.
Zhou, Xiang. 2012. "A Nonparametric Index of Stratification." Sociological Methodology, 42(1): 365–389. doi:10.1177/0081175012452207
Installation
pip install stratindex
Requires Python ≥ 3.12. The only runtime dependency is NumPy.
Usage
from stratindex import strat, srank, load_cpsmarch2015
d = load_cpsmarch2015() # bundled example data: March CPS 2015, 14,358 men
# stratum-specific information: population share and average percentile rank
print(srank(d["income"], d["big_class"], weights=d["weight"]))
# the stratification index with a between-/within-group decomposition
s = strat(
d["income"], d["big_class"],
weights=d["weight"],
group=d["education"], group_name="education",
)
print(s.format(digits=4))
overall stratification:
strat std_error
0.4128 0.01296
decomposition by education:
weight strat
within education 0.2435 0.2684
between education 0.7565 0.4592
Results are plain dataclasses: s.strat, s.std_error, s.strata_info,
s.decomposition, s.within_group; in Jupyter they render as HTML tables.
If pandas is installed, s.to_pandas() returns the tables as DataFrames, and
load_cpsmarch2015(as_pandas=True) returns a DataFrame.
A DataFrame (or any mapping of columns, like the dict above) can be passed directly — string keywords are resolved as column names, and the group label is taken from the column name:
s = strat(d, outcome="income", strata="big_class",
weights="weight", group="education")
pandas Categorical strata keep their category order (used by
ordered=True and for row order in the tables). Besides the default
Goodman–Kruskal approximation, a bootstrap standard error is available:
strat(..., se_method="bootstrap", n_boot=500, random_state=0).
Correspondence with the R package
| R | Python |
|---|---|
strat(outcome, strata, weights, ordered, group) |
strat(outcome, strata, weights=None, ordered=False, group=None, group_name="group") |
srank(outcome, strata, weights, group) |
srank(outcome, strata, weights=None, group=None) |
s$overall (strat, std_error) |
s.strat, s.std_error (or s.overall) |
s$strata_info |
s.strata_info |
s$decomposition |
s.decomposition |
s$within_group |
s.within_group |
data(cpsmarch2015) |
load_cpsmarch2015() |
Behavioral notes:
- Strata and group levels are ordered by their sorted unique values (as R's
factor()does for character vectors). Withordered=Truethis level order is the stratum order. - R derives the group label from the expression passed as
group; Python uses the column name in data mode (orgroup_name=explicitly). - The pairwise comparisons (an O(n²) C++ loop in the original) are computed in O(n log n) via weighted inversion counting; the full 14,358-row example runs in a fraction of a second.
- Numerical output is cross-validated against the original R package
(see
tests/data/r_golden.json, regenerated withscripts/r_golden.R).
Standard error
The reported standard error is the approximation of Goodman & Kruskal (1963),
as in the R package: se = sqrt((1 - strat²) · n / deno) where deno is the
total weight of comparable pairs.
License
GPL-3.0-or-later, same as the original R package (this is a derivative work).
The bundled cpsmarch2015 dataset originates from the March 2015 Current
Population Survey (U.S. Census Bureau / BLS public data), as distributed with
the R package.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stratindex-0.2.0.tar.gz.
File metadata
- Download URL: stratindex-0.2.0.tar.gz
- Upload date:
- Size: 237.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8da1b1c939f0d086a9c2be92d97f178a0b41e40494be734e61e2de86074a7c65
|
|
| MD5 |
f3d5d6581c457c18da348472cfd5c977
|
|
| BLAKE2b-256 |
b6281d02f230ce2805cfbf95d80f42570f217437241734a2ed253f3a19ab19f5
|
Provenance
The following attestation bundles were made for stratindex-0.2.0.tar.gz:
Publisher:
publish.yml on promsoft/stratindex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stratindex-0.2.0.tar.gz -
Subject digest:
8da1b1c939f0d086a9c2be92d97f178a0b41e40494be734e61e2de86074a7c65 - Sigstore transparency entry: 2158576304
- Sigstore integration time:
-
Permalink:
promsoft/stratindex@2ddb905ea8a9cf0aa87676443b2accb81a9f00f3 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/promsoft
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2ddb905ea8a9cf0aa87676443b2accb81a9f00f3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file stratindex-0.2.0-py3-none-any.whl.
File metadata
- Download URL: stratindex-0.2.0-py3-none-any.whl
- Upload date:
- Size: 223.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e66baf15e41d016c996ce75546c2f6aeb594e42daa46c88a58cb11fca47a8285
|
|
| MD5 |
fc8001d7abb7e8dcbba25dce2818f055
|
|
| BLAKE2b-256 |
8483bb1fe6ae45976ed682ce4358e97bf8e730a982677c0e12d876df7b9a40e0
|
Provenance
The following attestation bundles were made for stratindex-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on promsoft/stratindex
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stratindex-0.2.0-py3-none-any.whl -
Subject digest:
e66baf15e41d016c996ce75546c2f6aeb594e42daa46c88a58cb11fca47a8285 - Sigstore transparency entry: 2158576415
- Sigstore integration time:
-
Permalink:
promsoft/stratindex@2ddb905ea8a9cf0aa87676443b2accb81a9f00f3 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/promsoft
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2ddb905ea8a9cf0aa87676443b2accb81a9f00f3 -
Trigger Event:
push
-
Statement type: