A Python package for A/B testing statistical analysis
Project description
owl_ab_test
A Python package for A/B testing statistical analysis. This package provides tools for analyzing the results of A/B tests with support for both proportion-based and revenue-based metrics.
Installation
pip install owl_ab_test
Features
- Calculate statistics for A/B tests including:
- Proportion-based metrics (e.g., conversion rates)
- Revenue-based metrics (e.g., revenue per user)
- Process multiple metrics simultaneously
- Generate visualizations of confidence intervals using Plotly
- Handle control and multiple treatment groups
- Built-in error handling for invalid inputs
Usage
Basic Example - Proportion Metrics
from owl_ab_test import calculate_proportion_stats, process_stats, plot_confidence_intervals
import pandas as pd
# Calculate statistics for a single proportion metric
stats = calculate_proportion_stats(
success_count=150, # Number of successes in treatment group
total_count=1000, # Total sample size in treatment group
control_success=120, # Number of successes in control group
control_total=1000, # Total sample size in control group
confidence_level=0.95 # Optional confidence level (default: 0.95)
)
print(f"Lift: {stats['lift']:.2%}")
print(f"P-value: {stats['p_value']:.4f}")
print(f"95% CI: ({stats['ci_lower']:.2%}, {stats['ci_upper']:.2%})")
Basic Example - Revenue Metrics
from owl_ab_test import calculate_revenue_stats
# Calculate statistics for a revenue metric
stats = calculate_revenue_stats(
treatment_value=25.50, # Mean revenue in treatment group
treatment_std=15.20, # Standard deviation in treatment group
treatment_n=1000, # Sample size in treatment group
control_value=20.00, # Mean revenue in control group
control_std=14.80, # Standard deviation in control group
control_n=1000, # Sample size in control group
confidence_level=0.95 # Optional confidence level (default: 0.95)
)
print(f"Lift: {stats['lift']:.2%}")
print(f"P-value: {stats['p_value']:.4f}")
print(f"95% CI: ({stats['ci_lower']:.2%}, {stats['ci_upper']:.2%})")
Processing Multiple Metrics
# Example DataFrame structure
data = {
'variant': ['control', 'treatment_a', 'treatment_b'],
'bucketed_visitors': [1000, 1000, 1000],
'trial_starts': [120, 150, 140],
'purchases': [60, 75, 70],
'revenue': [1200.0, 1500.0, 1400.0],
'revenue_std': [800.0, 850.0, 820.0],
'user_count': [1000, 1000, 1000]
}
df = pd.DataFrame(data)
# Configure metrics to analyze
metrics_config = {
'trial_conversion': {
'type': 'proportion',
'success_col': 'trial_starts',
'total_col': 'bucketed_visitors'
},
'purchase_conversion': {
'type': 'proportion',
'success_col': 'purchases',
'total_col': 'bucketed_visitors'
},
'revenue_per_user': {
'type': 'revenue',
'value_col': 'revenue',
'std_col': 'revenue_std',
'n_col': 'user_count'
}
}
# Process all metrics
results = process_stats(df, metrics_config)
Visualizing Results
# Create a confidence interval plot
metric_mapping = {
'trial_conversion': 'Trial Conversion Rate',
'purchase_conversion': 'Purchase Conversion Rate',
'revenue_per_user': 'Revenue per User'
}
fig = plot_confidence_intervals(
results,
metric_mapping=metric_mapping,
width=900,
height=400
)
fig.show()
API Reference
calculate_proportion_stats
calculate_proportion_stats(success_count, total_count, control_success, control_total, confidence_level=0.95)
Calculates statistical metrics for proportion-based A/B tests.
Parameters:
success_count(int): Number of successes in treatment grouptotal_count(int): Total sample size in treatment groupcontrol_success(int): Number of successes in control groupcontrol_total(int): Total sample size in control groupconfidence_level(float, optional): Confidence level for intervals (default: 0.95)
Returns:
- dict with keys:
lift: Relative improvement over controlstatistic: Z-test statisticp_value: Two-sided p-valueci_lower: Lower bound of confidence interval for relative liftci_upper: Upper bound of confidence interval for relative lift
calculate_revenue_stats
calculate_revenue_stats(treatment_value, treatment_std, treatment_n,
control_value, control_std, control_n,
confidence_level=0.95)
Calculates statistical metrics for revenue-based A/B tests.
Parameters:
treatment_value(float): Mean value in treatment grouptreatment_std(float): Standard deviation in treatment grouptreatment_n(int): Sample size in treatment groupcontrol_value(float): Mean value in control groupcontrol_std(float): Standard deviation in control groupcontrol_n(int): Sample size in control groupconfidence_level(float, optional): Confidence level for intervals (default: 0.95)
Returns:
- dict with keys:
lift: Relative improvement over controlstatistic: T-test statisticp_value: Two-sided p-valueci_lower: Lower bound of confidence interval for relative liftci_upper: Upper bound of confidence interval for relative lift
process_stats
process_stats(df, metrics_config)
Processes multiple metrics for A/B test analysis, supporting both proportion and revenue metrics.
Parameters:
df(pandas.DataFrame): DataFrame containing experiment data- Required columns: 'variant' plus columns specified in metrics_config
metrics_config(dict): Configuration for metrics to analyze- Keys: metric names
- Values: dict with the following structure:
For proportion metrics:
{ 'type': 'proportion', 'success_col': 'column_name', 'total_col': 'column_name' }
For revenue metrics:{ 'type': 'revenue', 'value_col': 'column_name', 'std_col': 'column_name', 'n_col': 'column_name' }
Returns:
- pandas.DataFrame with columns:
Metric: Name of the metricGroup: Variant nameValue: Raw proportion or valueLift: Relative lift vs controlStatistic: Test statistic (Z-test for proportions, T-test for revenue)P-Value: Two-sided p-valueCI_Lower: Lower confidence intervalCI_Upper: Upper confidence interval
plot_confidence_intervals
plot_confidence_intervals(results, metric_mapping=None, width=900, height=400)
Creates a visualization of confidence intervals for experiment results.
Parameters:
results(pandas.DataFrame): Results DataFrame from process_proportion_statsmetric_mapping(dict, optional): Maps metric names to display nameswidth(int, optional): Plot width in pixels (default: 900)height(int, optional): Plot height in pixels (default: 400)
Returns:
- plotly.graph_objects.Figure: Interactive confidence interval plot
Requirements
- Python >=3.7
- numpy
- pandas
- scipy
- plotly
Notes
- The package uses z-test statistics for proportion metrics and t-test statistics for revenue metrics
- Confidence intervals are calculated for relative lift over control
- The visualization uses Plotly for interactive plots
- Treatment groups are compared against the control group specified by 'variant' == 'control'
License
MIT License
Contributing
Contributions are welcome! Please reach out to anika.ranginani@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file owl_ab_test-0.1.9.tar.gz.
File metadata
- Download URL: owl_ab_test-0.1.9.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cf0b9d7ff31b1809b91349d26716cfc7f7d37b9a4aeb860a8011bdd62328035
|
|
| MD5 |
8063a76971cb6b12a7aebceacb01fa88
|
|
| BLAKE2b-256 |
247a7dc9105f0b1d2bf8eb775ad9b6037665209d2fe887e6e8d52b18335b2366
|
File details
Details for the file owl_ab_test-0.1.9-py3-none-any.whl.
File metadata
- Download URL: owl_ab_test-0.1.9-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6686f9f5f922e359d9ad1938f878447c02012029d08b536ed9e8bdd92125f9f
|
|
| MD5 |
e2a5d599984dd9f14d40a91fb1944c49
|
|
| BLAKE2b-256 |
60b16ac76ab884c8272cb3b33bbb530432149b277c67203d66756fbbde9ef4f3
|