Skip to main content

Leximin calibration for survey weights

Project description

fairlex

PyPI version PyPI Downloads Python 3.8+ License: MIT

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 error first (then the next worst, etc.) and can also cap how far any weight is allowed to move from its base value.

Why use it?

When exact calibration is infeasible under weight caps.

  1. When targets are noisy/inconsistent and you want bounded misses rather than fragile exact hits.
  2. When you need fairness/stability—no margin (or subgroup) becomes the sacrificial lamb.
  3. 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 – finds weights that minimise the worst absolute deviation from the target margins (min–max residuals). This can drive margin errors down to machine precision, but may result in large weight adjustments.
  • Weight‐fair leximin – first performs residual leximin, then minimises the largest relative change from the base weights while keeping residuals at their optimum level. This yields a more stable set of weights.

Installation

fairlex requires Python 3.8+ and depends on numpy and scipy. You can install it via pip once uploaded to PyPI:

pip install fairlex

For development, clone this repository and install the dependencies:

git clone https://github.com/finite-sample/fairlex.git
cd fairlex
pip install -e .[dev]

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)

evaluate_solution returns a dictionary with a variety of diagnostics, including the maximum absolute residual, effective sample size (ESS), design effect and quantiles of the weight distribution. If you supply the base weights via base_weights, it also reports relative deviations from the original weights.

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

fairlex-0.1.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

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

fairlex-0.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fairlex-0.1.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for fairlex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a1b54af1c94ac0d3a2bff0ddcf6810223bd7db7e93a7cabe5aac6c11b2bcc545
MD5 68461a1b7567b1db5c9540a67987d9d7
BLAKE2b-256 ac433c097c188374a7651d52b40ba7cacee0e03fa9fb4aac360108ca49883ec3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fairlex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for fairlex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47883db76670f0e36205008225d891a6a66a22749a1ce1b28dc5ded0f494ad79
MD5 aa69afae8a1e31431718b54eebad28ed
BLAKE2b-256 706869b30946ef4561427ce5f641c33f2155dcd94208728d5880849deeb66df6

See more details on using hashes here.

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