| from statsmodels.stats.proportion import proportion_confint |
| |
| print('预估率可信区间的Python程序\nby Lokyshin\n') |
| |
| |
| n = 100 |
| |
| |
| positive = 30 |
| |
| |
| p = positive / n |
| |
| |
| alpha = 0.05 |
| |
| |
| np_value = n * p |
| n1_p_value = n * (1 - p) |
| |
| |
| if np_value > 5 and n1_p_value > 5: |
| |
| method = "Normal Approximation Method" |
| |
| ci_low, ci_up = proportion_confint(positive, n, alpha=alpha, method='normal') |
| ci_low, ci_up = round(ci_low, 3), round(ci_up, 3) |
| else: |
| |
| method = "Clopper-Pearson Method" |
| |
| ci_low, ci_up = proportion_confint(positive, n, alpha=alpha, method='beta') |
| ci_low, ci_up = round(ci_low, 3), round(ci_up, 3) |
| |
| print(f"样本量:{n}") |
| print(f"阳性数量:{positive}") |
| print(f"预期成功次数n*p:{round(np_value,3)}") |
| print(f"预期失败次数n*(1-p):{round(n1_p_value,3)}\n") |
| print(f"使用的方法是:{method}") |
| print('95% Confidence interval: [{}, {}]'.format(ci_low, ci_up)) |
| |
| print('\n感谢您使用本程序进行统计分析,再见。\n') |