fairlex: leximin calibration
fairlex implements risk-averse calibration of survey weights using leximin objectives.
Unlike standard calibration that either (a) hits all margins exactly (sometimes creating
spiky weights) or (b) accepts uneven misses, leximin prioritizes uniform guarantees: it
shrinks the worst margin miss first, then the next worst, and so on, with every weight
kept within a fixed ratio of its base value. Misses are compared as a percentage of each
target by default, so a small group's miss is not swamped by the population total.
Why use it?
When exact calibration is infeasible under weight caps.
- When targets are noisy/inconsistent and you want bounded misses rather than fragile exact hits.
- When you need fairness/stability—no margin (or subgroup) becomes the sacrificial lamb.
- In rolling waves, to prevent whiplash by bounding the worst per-unit weight changes.
fairlex is designed to be both easy to use and
flexible enough to support different calibration objectives. The two
principal calibration strategies are:
- Residual leximin (
leximin_residual) – finds weights whose margin misses are leximin-optimal: the largest miss is as small as the weight bounds allow, then the second largest, and so on. Many weight vectors achieve those misses; this function returns one of them. - Weight‐fair leximin (
leximin_weight_fair) – keeps every margin at its leximin miss (plus an optionalslack) and, among those weights, makes the relative weight changes|w - w0| / w0leximin-optimal too. A group's adjustment is spread evenly over its members, and units no margin needs to move keep their base weight.
Both are solved as sequences of linear programmes (SciPy's HiGHS) over the
distinct membership patterns in A, so with 0/1 margins the cost barely
depends on the number of respondents.
Installation
fairlex requires Python 3.12+ and depends on numpy>=1.26 and
scipy>=1.12. Install it from PyPI:
pip install fairlex
For development, clone this repository and sync the environment with uv:
git clone https://github.com/finite-sample/fairlex.git
cd fairlex
uv sync --all-groups
make help lists the available development targets; make ci runs the same
checks CI does.
Usage
Construct a membership matrix A of shape (m, n), where each row
corresponds to a margin and each column to a survey unit. Each entry
represents whether the unit belongs to the margin (1.0 or 0.0 for simple
groups). Supply the target totals b, the base weights w0 and call
the desired calibration function:
import numpy as np
from fairlex import leximin_weight_fair, evaluate_solution
# Example data: two margins (sex and age) plus total
A = np.array(
[
# sex: female
[1, 0, 1, 0, 1],
# sex: male
[0, 1, 0, 1, 0],
# age: young
[1, 1, 0, 0, 1],
# age: old
[0, 0, 1, 1, 0],
# total
[1, 1, 1, 1, 1],
],
dtype=float,
)
target = np.array([6, 4, 6, 4, 10], dtype=float) # Feasible targets
w0 = np.array([1, 1, 1, 1, 1], dtype=float)
# Calibrate using weight‐fair leximin
res = leximin_weight_fair(A, target, w0, min_ratio=0.5, max_ratio=2.0)
# Inspect the weights and diagnostics
weights = res.w
metrics = evaluate_solution(A, target, weights, base_weights=w0)
print(metrics)
res.residuals holds the raw misses A @ w - b, res.epsilon the
largest miss as a fraction of its target (scale="absolute" compares raw
misses instead) and res.t the largest relative weight change.
evaluate_solution returns a dictionary with a variety of diagnostics,
including the largest absolute and relative residual, effective sample size
(ESS), design effect and the requested quantiles of the weight distribution
(weight_p99, weight_p95, weight_p50 by default). If you supply
the base weights via base_weights, it also reports relative deviations
from the original weights, over units whose base weight is positive.
Metadata
Release files for fairlex 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fairlex-0.4.0.tar.gz | 13.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fairlex-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.5 kB
Release files / fairlex-0.4.0.tar.gz
| Download URL | fairlex-0.4.0.tar.gz |
|---|---|
| Size | 13.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
744e659c193107fe1ddd89235178a2a5bf0bfe7c2767f211f07012249a37713e
|
|
BLAKE2b-256 checksum How to use checksums |
57e41c2d7de6a7b84c53c81fd55621e3ab0472922cedfe0f43632b4bd49a41a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logRelease files / fairlex-0.4.0-py3-none-any.whl
| Download URL | fairlex-0.4.0-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
461f94f39d81a181fccb35f86421f9a1cd1b0504ec2cb56907510913d490d16a
|
|
BLAKE2b-256 checksum How to use checksums |
3f5c22202bc3acefde4729beef123f609e56350cb0a45f9fd88d63ff60fcd0d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency log