リサンプリング結果の可視化
前の演習で作成したシミュレーション結果を可視化しましょう。引き続き、NBA選手グループの体重(kg)を含む nba_weights を使います。
nba_weights = [96.7, 101.1, 97.9, 98.1, 98.1,
100.3, 101.0, 98.0, 97.4]
こちらが前の演習で使ったシミュレーションコードです。
simu_weights = []
for i in range(1000):
bootstrap_sample = random.choices(nba_weights, k=9)
simu_weights.append(np.mean(bootstrap_sample))
mean_weight = np.mean(simu_weights)
upper = np.quantile(simu_weights, 0.975)
lower = np.quantile(simu_weights, 0.025)
print(mean_weight, lower, upper)
前の演習で作成した simu_weights リストはすでに読み込まれています。同様に、mean_weight、lower、upper も、平均と信頼区間の2.5%および97.5%分位点として定義済みです。
次のパッケージは読み込み済みです:random、numpy は np、seaborn は sns、matplotlib.pyplot は plt。
この演習はコースの一部です
Pythonで学ぶモンテカルロ・シミュレーション
演習の手順
sns.displot()を使って、シミュレーションで得た体重の分布を描画します。plt.axvline()を使って、95%信頼区間の2本の縦線(lower、続いてupper)を赤で、平均を緑で描画します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Plot the distribution of the simulated weights
____
# Plot vertical lines for the 95% confidence intervals and mean
plt.axvline(____, color="red")
plt.axvline(____, color="red")
plt.axvline(____, color="green")
plt.show()