Poisson 隨機變數
numpy.random 模組也提供多種適用於離散與連續隨機變數的機率分配。在本練習中,你將學會如何從機率分配中抽樣。
特別是,你會從一個非常重要的離散機率分配——Poisson 分配——中抽樣。它通常用來建模事件發生的平均速率。
完成本練習後,你就能把同樣的步驟套用到 numpy.random 中的任何機率分配。此外,你也會看到,當我們從同一個分配抽取更多樣本時,樣本平均數如何改變。
本練習屬於課程
Python 的統計模擬
練習說明
- 使用
np.random.poisson()以lam(lambda)與size_1從 Poisson 分配抽樣。 - 重複上述步驟,但這次改用
size_2。 - 對於上述每個樣本,使用
np.mean()與abs()計算其平均數與 lambda 的絕對差。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Initialize seed and parameters
np.random.seed(123)
lam, size_1, size_2 = 5, 3, 1000
# Draw samples & calculate absolute difference between lambda and sample mean
samples_1 = np.random.poisson(____, ____)
samples_2 = np.random.poisson(____, ____)
answer_1 = abs(____)
answer_2 = abs(____)
print("|Lambda - sample mean| with {} samples is {} and with {} samples is {}. ".format(size_1, answer_1, size_2, answer_2))