Empirical Calibration (EC) is a Python library (version 0.12) designed for correcting bias in data samples using generic weighting methods. It formulates the calibration problem as a convex optimization, solved efficiently in a dual form, and aims to reduce data biases in various statistical fields, such as survey sampling and causal studies with observational data. The library is actively maintained, with the latest release in May 2024 and ongoing development on GitHub.
pip install empirical-calibrationVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `empirical_calibration` to compute sample weights. It simulates two sets of covariates: `covariates_sample` representing your biased data and `target_covariates` representing the desired distribution (e.g., from a population). The `maybe_exact_calibrate` function then calculates weights for the sample data such that its weighted covariate distribution matches the target distribution as closely as possible, using the specified optimization objective (here, `ENTROPY`).
Ensure both inputs have identical structure (column names, order, and data types) for the variables intended for calibration. Consider explicit type conversions or column reordering if loading from different sources.
Consider adjusting the `objective` (e.g., `QUADRATIC` might be more robust for some problems), `epsilon` (tolerance for matching marginals), or `max_iter` parameters in `calibrate` or `maybe_exact_calibrate`. Pre-processing covariates (e.g., binning continuous variables, handling rare categories) can also improve convergence.
Always verify that documentation or examples found online pertain to the Python `empirical-calibration` library (often associated with `google/empirical_calibration` on GitHub) to avoid applying incorrect API calls or concepts.