Skip to content

Instantly share code, notes, and snippets.

@cjmcgraw
Last active June 23, 2021 18:46
Show Gist options
  • Save cjmcgraw/2f33b7910987bfb13982a78cdd5b6e8e to your computer and use it in GitHub Desktop.
Save cjmcgraw/2f33b7910987bfb13982a78cdd5b6e8e to your computer and use it in GitHub Desktop.
A script containing functions to calculate minimum required sample size for experiments:
#! /usr/bin/env python
import math
import statistics
def binomial_required_sample_size(p1, p2, min_diff=5e-4, include_continuity_correct=True, two_sided=True):
"""derived from: https://www.itl.nist.gov/div898/handbook/prc/section2/prc242.htm"""
z_alpha = 1.95
if two_sided:
z_alpha = 2.24
z_beta = 0.89
p1_std = math.sqrt(p1 * (1.0 - p1))
p2_std = math.sqrt(p2 * (1.0 - p2))
n = pow((z_alpha * p1_std + z_beta * p2_std) / (min_diff), 2)
if include_continuity_correct:
n += 1/min_diff
return n
@cjmcgraw
Copy link
Author

As of current revision this is calculated with an assumed minimum difference of 1/2 of 1%.

Using itl.nist documentation for its calculation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment