balance
The balance python package offers a simple workflow and methods for dealing with biased data samples when looking to infer from them to some target population of interest.
balance reweights biased samples toward a target population
A Python package from Meta for fitting survey weights that correct non-response and coverage bias, with diagnostics, outcome estimation, and difference-in-differences support.
What balance does
balance is a Python package that offers a workflow and methods for dealing with biased data samples when inferring from them to a population of interest. The README explains that biased samples arise in survey statistics through non-response or sampling bias, in observational studies when comparing treated and untreated groups, and in any data with selection bias. Under the missing at random assumption, bias can sometimes be partially mitigated by using auxiliary information present for all items in the sample and in a sample from the population. The package fits a weight for each unit in the sample that can be interpreted loosely as the number of people from the target population that unit represents, which helps reduce coverage and non-response bias. The intended audience is researchers who balance biased samples, including survey methodologists, demographers, UX researchers, market researchers, data scientists, statisticians, and machine learners. The workflow runs in steps: load respondent data, load target population data, run diagnostics on covariates, adjust the sample to the target, evaluate the results, use the weights for population estimates, and save the output weights. The README ties the method to a 2023 paper by Sarig, Galili, and Eilat, and notes the package is in beta while actively supported. Installation is from PyPI with pip, and the package builds on Linux, OSX, and Windows for Python 3.9 through 3.14. The dependency list pins numpy, pandas, scipy, scikit-learn, statsmodels, and plotting libraries to version ranges that keep binary compatibility, and the project documents those ranges in its pyproject file so installs stay reproducible across the supported Python versions.
Adjustment and diagnostics methods
The README lists several adjustment methods that balance implements. Logistic regression with L1 penalization is one option. Covariate Balancing Propensity Score, or CBPS, is another. Post-stratification and raking are also provided. For diagnostics the main tools compare the sample before and after applying weights against the target population. Plots include barplots, density plots for weights and covariances, qq-plots, and a love plot that shows per-covariate absolute standardized mean difference before and after, with a reference cutoff. Statistical summaries cover weight distributions such as Kish's design effect and main summaries like mean, median, variance, and quantiles, plus covariate distributions measured by absolute standardized mean difference. The README shows a code example where a sample and target are loaded, the target is set, and the sample is adjusted with a single call. The summary reports covariate ASMD reduction and a design effect. The outcome can then be inspected, with the README printing mean outcomes for the self, unadjusted, and adjusted views. The package also exposes an evaluate step so the user can see how much the weights moved the outcome estimate, which the documentation frames as a check rather than a third estimator. The README also describes a design-based inference path where balance pairs with a separate difference-in-differences library, and it provides an example using a public health panel with sensitivity analysis. The package ships architecture notes and copilot instructions for AI coding assistants, which the README lists under developer resources for those extending the library.
Outcome estimation and design-based inference
Once a sample is reweighted, balance offers more than one way to estimate a target-population outcome mean. The inverse-propensity Hájek weighted mean uses the weighted average of observed outcomes and is consistent when the weights are correct. The outcome-model estimate fits a learner on the responders and applies it to the target covariates, and is consistent when the outcome model is correct. The README states agreement between the two is reassuring while disagreement points at a misspecified model, and a combined AIPW estimator is available as a point estimate with variance and confidence interval deferred. A train and holdout transfer lets the user fit the outcome model on one frame and apply it to a different frame with the same covariate schema. The package also pairs with a separate library called diff-diff for survey-weighted difference-in-differences, consuming balance weights through a SurveyDesign object and returning design-consistent estimates with HonestDiD sensitivity. The thin adapter balance.interop.diff_diff turns the handoff into a single import, installed with an extra. The README points to tutorials, architecture docs, and copilot instructions for AI assistants, and it lists the maintainers from Meta's Central Applied Science team. The documentation site at import-balance.org carries the full guides, and the project cites the 2023 arXiv paper as the methodological reference.
Editorial conclusion
The balance package is released under the MIT license, and the repository's most recent commit was on 2026-08-26. It is hosted at github.com/facebookresearch/balance.
Community notes