Skip to main content

Statistical analysis tools for A/B experiment metric evaluation

Project description

ab-metric-eval

Statistical analysis tools for A/B experiment metric evaluation.

ab-metric-eval provides two general-purpose functions for analysing experiment results from any pandas DataFrame — no Spark or platform-specific dependencies required.

Features

  • Experiment summaries — group-level enrollment counts, crossover and exclusion rates, and a daily enrollment timeline chart
  • Hypothesis testing — Welch's t-test (continuous) and proportions z-test (binary), with configurable significance level
  • CUPED variance reduction — optional pre-exposure covariate adjustment to increase statistical power
  • Winsorization — symmetric or asymmetric outlier clipping for continuous metrics
  • Pre-exposure bias testing — diagnostic check for randomisation imbalance before drawing conclusions
  • Flexible imputation — zero-fill or drop-missing strategies for pre/post pivoting

Installation

pip install ab-metric-eval

Quick start

import pandas as pd
from ab_metric_eval import generate_experiment_summary, analyze_experiment_metric

# df is a pandas DataFrame with one or two rows per unit
# (pre-exposure and post-exposure), containing at minimum:
#   experiment_group, analysis_unit_id, analysis_exclusion_flag,
#   crossover_flag, first_exposure_utc_date

# 1. Summarise enrollment and group balance
summary_df, enrollment_df, fig = generate_experiment_summary(df)

# 2. Run a hypothesis test on a continuous metric
summary, results, fig, winsor_summary = analyze_experiment_metric(
    df=df,
    metric_column="revenue",
    pre_exposure_eval_column="pre_revenue",   # optional, enables CUPED + bias test
    metric_type="continuous",
    winsorize_percentile=0.99,
    apply_cuped=True,
    alpha=0.05,
)

API reference

generate_experiment_summary(df)

Generates group-level summary statistics and a daily enrollment timeline.

Parameters

Name Type Description
df pd.DataFrame Per-unit experiment data. Required columns: experiment_group, name, analysis_unit_id, analysis_exclusion_flag, crossover_flag, first_exposure_utc_date. Optional: metric_time_window.

Returns (summary_df, enrollment_df, fig) — group summary, daily enrollment counts, matplotlib figure.


analyze_experiment_metric(df, metric_column, ...)

Runs a full statistical analysis on a single metric, including optional pre-exposure bias testing and CUPED adjustment.

Parameters

Name Type Default Description
df pd.DataFrame Per-unit data (pre- and/or post-exposure rows).
metric_column str Post-exposure metric column to test.
pre_exposure_eval_column str | None None Pre-exposure metric column. Required for bias test and CUPED.
metric_type str 'continuous' 'continuous' or 'binary'.
winsorize_percentile float | tuple | None None Winsorization bounds. A single float (e.g. 0.99) clips symmetrically at the (1%, 99%) percentiles. A tuple (lower, upper) allows asymmetric clipping; use None for either end to skip that tail.
alpha float 0.05 Significance level for the hypothesis test.
apply_cuped bool False Apply CUPED adjustment (requires pre_exposure_eval_column; not supported for binary metrics).
imputation_strategy str 'zero' How to handle units missing pre- or post-exposure rows after pivoting: 'zero' imputes with 0, 'drop' removes the unit.
include_exclusion_flag bool False If False, units where analysis_exclusion_flag is True are removed.
include_crossover_flag bool False If False, units where crossover_flag is True are removed.
bias_test_alpha float | None None Significance threshold for the pre-exposure balance diagnostic. When None, a graduated reporting scheme is used instead of a binary pass/fail.

Returns (summary_df, test_results_df, fig, winsorization_summary_df).


Data classes

ExperimentConfig and MetricDefinition are convenience dataclasses for grouping experiment parameters and metric definitions respectively.

from ab_metric_eval import ExperimentConfig, MetricDefinition

config = ExperimentConfig(
    expt_name="my-experiment",
    enrollment_date_start="2025-01-01",
    enrollment_date_end_incl="2025-01-31",
    analysis_date_start="2025-02-01",
    analysis_date_end_incl="2025-02-28",
    unit_type="userId",
)

metric = MetricDefinition(
    name="revenue",
    sql_expression="SUM(revenue)",
    description="Total revenue per user",
)

Source

github.com/five-blue-tiles/general-repo/tree/main/ab-metric-eval

Requirements

  • Python 3.8+
  • numpy >= 1.19
  • pandas >= 1.0
  • scipy >= 1.5
  • statsmodels >= 0.12
  • matplotlib >= 3.3
  • seaborn >= 0.11

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ab_metric_eval-0.1.1.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ab_metric_eval-0.1.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file ab_metric_eval-0.1.1.tar.gz.

File metadata

  • Download URL: ab_metric_eval-0.1.1.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for ab_metric_eval-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8ecab59a26ef314f104f9d7a3dc89eb01e102c5234ebe4f99e8cac0b0c540e35
MD5 5b16b62f7636d575422926ee922f0587
BLAKE2b-256 4964d5758f709dfd88524ebf407c2ead77d2f14b33cbd0222d69bf4188640abb

See more details on using hashes here.

File details

Details for the file ab_metric_eval-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ab_metric_eval-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for ab_metric_eval-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac23563ae48f2dc53e17271cdbe3d17112c4f6e3141c7eb299ee3b6061e44ca5
MD5 46e6e6370524d9f3934cb6f31d72c440
BLAKE2b-256 77e275fd5962a0835944e225939616bac07006438c563d3310733060c1e64b4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page