開始使用免費開始

檢定力分析 - 第二部分

先前我們已經模擬過一次實驗,並產生了 p 值。現在要用同樣的架構來計算統計檢定力(power)。一個實驗的檢定力,代表當處置組與對照組之間真的存在差異時,實驗偵測出這個差異的能力。良好的統計實務是把目標設定在 80% 的檢定力。

以我們的網站為例,假設你想知道每個版本各需要多少造訪人數,才能在 80% 的檢定力下偵測到停留時間提升 10% 的效果。做法是從小樣本(50)開始,模擬多次實驗並檢查檢定力。如果達到 80% 就停止;否則就增加樣本數,然後再試一次。

本練習屬於課程

Python 的統計模擬

檢視課程

練習說明

  • 對於 time_spent 這些隨機變數,將 size 設為 tuple,使形狀為 sample_size × sims
  • power 計算為小於 0.05(具統計顯著)的 p 值所占比例。
  • power 大於或等於 80%,就在 while 迴圈中使用 break 跳出。否則持續將 sample_size 以 10 為單位遞增。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

sample_size = 50

# Keep incrementing sample size by 10 till we reach required power
while 1:
    control_time_spent = np.random.normal(loc=control_mean, scale=control_sd, size=(____,____)))
    treatment_time_spent = np.random.normal(loc=control_mean*(1+effect_size), scale=control_sd, size=(____,____))
    t, p = st.ttest_ind(treatment_time_spent, control_time_spent)
    
    # Power is the fraction of times in the simulation when the p-value was less than 0.05
    power = (p < 0.05).sum()/____
    if ____: 
        ____
    else: 
        ____ += ____
print("For 80% power, sample size required = {}".format(sample_size))
編輯並執行程式碼