From fd69eae1f86c35e155e7a42e49dbcf5f13c54571 Mon Sep 17 00:00:00 2001 From: wizardforcel <562826179@qq.com> Date: Wed, 24 Jan 2018 11:24:45 +0800 Subject: [PATCH] ch16. --- 16.md | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/16.md b/16.md index 26cd029..dd08a63 100644 --- a/16.md +++ b/16.md @@ -401,3 +401,136 @@ Empirical P-value: 0.0 我们使用排列检验来帮助我们确定,类别属性的分布是否与类别相关。 一般来说,排列检验可以这样使用来确定,两个类别分布是否从相同的基本分布随机抽样。 +## A/B 测试 + +我们使用随机排列来查看,两个样本是否从相同的基本分类分布抽取。 如果样本是数值的,则可以使用相同的方法;检验统计量的选择通常比较简单。 在我们使用`Deflategate`数据的例子中,我们使用了不同的方法来测试爱国者队和小马队用球是否来自相同的基本分布。 + +在现代数据分析中,决定两个数值样本是否来自相同的基本分布称为 A/B 测试。 名称是指两个样本 A 和 B 的标签。 + +### 吸烟者和不吸烟者 + +我们对随机抽样的母亲及其新生儿进行了许多不同的分析,但是我们还没有查看母亲是否吸烟的数据。 研究的目的之一,是看母亲吸烟是否与出生体重有关。 + +```py +baby = Table.read_table('baby.csv') +baby +``` + + +| Birth Weight | Gestational Days | Maternal Age | Maternal Height | Maternal Pregnancy Weight | Maternal Smoker | +| --- | --- | --- | --- | --- | --- | +| 120 | 284 | 27 | 62 | 100 | False | +| 113 | 282 | 33 | 64 | 135 | False | +| 128 | 279 | 28 | 64 | 115 | True | +| 108 | 282 | 23 | 67 | 125 | True | +| 136 | 286 | 25 | 62 | 93 | False | +| 138 | 244 | 33 | 62 | 178 | False | +| 132 | 245 | 23 | 65 | 140 | False | +| 120 | 289 | 25 | 62 | 125 | False | +| 143 | 299 | 30 | 66 | 136 | True | +| 140 | 351 | 27 | 68 | 120 | False | + +(省略了 1164 行) + +我们首先选择`Birth Weight`和`Maternal Smoker`。 样本中有 715 名非吸烟者,459 名吸烟者。 + +```py +weight_smoke = baby.select('Birth Weight', 'Maternal Smoker') +weight_smoke.group('Maternal Smoker') +``` + + +| Maternal Smoker | count | +| --- | --- | +| False | 715 | +| True | 459 | + +下面的第一个直方图显示了样本中非吸烟者的婴儿出生体重的分布。 第二个显示了吸烟者的婴儿出生体重。 + +```py +nonsmokers = baby.where('Maternal Smoker', are.equal_to(False)) +``` + +```py +nonsmokers.hist('Birth Weight', bins=np.arange(40, 181, 5), unit='ounce') +``` + +两种分布都大致是钟形,中心在 120 盎司附近。 当然,这些分布并不相同,这就产生了这样一个问题,即差异是否仅仅反映了机会变异,还是反映了总体分布的差异。 + +这个问题可以通过假设检验来回答。 + +原假设:在总体中,不吸烟的母亲的婴儿出生体重的分布和吸烟的母亲相同。 样本中的差异是偶然的。 + +备选假设:两种分布在总体中是不同的。 + +检验统计量:出生体重是一个定量变量,所以用均值的绝对差作为检验统计量是合理的。 + +检验统计量的观测值约为 9.27 盎司。 + +```py +means_table = weight_smoke.group('Maternal Smoker', np.mean) +means_table +``` + +| Maternal Smoker | Birth Weight mean | +| --- | --- | +| False | 123.085 | +| True | 113.819 | + +```py +nonsmokers_mean = means_table.column(1).item(0) +smokers_mean = means_table.column(1).item(1) +nonsmokers_mean - smokers_mean +9.266142572024918 +``` + +## 排列检验 + +为了看看原假设下是否有可能出现这种差异,我们将使用排列检验,就像我们在前一节中所做的那样。 我们必须为检验统计量改变代码。 为此,我们将像上面那样计算平均值的差,然后取绝对值。 + +请记住,在原假设下,出生体重的所有排列与`Maternal Smoker `列等可能出现。 所以,就像以前一样,每次重复都是打乱正在比较的变量。 + +```py +def permutation_test_means(table, variable, classes, repetitions): + + """Test whether two numerical samples + come from the same underlying distribution, + using the absolute difference between the means. + table: name of table containing the sample + variable: label of column containing the numerical variable + classes: label of column containing names of the two samples + repetitions: number of random permutations""" + + t = table.select(variable, classes) + + # Find the observed test statistic + means_table = t.group(classes, np.mean) + obs_stat = abs(means_table.column(1).item(0) - means_table.column(1).item(1)) + + # Assuming the null is true, randomly permute the variable + # and collect all the generated test statistics + stats = make_array() + for i in np.arange(repetitions): + shuffled_var = t.select(variable).sample(with_replacement=False).column(0) + shuffled = t.select(classes).with_column('Shuffled Variable', shuffled_var) + m_tbl = shuffled.group(classes, np.mean) + new_stat = abs(m_tbl.column(1).item(0) - m_tbl.column(1).item(1)) + stats = np.append(stats, new_stat) + + # Find the empirical P-value: + emp_p = np.count_nonzero(stats >= obs_stat)/repetitions + + # Draw the empirical histogram of the tvd's generated under the null, + # and compare with the value observed in the original sample + Table().with_column('Test Statistic', stats).hist(bins=20) + plots.title('Empirical Distribution Under the Null') + print('Observed statistic:', obs_stat) + print('Empirical P-value:', emp_p) +permutation_test_means(baby, 'Birth Weight', 'Maternal Smoker', 5000) +Observed statistic: 9.266142572024918 +Empirical P-value: 0.0 +``` + +原始样本中的观测差异约为 9.27 盎司,与此分布不一致:经验性 P 值为 0,这意味着确切的 P 值确实非常小。 因此,测试的结论是,在总体中,不吸烟者和吸烟者的婴儿出生体重的分布是不同的。 + + -- GitLab