Created
October 14, 2022 17:43
-
-
Save galenseilis/5175009c8c40550f04ae3587041cbf50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from scipy import stats | |
| def wilson_cont(n1, n2, alpha=0.05): | |
| ''' | |
| Wilson score interval with continuity correction. | |
| Two-tail interval is assumed. | |
| Parameters: | |
| n1 (int): Count of outcome 1. | |
| n2 (int): Count of outcome 2. | |
| alpha (float): | |
| Source: | |
| https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval | |
| ''' | |
| assert type(n1) == int and type(n2) == int | |
| assert 0 < alpha < 1 | |
| z = stats.norm.ppf(1 - alpha / 2) | |
| n = n1 + n2 | |
| phat = n1 / n | |
| num1 = 2 * n * phat + z**2 | |
| num2 = z * np.sqrt(z**2 - 1 / n + 4 * n * phat * (1 - phat) + 4 * phat - 2) + 1 | |
| num3 = z * np.sqrt(z**2 + 1 / n + 4 * n * phat * (1 - phat) - 4 * phat - 2) + 1 | |
| denom = 2 * (n + z **2) | |
| return max(0, (num1 - num2) / denom), min(1, (num1 + num3) / denom) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment