Skip to main content

A comprehensive Python library for A/B testing analysis

Project description

AB Testing Library

A comprehensive Python library designed for A/B testing analysis, providing essential statistical tools for hypothesis testing, confidence interval calculations, p-value visualizations, and multiple hypothesis correction methods. Whether you're running simple two-group comparisons or more complex multi-group analyses, this library equips you with the necessary functions to derive meaningful insights from your experiments.

Table of Contents

Features

  • Minimum Detectable Effect (MDE) Calculations: Determine the smallest effect size you can detect with your experimental setup.
  • Ratio-Specific MDE: Calculate MDE for metrics expressed as ratios.
  • Statistical Testing: Perform t-tests and proportion tests between groups to evaluate significance.
  • P-value Visualization: Visualize p-value dynamics over time and their distributions.
  • Multiple Hypothesis Correction: Apply the Benjamini-Hochberg procedure to control the false discovery rate.

Essential Mathematical Formulas


Sample Size

$$ n = \frac{(Z_{1-\alpha/2} + Z_{1-\beta})^2 \cdot (Var_{\text{test}}+Var_{\text{control}})}{\delta^2} $$


MDE

$$ \text{MDE} = \frac{(Z_{1-\alpha/2} + Z_{1-\beta}) \cdot \sqrt{(Var_{\text{test}}+Var_{\text{control}})}}{\sqrt{n}} $$


T-test (Welch's)

$$ t = \frac{\bar{X}_1 - \bar{X}_2}{\sqrt{\frac{S_1^2}{n_1} + \frac{S_2^2}{n_2}}} $$


Z-test (Wald's)

$$ z = \frac{\hat{p}_1 - \hat{p}_2}{\sqrt{\hat{p}(1 - \hat{p}) \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}} $$


Delta method

$$ \text{Var}\left(\frac{X}{Y}\right) \approx \frac{\text{Var}(X)}{\bar{Y}^2} + \frac{\bar{X}^2 \cdot \text{Var}(Y)}{\bar{Y}^4} - 2 \cdot \frac{\bar{X} \cdot \text{Cov}(X, Y)}{\bar{Y}^3} $$


Benjamini-Hochberg

$$ p_k \leq \frac{k}{m} \cdot Q $$

Installation

You can install the library using pip:

pip install expab

Or, if you have the source code cloned locally, install it in editable mode:

pip install -e .

Deleting the package with terminal to re-install

pip uninstall expab

When new version is released, it needs to be updated with the following command

pip install expab --upgrade

Usage

Generated data for testing library

import random
import numpy as np
import pandas as pd

# Data for using mde

sample_size = 10000
dataset = [random.randrange(1000, sample_size) for i in range(sample_size)]
dataset_binary = [random.randrange(0, 2) for i in range(sample_size)]
mean = np.mean(dataset)
std = np.std(dataset)

# Dataframe for applying tests

sample_size = 10000

df = pd.DataFrame()
df['user_id'] = pd.Series([i for i in range(sample_size)])
df['has_treatment'] = pd.Series(np.random.randint(0, 2, sample_size))
df['value'] = pd.Series([random.randrange(1000, sample_size) for i in range(sample_size)])
df['order_number'] = pd.Series([random.randrange(0, 10) for i in range(sample_size)])
df['binary'] = pd.Series([random.randrange(0, 2) for i in range(sample_size)])

Calculate Minimum Detectable Effect (MDE)

Calculate the Minimum Detectable Effect (MDE) given the mean, standard deviation, and sample size.

from expab import get_mde

mean = 100
std = 15
sample_size = 1000

mde_percentage, mde_absolute = get_mde(mean, std, [sample_size])
print(f"MDE: {mde_percentage}% ({mde_absolute})")

Calculate MDE for Ratios

Calculate MDE when your metric is a ratio (e.g., conversion rate).

from expab import get_mde_ratio
import numpy as np

numerator = np.array([50050, 55051, 60502, 65506, 70304])
denominator = np.array([500, 550, 600, 650, 700])
sample_size = 1000

mde_ratio_percentage, mde_ratio_absolute = get_mde_ratio(numerator, denominator, [sample_size])
print(f"MDE Ratio: {mde_ratio_percentage}% ({mde_ratio_absolute})")

Plot P-value Over Time

Visualize how p-values change over different time periods during your experiment.

from expab import plot_p_value_over_time

dates = ['2024-01', '2024-02', '2024-03', '2024-04']
test_group = [[1.2, 1.3, 1.1], [1.4, 1.5, 1.3], [1.5, 1.6, 1.4], [1.7, 1.8, 1.6]]
control_group = [[1.1, 1.0, 1.2], [1.2, 1.1, 1.3], [1.3, 1.2, 1.4], [1.4, 1.3, 1.5]]

plot_p_value_over_time(dates, test_group, control_group)

Perform T-Tests Between Groups

Conduct t-tests between two groups and obtain statistical metrics.

from expab import ttest
import pandas as pd
import numpy as np

# Sample DataFrame
data = {
    'group': [0]*100 + [1]*100,
    'metric': np.random.normal(100, 15, 200)
}
df = pd.DataFrame(data)

results = ttest(df, metric_col='metric', ab_group_col='group')
results

Perform Proportion Tests Between Groups

Perform proportion tests to compare binary outcomes between groups.

from expab import ztest_proportion
import pandas as pd
import numpy as np

# Sample DataFrame
data = {
    'group': [0]*1000 + [1]*1000,
    'success': np.random.binomial(1, 0.1, 2000)
}
df = pd.DataFrame(data)

results = ztest_proportion(df, metric_col='success', ab_group_col='group')
results

Perform T-Tests on Delta Between Ratios

Compare the delta between two ratio metrics across groups.

from expab import ttest_delta
import pandas as pd
import numpy as np

# Sample DataFrame
data = {
    'group': [0]*1000 + [1]*1000,
    'numerator': np.random.binomial(1, 0.1, 2000),
    'denominator': np.random.binomial(10, 0.5, 2000)
}
df = pd.DataFrame(data)

results = ttest_delta(
    df, 
    metric_num_col='numerator', 
    metric_denom_col='denominator', 
    ab_group_col='group'
)
results

Plot P-value Distribution from A/A Tests

Visualize the distribution of p-values from A/A testing to assess test calibration.

from expab import plot_p_value_distribution
import numpy as np
import pandas as pd

control_group = np.random.normal(100, 15, 1000)
test_group = np.random.normal(100, 15, 1000)

plot_p_value_distribution(control_group, test_group)

Plot P-value ECDF

Create Empirical Cumulative Distribution Function (ECDF) plots for p-values.

from expab import plot_pvalue_ecdf
import pandas as pd
import numpy as np

# Sample DataFrame
data = {
    'has_treatment': [1]*1000 + [0]*1000,
    'gmv': np.random.normal(100, 15, 2000)
}
control_group = pd.DataFrame(data)
test_group = pd.DataFrame(data)

plot_pvalue_ecdf(control_group, test_group, title='P-value ECDF')

Apply Benjamini-Hochberg Procedure

Control the false discovery rate when performing multiple hypothesis tests.

from expab import method_benjamini_hochberg
import numpy as np
import pandas as pd

pvalues = np.random.uniform(0, 1, 100)
adjusted = method_benjamini_hochberg(pvalues, alpha=0.05)
print(adjusted)

License

This project is licensed under the MIT License.

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

expab-0.0.6.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

expab-0.0.6-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file expab-0.0.6.tar.gz.

File metadata

  • Download URL: expab-0.0.6.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for expab-0.0.6.tar.gz
Algorithm Hash digest
SHA256 6a2596b00bc97cd1dd49ba96c06f1e5944e002a980e50d3a969e11458b13b40a
MD5 733dad6f9f7630e90463ff4916be3081
BLAKE2b-256 c91738c4f5e16da172fbb2b1bef698ffb5a15ad16f938c754b3851c426598efe

See more details on using hashes here.

Provenance

The following attestation bundles were made for expab-0.0.6.tar.gz:

Publisher: publish.yml on Renarion/expab

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

File details

Details for the file expab-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: expab-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for expab-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1e7cafc66589092219575382abe0217d9b756221f7f8306e35a452bc7c5cd781
MD5 d8df262c7cecf309806b8ac7d916b2fb
BLAKE2b-256 66c18af3c90890bf4799831110e93011db9850ba7432e6c4b58b4c061fb9d8a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for expab-0.0.6-py3-none-any.whl:

Publisher: publish.yml on Renarion/expab

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