Skip to main content

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.

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. If pandas is installed, s.to_pandas() returns the tables as DataFrames, and load_cpsmarch2015(as_pandas=True) returns a DataFrame.

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). With ordered=True this level order is the stratum order.
  • R derives the group label from the expression passed as group; pass group_name= explicitly in Python.
  • The O(n²) pairwise kernel (C++ in the original) is implemented as blocked, vectorized NumPy; the full 14,358-row example runs in a few seconds.
  • Numerical output is cross-validated against the original R package (see tests/data/r_golden.json, regenerated with scripts/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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stratindex-0.1.0.tar.gz (231.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stratindex-0.1.0-py3-none-any.whl (219.8 kB view details)

Uploaded Python 3

File details

Details for the file stratindex-0.1.0.tar.gz.

File metadata

  • Download URL: stratindex-0.1.0.tar.gz
  • Upload date:
  • Size: 231.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for stratindex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 833a32201b5653f71fdc7ca8d389064d3578af5339549a768cada1063c57f08f
MD5 c2db5cef77ab53670ac04627740d4740
BLAKE2b-256 d0ee9177f4863f22783ba9ae1650f3cdee5ed9363743da08d0a24d24422c1dc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for stratindex-0.1.0.tar.gz:

Publisher: publish.yml on promsoft/stratindex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file stratindex-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: stratindex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 219.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for stratindex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27538520889d7e2295e4382355aa1006fc669e583e3fc328d5f6774c1920d87e
MD5 a8e1eecf60d4645b7b76293dc83b4f70
BLAKE2b-256 378d34964716ad5f4ac0c9acccd50c7160a69df7ba44c551aec9aad689e847d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for stratindex-0.1.0-py3-none-any.whl:

Publisher: publish.yml on promsoft/stratindex

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page