python - Python中两个比例之间差异的置信区间

标签 python statistics confidence-interval ab-testing

例如,在 AB 测试中,A 总体可能有 1000 个数据点,其中 100 个是成功的。而 B 可能有 2000 个数据点和 220 次成功。这使 A 的成功比例为 0.1,B 的成功比例为 0.11,其增量为 0.01。我如何在 Python 中计算此增量周围的置信区间?

Stats 模型可以针对一个样本执行此操作,但似乎没有一个包来处理 AB 测试所必需的两个样本之间的差异。 ( http://www.statsmodels.org/dev/generated/statsmodels.stats.proportion.proportion_confint.html )

最佳答案

我无法从 Statsmodels 中找到与此相关的函数。然而,this website复习数学以生成置信区间以及作为以下函数的来源:

def two_proprotions_confint(success_a, size_a, success_b, size_b, significance = 0.05):
    """
    A/B test for two proportions;
    given a success a trial size of group A and B compute
    its confidence interval;
    resulting confidence interval matches R's prop.test function

    Parameters
    ----------
    success_a, success_b : int
        Number of successes in each group

    size_a, size_b : int
        Size, or number of observations in each group

    significance : float, default 0.05
        Often denoted as alpha. Governs the chance of a false positive.
        A significance level of 0.05 means that there is a 5% chance of
        a false positive. In other words, our confidence level is
        1 - 0.05 = 0.95

    Returns
    -------
    prop_diff : float
        Difference between the two proportion

    confint : 1d ndarray
        Confidence interval of the two proportion test
    """
    prop_a = success_a / size_a
    prop_b = success_b / size_b
    var = prop_a * (1 - prop_a) / size_a + prop_b * (1 - prop_b) / size_b
    se = np.sqrt(var)

    # z critical value
    confidence = 1 - significance
    z = stats.norm(loc = 0, scale = 1).ppf(confidence + significance / 2)

    # standard formula for the confidence interval
    # point-estimtate +- z * standard-error
    prop_diff = prop_b - prop_a
    confint = prop_diff + np.array([-1, 1]) * z * se
    return prop_diff, confint

关于python - Python中两个比例之间差异的置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47570903/

相关文章:

用于发现流量数据中的异常 ("spikes") 的算法

r - 支持向量机的训练和测试

r - vcovHC 和置信区间

r - 确定 R 中分布的高密度区域

python - 工作 python xml

python - Django 查询集和 GROUP BY

不同操作系统的 Python open() unicode 文件名行为不同

R:通过两列应用 Pearson 卡方检验

r - 添加置信区间以从 R 中的模拟数据绘制

python - 使用 Python 中的 POST 请求通过网站表单上传基本文件