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.
  • Provides visualization tools for analysis.
  • Supports simulation of datasets for testing and demonstration purposes.

Limitations

At the moment, pybalance only implements matching routines. Suport for weighting methods is on our roadmap and will appear in a future release.

Citation

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

Metadata

Release files for pybalance 0.2.3

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.2.3
File Size Uploaded
pybalance-0.2.3.tar.gz 8.6 MB Details

Built distribution (wheel)

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

Total release size: 17.2 MB

Release files / pybalance-0.2.3.tar.gz

Download URL pybalance-0.2.3.tar.gz
Size 8.6 MB
Tags Source
SHA-256 checksum
How to use checksums
74fcfff554aa9c102611097c566dd081d5d745b7816621d270b0079f42c0973c
BLAKE2b-256 checksum
How to use checksums
e0acd30fa48400f2836341baf24a84d1689ccc5db38ba0ed3ca983e7eb744159
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 May 26, 2025.

Transparency log

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

Download URL pybalance-0.2.3-py3-none-any.whl
Size 8.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
363ebe1c9fe12152a87813c711984671542cad4045eec3a2b3a4c38b8cf083c6
BLAKE2b-256 checksum
How to use checksums
8809b316b1a2a5ed6042e3a7816e4a939a95429c7bd44ea0663da38c9bf5398e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 May 26, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.5

2 release files

0.1.1

2 release files

0.1.0

2 release files

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