無安打發生得多頻繁?
現代(1901–2015)美國職棒大聯盟中,每次無安打之間所經過的比賽場數,已存放在陣列 nohitter_times 中。
如果你假設無安打可用卜瓦松程序來描述,則無安打之間的時間會服從指數分佈。正如你所見,指數分佈只有一個參數,我們記為 $\tau\(,代表典型的間隔時間。讓指數分佈最符合資料的參數 \)\tau$,就是無安打之間的平均間隔時間(時間單位為比賽場數)。
請從資料計算此參數的值。接著,使用 np.random.exponential(),以你求得的 \(\tau\) 從指數分佈抽取無安打間隔時間,來「重演」美國職棒的歷史,並將柱狀圖繪製為 PDF 的近似。
已為你匯入 NumPy、pandas、matplotlib.pyplot 和 seaborn,分別命名為 np、pd、plt 與 sns。
本練習屬於課程
Statistical Thinking in Python(第 2 部分)
練習說明
- 將隨機數產生器設為種子
42。 - 計算無安打之間的平均時間(單位為比賽場數)。
- 從具有你以無安打間隔平均數計得之參數的指數分佈中抽取 100,000 個樣本。
- 使用
plt.hist()繪製理論 PDF。請使用關鍵字引數bins=50、density=True、histtype='step'。記得為座標軸加上標籤。 - 顯示你的圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Seed random number generator
____
# Compute mean no-hitter time: tau
tau = ____
# Draw out of an exponential distribution with parameter tau: inter_nohitter_time
inter_nohitter_time = ____(____, 100000)
# Plot the PDF and label axes
_ = ____(inter_nohitter_time,
____, ____, ____)
_ = plt.xlabel('Games between no-hitters')
_ = plt.ylabel('PDF')
# Show the plot
plt.show()