Skip to main content

HypEx: Advanced Causal Inference and AB Testing Toolkit

PyPI version Coverage gate Documentation Python versions License Telegram

HypEx (Hypotheses and Experiments) is a library for causal inference and AB testing. It runs the same experiments on pandas for everyday analysis and on Apache Spark for data that does not fit on one machine.

What's new in 2.0

HypEx 2.0 adds an Apache Spark backend, is substantially faster than 1.0.x (most of all on large data, in A/A loops and in matching), and extends the statistical toolkit. See the release notes for the full list.

  • Spark backend. Dataset runs on pandas or Spark with the same API for AA tests, AB tests, homogeneity tests and matching.
  • Speed. Vectorised splitting, Spark checkpointing, batched statistical tests, FAISS shuffle mode and co-partitioned search for distributed matching.
  • New statistics and blocks. StatsUTest, matching bias correction and metrics, NaDropper, Float32Caster, AATest(dry_test=...).
  • CUPED and CUPAC variance reduction in ABTest, with a variance reduction report.
  • Multiple-testing corrections in ABTest (Holm by default, plus Bonferroni, Sidak, FDR and others).
  • Breaking changes against 1.0.7: Python 3.13 is not supported, Output.resume is now Output.summary, A/A splits use a different hash, Matching extracts full data and indexes only on request. Version 0.1.x is no longer supported (pip install hypex==0.1.10).

Introduction

HypEx employs Rubin's Causal Model (RCM) for matching closely related pairs, ensuring equitable group comparisons when estimating treatment effects. Its automated pipeline calculates the Average Treatment Effect (ATE), Average Treatment Effect on the Treated (ATT) and Average Treatment Effect on the Control (ATC), with a standardized interface for running the estimations.

Beyond causal inference, HypEx provides AA tests, homogeneity tests and AB tests (including A/B/n tests with multiple testing corrections and CUPED/CUPAC variance reduction) to rigorously test hypotheses and validate experimental results.

Features

  • Matching: Faiss-based nearest neighbor search (Mahalanobis or L2 distance), several neighbors per object, feature weights, matching inside groups, optional categorical encoding and bias estimation.
  • Matching quality tests: SMD, KS, PSI, Repeats, t-test and chi2-test to check the robustness of the matching.
  • AA test: repeated random splits with a selection of the most homogeneous split, optional stratification, early stopping and custom group sizes.
  • Homogeneity test: compares target and feature distributions between groups.
  • AB test: group difference with t-test, u-test and chi2-test, A/B/n tests with multiple testing corrections, CUPED and CUPAC variance reduction with a detailed report.
  • Roles: describe your data once (TreatmentRole, TargetRole, FeatureRole, StratificationRole, InfoRole) and every experiment picks what it needs.
  • Pandas and Spark backends behind one Dataset API.

Warnings

Some functions in HypEx help to solve auxiliary tasks but cannot automate decisions on experiment design.

Note: For Matching, it is recommended to use no more than 7 features: more may lead to the curse of dimensionality and make the results unrepresentative.

Installation

pip install -U hypex

To pin this release: pip install hypex==2.0.0.

Optional extras for Spark and CUPAC models:

pip install -U "hypex[spark]"  # Spark backend
pip install -U "hypex[cat]"    # CatBoost
pip install -U "hypex[lgbm]"   # LightGBM
pip install -U "hypex[all]"    # everything above

Requirements: Python >=3.8, <3.13. The base installation uses pandas and does not include PySpark. The spark extra installs PySpark 3.5.7; running on Spark also needs Java (8, 11 or 17). Requesting the Spark backend without the extra raises an error with the installation command.

Quick start

Explore usage examples and tutorials here.

Matching example

from hypex.dataset import Dataset, InfoRole, TreatmentRole, TargetRole, FeatureRole
from hypex import Matching

data = Dataset(
    roles={
        "user_id": InfoRole(int),  # InfoRole for ID
        "treat": TreatmentRole(int),  # TreatmentRole identifies the group (control or target)
        "post_spends": TargetRole(float),  # TargetRole for the target
    },
    data="data.csv",
    default_role=FeatureRole(),  # All remaining columns are features (used to search for similar objects)
)

test = Matching()  # Classic matching (Mahalanobis distance + quality tests)
test = Matching(distance="l2")  # Choose the distance
test = Matching(n_neighbors=3)  # Several neighbors per object
test = Matching(group_match=True)  # Match inside groups of categorical features
test = Matching(weights={"age": 2.0})  # Custom feature weights
test = Matching(extract_full_data=True, compute_indexes=True)  # Also build full_data and indexes

result = test.execute(data)
result.summary  # Summary of results (ATE, ATT, ATC)
result.quality_results  # Matching quality tests
result.full_data  # Wide dataset with pairs (requires extract_full_data=True)
result.indexes  # Matched pairs of indexes, good for join (requires compute_indexes=True)

full_data and indexes are skipped by default because they are expensive on large data.

More about Matching here

AA-test example

from hypex.dataset import Dataset, InfoRole, TargetRole, StratificationRole
from hypex import AATest

data = Dataset(
    roles={
        "user_id": InfoRole(int),  # InfoRole for ID
        "pre_spends": TargetRole(),  # TargetRole for the homogeneity check
        "post_spends": TargetRole(),  # TargetRole for the homogeneity check
        "gender": StratificationRole(str),  # StratificationRole for strata
    },
    data="data.csv",
)

aa = AATest(n_iterations=10)
result = aa.execute(data)

result.summary  # Summary of the whole test
result.aa_score  # AA score
result.best_split  # The most homogeneous split
result.best_split_statistic  # Statistics of the best split

More about AA test here

Homogeneity test example

from hypex import HomogeneityTest

# `data` has a TreatmentRole column and TargetRole columns, as above
result = HomogeneityTest().execute(data)
result.summary

More about the homogeneity test here

AB-test example

from hypex.dataset import Dataset, InfoRole, TreatmentRole, TargetRole
from hypex import ABTest

data = Dataset(
    roles={
        "user_id": InfoRole(int),  # InfoRole for ID
        "treat": TreatmentRole(),  # TreatmentRole identifies the group (control or target)
        "pre_spends": TargetRole(),  # TargetRole for A/B(n) tests
        "post_spends": TargetRole(),  # TargetRole for A/B(n) tests
    },
    data="data.csv",
)

test = ABTest()  # Classic A/B test
test = ABTest(multitest_method="bonferroni")  # A/Bn test with Bonferroni correction
test = ABTest(additional_tests=["t-test", "u-test", "chi2-test"])  # Choose the tests
test = ABTest(cuped_features={"post_spends": "pre_spends"})  # CUPED variance reduction
test = ABTest(enable_cupac=True, cupac_models=["linear", "ridge"])  # CUPAC variance reduction

result = test.execute(data)
result.summary  # Summary of results
result.multitest  # Multiple testing corrections
result.sizes  # Group sizes
result.variance_reduction_report  # Variance reduction report for CUPED/CUPAC

CUPED and CUPAC

CUPED uses one pre-experiment value of the target. CUPAC predicts the target from pre-experiment covariates, one model per time period transition. Its configuration comes from the roles: PreTargetRole(parent=..., lag=...) marks a historical value of a target, FeatureRole(parent=..., lag=...) a covariate of the same period, and TargetRole(cofounders=[...]) lists the covariates of the target.

from hypex import ABTest
from hypex.dataset import Dataset, FeatureRole, InfoRole, PreTargetRole, TargetRole, TreatmentRole

data = Dataset(
    roles={
        "d": TreatmentRole(),
        "y": TargetRole(cofounders=["X1", "X2"]),
        "y_lag1": PreTargetRole(parent="y", lag=1),  # target one period ago
        "X1_lag1": FeatureRole(parent="X1", lag=1),
        "X2_lag1": FeatureRole(parent="X2", lag=1),
        "y_lag2": PreTargetRole(parent="y", lag=2),  # target two periods ago
        "X1_lag2": FeatureRole(parent="X1", lag=2),
        "X2_lag2": FeatureRole(parent="X2", lag=2),
    },
    data=df,
    default_role=InfoRole(),  # all other columns are ignored
)

result = ABTest(cuped_features={"y": "y_lag1"}).execute(data)  # CUPED
result.variance_reduction_report

# CUPAC: the best model is selected for every transition
result = ABTest(enable_cupac=True, cupac_models=["linear", "ridge", "lasso", "catboost"]).execute(data)
result.cupac.variance_reductions  # variance reduction per model
result.cupac.feature_importances  # which covariates helped the most

"catboost" requires pip install "hypex[cat]". If cupac_models is omitted, all available models are tried.

Full guide: CUPED & CUPAC tutorial.

More about AB test here

Spark backend

Every experiment above runs on Spark with the same API. Create a SparkSession, pass it to Dataset and select the backend explicitly. data can be a pandas.DataFrame, it is converted to a Spark DataFrame.

from pyspark.sql import SparkSession

from hypex import AATest
from hypex.dataset import Dataset, InfoRole, TargetRole
from hypex.utils import BackendsEnum

spark = SparkSession.builder.master("local[*]").appName("HypEx").getOrCreate()

data = Dataset(
    roles={
        "user_id": InfoRole(int),
        "pre_spends": TargetRole(),
        "post_spends": TargetRole(),
    },
    data=pandas_df,
    session=spark,
    backend=BackendsEnum.spark,
)

result = AATest(n_iterations=10, float32=True).execute(data)
result.summary

Notes:

  • Computation is lazy and distributed. Spark pays off on large data; on small data pandas is faster.
  • Dataset.data is a pyspark.sql.DataFrame on this backend, so inspect it with the Spark API.
  • Convert between backends with dataset.to_backend(BackendsEnum.pandas) or dataset.to_backend(BackendsEnum.spark, session=spark). Converting a large Spark dataset to pandas is refused when it would not fit in memory.
  • SmallDataset is pandas-based by design: it holds compact analysis_tables and reporting results.
  • Supported runtime: Python >=3.8, PySpark 3.5.7, Java 8/11/17.

Each tutorial has a "Working with the Spark backend" section.

Documentation

For more detailed information about the library, visit our documentation on ReadTheDocs. It has guides and tutorials to get started and detailed API documentation for advanced use cases.

The architecture of the package (executors, ExperimentData, backends) is described in hypex/README.md.

Additional architecture documentation and source navigation are available in the DeepWiki codebase overview. This AI-generated resource may lag behind the latest release.

Contributions

Join our community! For guidelines on contributing, reporting issues or seeking support, please refer to our Contributing Guidelines.

More Information & Resources

Habr (ru) - discover how HypEx is revolutionizing causal inference in various fields.
A/B testing seminar - Seminar in NoML about matching and A/B testing
Matching with HypEx: Simple Guide - Simple matching guide with explanation
Matching with HypEx: Grouping - Matching with grouping guide
HypEx vs Causal Inference and DoWhy - discover why HypEx is the best solution for causal inference
HypEx vs Causal Inference and DoWhy: part 2 - discover why HypEx is the best solution for causal inference

Testing different libraries for the speed of matching

The benchmark follows the methodology of this Kaggle notebook, adapted to the current Matching API. Within-industry matching searches for neighbors only within the same industry category; otherwise, neighbors are selected from the full dataset with industry retained as a feature. Missing values are filled before estimation. Each cell is a single run, excluding data-generation and dataset-construction time.

Environment: Apple M4 Pro with 24 GB RAM, Python 3.11.14, HypEx 2.0.0 (21577a1), DoWhy 0.11.1, causalinference 0.1.3.post1, and PySpark 3.5.1 in local mode with 8 GB driver memory. Causal Inference runs above 65 536 rows did not complete within a 10-minute timeout or were not run; both cases are marked with —. The fastest result for each group size is shown in bold.

Library Backend Within-industry matching 32 768 65 536 131 072 262 144 524 288 1 048 576 2 097 152 4 194 304
Causal Inference Pandas No 11.22s 48.02s — — — — — —
DoWhy Pandas No 1.70s 3.49s 6.95s 14.24s 29.00s 57.21s 114.55s 243.34s
HypEx Pandas Yes 0.59s 0.73s 1.03s 2.00s 4.05s 8.49s 19.38s 59.92s
HypEx Pandas No 0.39s 0.66s 1.24s 2.72s 6.84s 21.10s 50.18s 145.79s
HypEx Spark Yes 50.87s 45.95s 42.86s 48.87s 63.41s 165.92s 196.46s 250.98s
HypEx Spark No 14.30s 15.98s 15.69s 20.75s 81.91s 92.36s 108.85s 118.82s

Join Our Community

Have questions or want to discuss HypEx? Join our Telegram chat and connect with the community and the developers.

Metadata

Release files for HypEx 2.0.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 HypEx 2.0.0
File Size Uploaded Provenance Actions
hypex-2.0.0.tar.gz 329.2 kB With provenance Details for hypex-2.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 705.4 kB

Release files / hypex-2.0.0.tar.gz

Download URL hypex-2.0.0.tar.gz
Size 329.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4112759fa30372d21c403523baeeb7b9ee71001f22776370b44ba931835a0896
BLAKE2b-256 checksum
How to use checksums
0b670642ff818e1e5668df4ae9e8a3065ba91117cbdbc55dc05a21c51be2c219
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 Nov 23, 2026.

Transparency log

Release files / hypex-2.0.0-py3-none-any.whl

Download URL hypex-2.0.0-py3-none-any.whl
Size 376.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
233f06bd0600d98ca11b9c411fe798b556915c42f92333f6091a4e2f05192077
BLAKE2b-256 checksum
How to use checksums
7f627ae8e2877b747fe03ec333fde2ac575d32c978be0e28d9f3e360a5327ff4
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 Nov 23, 2026.

Transparency log

Security for HypEx 2.0.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 hypex-2.0.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 Nov 23, 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.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files Full provenance

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