假設檢定與 z 分數

Python 中的假設檢定

James Chapman

Curriculum Manager, DataCamp

A/B 測試

  • 2013 年,Electronic Arts(EA)推出 SimCity 5
  • 他們想要提高遊戲的預購量
  • 他們用 A/B 測試比較不同廣告情境
  • 做法是將使用者分成「控制組」與「實驗組」

Electronic Arts 大樓

1 圖片來源:「Electronic Arts」,作者 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 distribution)

import numpy as np

# Step 3. Repeat steps 1 & 2 many times, appending to a list so_boot_distn = [] for i in range(5000): so_boot_distn.append(
# Step 2. Calculate point estimate np.mean(
# Step 1. Resample 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{standardized value} = \dfrac{\text{value} - \text{mean}}{\text{standard deviation}}$

$z = \dfrac{\text{sample stat} - \text{hypoth. param. value}}{\text{standard error}}$

Python 中的假設檢定

$z = \dfrac{\text{sample stat} - \text{hypoth. param. value}}{\text{standard error}}$

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 的常態分佈

標準常態分佈機率密度函數的密度圖

Python 中的假設檢定

一起來練習吧!

Python 中的假設檢定

Preparing Video For Download...