Skip to main content

https://bayer-group.github.io/pybalance/index.html

Confounding Adjustment

In scientific experiments, researchers aim to identify cause and effect by holding all variables except one constant. Any difference in outcome can then be attributed to the manipulated variable.

However, in many practical cases, it is not possible to control the variables of interest. For instance, it is unethical to conduct a randomized trial to test the effects of smoking on long-term health outcomes; yet the answer to this question is of extreme interest to policy makers, insurance companies and regulatory agencies. Similarly, in social science research, when studying the impact of education on income, researchers cannot manipulate individuals' education levels while holding all other variables constant.

In these cases, observational data can form the basis for "natural experiments" but care must be taken in interpreting these data. One major issue with interpreting these data is known as "confounding".

A classic example of confounding is the association between coffee consumption and heart disease. Initially, a study might find a positive correlation between high coffee consumption and increased risk of heart disease. However, this apparent relationship could be confounded by the fact that heavy coffee drinkers are more likely to also smoke, which is a known risk factor for heart disease. In this case, smoking acts as a confounding variable, as it distorts the true relationship between coffee consumption and heart disease. To address this, researchers need to adjust for smoking status and potentially other relevant variables to accurately assess the independent impact of coffee consumption on heart disease risk.

In general, any comparative analysis of two non-randomized populations will differ systematically in a number of covariate dimensions and these systematic differences must be adjusted for as part of any causal inference analysis. That is where pybalance comes in.

PyBalance

pybalance is a suite of tools in python for performing confounding adjustment in non-randomized populations. In pybalance, we start with measures of "balance" (how similar two populations are) and directly optimize this metric. This approach is different, and we think almost always better, from the well-known propensity score approach, in which the probability of treatment assignment is modelled, but balance metrics are almost always anyway implicitly defining the success criterion (see our demo). Our approach here is to explicitly define and directly optimize the balance metric that is relevant for the given problem.

The pybalance library implements several routines for optimizing balance. To learn more about these methods, head on over to the demos. Then give the code a spin for yourself by following the installation instructions. Any questions or issues please feel free to open an issue or start a discussion.

An application of this library to build an external control arm in a pharmaceutical setting is presented here.

Features

  • Implements linear and non-linear optimization approaches for matching.
  • Utilizes integer program solvers and evolutionary solvers for optimization.
  • Includes implementation of propensity score matching for comparison.
  • Offers a variety of balance calculators and matchers.
  • Implements weighting methods, including Matching-Adjusted Indirect Comparison (MAIC), general entropy-balancing weights, and propensity-score (IPTW) weights, for use cases where patients should be reweighted rather than dropped.
  • Provides visualization tools for analysis.
  • Supports simulation of datasets for testing and demonstration purposes.

Limitations

Weighting support currently covers MAIC, general entropy balancing, and IPTW. Other common estimators (overlap/matching weights, CBPS, stabilized IPTW) are not yet implemented.

Citation

If you use pybalance for your research, please acknowledge this with a citation to our paper.

Metadata

Release files for pybalance 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybalance 0.3.0
File Size Uploaded Provenance Actions
pybalance-0.3.0.tar.gz 8.6 MB With provenance Details for pybalance-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pybalance 0.3.0
File Interpreter ABI Platform Actions
pybalance-0.3.0-py3-none-any.whl Python 3 none any Details for pybalance-0.3.0-py3-none-any.whl

Total release size: 17.3 MB

Release files / pybalance-0.3.0.tar.gz

Download URL pybalance-0.3.0.tar.gz
Size 8.6 MB
Tags Source
SHA-256 checksum
How to use checksums
aefbfe65f41420a7f7e840ff59930983d8d7e20b0f79904748f069b1f9338517
BLAKE2b-256 checksum
How to use checksums
ac962ead8fc35b3dcd5343295c74c3e73e82803fc5260aabd8260251e04ac1b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 22, 2026.

Transparency log

Release files / pybalance-0.3.0-py3-none-any.whl

Download URL pybalance-0.3.0-py3-none-any.whl
Size 8.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
109328790b1e29ae5110fda30663bc0d9a73fa85e3a0d42b8fa7f1d6ccaceb17
BLAKE2b-256 checksum
How to use checksums
a23e17053d6295436f0b03072048c1b560132fa6ea9c4f0dc88be24bd5bb0f50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Oct 22, 2026.

Transparency log

Security for pybalance 0.3.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pybalance-0.3.0.tar.gz

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 Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page