模擬後驗抽樣
你剛決定對療效率使用 Beta(5, 2) 先驗。同時,你用二項分配來建模資料(治癒一位病患代表「成功」,還記得嗎?)。由於 beta 分配是二項概似的共軛先驗,你可以直接模擬後驗!
你知道如果先驗是 $Beta(a, b)$,則後驗是 $Beta(x, y)$,其中:
$x = NumberOfSuccesses + a$,
$y = NumberOfObservations - NumberOfSuccesses + b$。
你能模擬後驗分配嗎?回想一下,總共有 22 位病患的資料,其中有 19 位已被治癒。numpy 與 seaborn 已分別以 np 與 sns 匯入供你使用。
本練習屬於課程
Bayesian Data Analysis in Python
練習說明
- 將接受治療與被治癒的病患人數,分別指定給
num_patients_treated與num_patients_cured。 - 使用合適的
numpy函式從後驗分配取樣,並將結果指定給posterior_draws。 - 使用合適的
seaborn函式繪製後驗分配。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define the number of patients treated and cured
num_patients_treated = ____
num_patients_cured = ____
# Simulate 10000 draws from the posterior distribuition
posterior_draws = ____(____ + ____, ____ - ____ + ____, 10000)
# Plot the posterior distribution
____(____, shade=True)
plt.show()