假设检验与 z 分数

Python 假设检验

James Chapman

Curriculum Manager, DataCamp

A/B 测试

  • 2013 年,Electronic Arts(EA)发布《模拟城市 5》
  • 他们想提高游戏的预购量
  • 采用 A/B 测试评估不同广告方案
  • 将用户分为对照组和处理组

Electronic Arts 大楼

1 图片来源:"Electronic Arts" by majaX1 CC BY-NC-SA 2.0
Python 假设检验

零售网页 A/B 测试

对照:

SimCity 网页,横幅写着"预购并在下次购买时减 $20"

处理:

无横幅的 SimCity 网页

Python 假设检验

A/B 测试结果

  • 处理组(无广告)的购买量比对照组(有广告)高 43.4%
  • "展示广告会提升销量"的直觉是错的
  • 该结果是统计显著还是偶然?
  • 需用 EA 的数据来判定
  • 结合《Sampling in Python》与本课程的方法来完成
Python 假设检验

Stack Overflow 开发者调查 2020

import pandas as pd
print(stack_overflow)
      respondent  age_1st_code  ...   age  hobbyist
0           36.0          30.0  ...  34.0       Yes
1           47.0          10.0  ...  53.0       Yes
2           69.0          12.0  ...  25.0       Yes
3          125.0          30.0  ...  41.0       Yes
4          147.0          15.0  ...  28.0        No
...          ...           ...  ...   ...       ...
2259     62867.0          13.0  ...  33.0       Yes
2260     62882.0          13.0  ...  28.0       Yes

[2261 rows x 8 columns]
Python 假设检验

关于均值的假设

一个假设:

数据科学家总体的年薪均值为 $110,000

点估计(样本统计量):

mean_comp_samp = stack_overflow['converted_comp'].mean()
119574.71738168952
Python 假设检验

生成自助法分布(bootstrap)

import numpy as np

# 步骤 3:重复步骤 1 和 2 多次,并追加到列表 so_boot_distn = [] for i in range(5000): so_boot_distn.append(
# 步骤 2:计算点估计 np.mean(
# 步骤 1:重采样 stack_overflow.sample(frac=1, replace=True)['converted_comp']
)
)
1 自助分布在《Sampling in Python》第 4 章讲解
Python 假设检验

可视化自助法分布

import matplotlib.pyplot as plt
plt.hist(so_boot_distn, bins=50)
plt.show()

自助法分布直方图——呈钟形,约在 110000 到 140000 之间

Python 假设检验

标准误差

std_error = np.std(so_boot_distn, ddof=1)
5607.997577378606
Python 假设检验

z 分数

$\text{标准化值} = \dfrac{\text{值} - \text{均值}}{\text{标准差}}$

$z = \dfrac{\text{样本统计量} - \text{假设参数值}}{\text{标准误差}}$

Python 假设检验

$z = \dfrac{\text{样本统计量} - \text{假设参数值}}{\text{标准误差}}$

stack_overflow['converted_comp'].mean()
119574.71738168952
mean_comp_hyp = 110000
std_error
5607.997577378606
z_score = (mean_comp_samp - mean_comp_hyp) / std_error
1.7073326529796957
Python 假设检验

检验该假设

  • 1.707 是大还是小?
  • 这是本课程的目标!
Python 假设检验

检验该假设

  • 1.707 是大还是小?
  • 这是本课程的目标!

 

假设检验用例:

 

判断样本统计量与期望(或"假设")值是接近还是偏离

Python 假设检验

标准正态(z)分布

标准正态分布:均值 = 0,标准差 = 1 的正态分布

标准正态分布 PDF 的密度图

Python 假设检验

Passons à la pratique !

Python 假设检验

Preparing Video For Download...