検定統計量を可視化する
この演習では、帰無仮説に近づくために、2 通りの方法で得られた検定統計量の分布を比較します。
まず、早い時間帯と遅い時間帯でグループ化した 2 つの「母集団」を調べ、検定統計量の分布を計算します。次に、2 つの母集団をシャッフルして時間順をなくし、それぞれが早い・遅い時間の混在となるようにしてから、検定統計量の分布を再計算します。
準備として、2 つの時間継続グループ group_duration_short と group_duration_long、および 2 つの関数 shuffle_and_split() と plot_test_statistic() をあらかじめ読み込んであります。
この演習はコースの一部です
Pythonで学ぶ線形モデリング入門
演習の手順
np.random.choice()を使ってgroup_duration_shortとgroup_duration_longをリサンプリングし、リサンプルの差を取ってtest_statistic_unshuffledを計算します。- 元の
group_duration_shortとgroup_duration_long(この順序で指定)に対してshuffle_and_split()を用い、2 つの新しい混合集団を作成します。 - シャッフル後の集団をリサンプリングし、
resample_longからresample_shortを引いて、新しいtest_statistic_shuffledを計算します。 plot_test_statistic()を使って、2 つの検定統計量の分布をプロットし、見た目で比較します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# From the unshuffled groups, compute the test statistic distribution
resample_short = np.random.choice(____, size=500, replace=____)
resample_long = np.random.choice(____, size=500, replace=____)
test_statistic_unshuffled = ____ - ____
# Shuffle two populations, cut in half, and recompute the test statistic
shuffled_half1, shuffled_half2 = shuffle_and_split(____, ____)
resample_half1 = np.random.choice(____, size=500, replace=____)
resample_half2 = np.random.choice(____, size=500, replace=____)
test_statistic_shuffled = resample_half2 - resample_half1
# Plot both the unshuffled and shuffled results and compare
fig = plot_test_statistic(____, label='Unshuffled')
fig = plot_test_statistic(____, label='Shuffled')