Skip to main content

Bayesian A/B testing for proportions

Project description

BayesProp Logo

Bayesian A/B Testing for Proportions

PyPI Downloads Python License: MIT Tests codecov

A Python package for Bayesian hypothesis testing of success-rate differences in any Bernoulli-like experiment, using analytic and approximate inference methods. Input data can be binary (0/1) or real-valued on (0, 1) — continuous scores are automatically binarized at a configurable threshold. Typical applications include comparing treatments, groups, items, model variants, or any two conditions whose outcomes can be expressed as proportions. Please check out our Getting Started guide for installation and quick examples.

Features

  • Effect-size inference for proportions — estimate and test the difference in success rates for both paired and non-paired samples
  • Savage–Dickey Bayes Factor — test a point-null hypothesis ($\delta = 0$) without fitting a separate null model
  • Posterior of the null & ROPE — quantify the posterior mass inside a Region of Practical Equivalence for nuanced decisions beyond simple reject/accept
  • Posterior predictive checks — assess model fit by comparing observed data to data simulated from the posterior
  • Bayes Factor Design Analysis (BFDA) — plan sample sizes to reach a target level of evidence before running the experiment
  • Sequential / streaming design — update the posterior batch-by-batch as data arrive and stop early once the Bayes factor crosses an upper or lower threshold (SequentialNonPairedBayesPropTest, SequentialPairedBayesPropTest)
  • Operating-characteristic analysiscalibrated-Bayes frequentist evaluation of the chosen decision rule: three-way decision rates (reject / accept / inconclusive), Type-I sweep over the baseline rate, 95 % credible-interval coverage, and the sequential stopping-time distribution, with matched-α Fisher's exact (non-paired) or McNemar exact (paired) baselines overlaid. Pre-built Monte-Carlo harness in bayesprop.utils.operation_characteristics and …_paired, plus turnkey notebooks for both designs
  • Publication-ready plots — posterior distributions, predictive checks, Savage–Dickey density-ratio plots, BFDA power curves, sequential BF₁₀ trajectories, and OC diagnostic plots (with Wilson Monte-Carlo bands) out of the box

Models

Model Class Method When to use
Non-paired Beta–Bernoulli NonPairedBayesPropTest Conjugate Beta posteriors per arm; P(B>A) by quadrature, Δ summaries by Monte Carlo Independent groups, exact & fast
Paired Logistic (Laplace) PairedBayesPropTest MAP + Laplace approximation Paired scores, large n, fast iteration
Paired Logistic (Pólya–Gamma) PairedBayesPropTestPG Exact Gibbs sampling Paired scores, small n, exact posterior
Paired Bayesian Bootstrap PairedBayesPropTestBB Nonparametric — Dirichlet weights on the empirical distribution of paired differences Paired scores, no prior elicitation, robustness to likelihood misspecification (ROPE-driven; no Savage–Dickey BF)

Quick start

import numpy as np
from bayesprop.resources.bayes_nonpaired import NonPairedBayesPropTest

# Binary or [0,1]-valued data:
y_A = np.array([1,1,0,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,1])     # 16/20 = 0.80
y_B = np.array([0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,1,0,0,0,0])     #  6/20 = 0.30

# Fit posterior & summarise
model = NonPairedBayesPropTest(seed=42).fit(y_A, y_B)

s = model.summary
print(f"\nMean Δ (θ_A − θ_B) = {s.mean_delta:+.4f}")
print(f"95% CI = [{s.ci_95.lower:.4f}, {s.ci_95.upper:.4f}]")
print(f"P(A > B) = {s.p_A_greater_B:.4f}")

# ── Unified decision ─────────────────────────────────────────────────
d = model.decide()
bf = d.bayes_factor

print("\n--- Unified Decision ---")
print(f"  Bayes Factor: BF₁₀ = {bf.BF_10:.2f}{bf.decision}")
print(f"  Posterior Null: P(H₀|D) = {d.posterior_null.p_H0:.4f}{d.posterior_null.decision}")
print(f"  ROPE: {d.rope.decision} ({d.rope.pct_in_rope:.1%} in ROPE)")

# Plots
model.plot_posteriors()
model.plot_savage_dickey()

Installation

pip install BayesProp

Or with uv:

uv add BayesProp

For development (from source):

git clone https://github.com/AVoss84/bayesProp.git
cd bayesprop
uv venv --python 3.13
uv sync
source .venv/bin/activate

Dependencies

  • Python ≥ 3.13
  • numpy, scipy, matplotlib, pandas
  • pydantic (v2)
  • polyagamma

References

  • Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A. & Rubin, D. B. (2013). Bayesian Data Analysis (3rd ed.). Chapman & Hall/CRC.
  • Kruschke, J. K. (2018). Rejecting or accepting parameter values in Bayesian estimation. Advances in Methods and Practices in Psychological Science, 1(2), 270–280.
  • Polson, N. G., Scott, J. G. & Windle, J. (2013). Bayesian inference for logistic models using Pólya–Gamma latent variables. JASA, 108(504), 1339–1349.
  • Rubin, D. B. (1981). The Bayesian Bootstrap. The Annals of Statistics, 9(1), 130–134.
  • Schönbrodt, F. D. & Wagenmakers, E.-J. (2018). Bayes factor design analysis: Planning for compelling evidence. Psychonomic Bulletin & Review, 25(1), 128–142.

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

bayesprop-0.1.0.8.tar.gz (88.5 kB view details)

Uploaded Source

Built Distribution

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

bayesprop-0.1.0.8-py3-none-any.whl (76.4 kB view details)

Uploaded Python 3

File details

Details for the file bayesprop-0.1.0.8.tar.gz.

File metadata

  • Download URL: bayesprop-0.1.0.8.tar.gz
  • Upload date:
  • Size: 88.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bayesprop-0.1.0.8.tar.gz
Algorithm Hash digest
SHA256 7578ed68ccbb300f3a8c8b361ce187102a68f5449835ff93a667afe1330d5837
MD5 60343d5387fa9f5009c3138274cd42c9
BLAKE2b-256 24a511303e211ff0e5e76a555f88082c4ba72e81243eb5fc6c74c664e90b7708

See more details on using hashes here.

Provenance

The following attestation bundles were made for bayesprop-0.1.0.8.tar.gz:

Publisher: publish_pypi.yml on AVoss84/bayesProp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bayesprop-0.1.0.8-py3-none-any.whl.

File metadata

  • Download URL: bayesprop-0.1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 76.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bayesprop-0.1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 145b812031244bddff72beff1be9c1168dff8c34ee5dd6c2f80310ac8d53f42c
MD5 f9a040b103d39fa3c9bebdf21920695e
BLAKE2b-256 6e46c57d541f61a93fd2834288fe2b328202407b6abd19c99a5b85052ae86eca

See more details on using hashes here.

Provenance

The following attestation bundles were made for bayesprop-0.1.0.8-py3-none-any.whl:

Publisher: publish_pypi.yml on AVoss84/bayesProp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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