用三種硬幣更新信念
假設硬幣不是只有公平或偏斜兩種,而是有三種可能:公平(正面機率 50%)、偏低(正面機率 25%)、偏高(正面機率 75%)。先驗機率為:公平 80%、偏低 10%、偏高 10%。
你觀察到 20 次擲硬幣中有 14 次為正面。硬幣是公平的機率是多少?
本練習屬於課程
R 的機率基礎
練習說明
- 使用
rbinom()函式模擬:公平硬幣 80,000 次、高偏硬幣 10,000 次、低偏硬幣 10,000 次,每次抽樣包含 20 次擲硬幣。分別儲存為flips_fair、flips_high、flips_low。 - 對每一種類型,計算結果等於 14 的硬幣數量。分別儲存為
fair_14、high_14、low_14。 - 計算硬幣為公平的後驗機率:用結果等於 14 的公平硬幣數量除以結果等於 14 的所有硬幣總數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Simulate 80,000 draws from fair coin, 10,000 from each of high and low coins
flips_fair <-
flips_high <-
flips_low <-
# Compute the number of coins that resulted in 14 heads from each of these piles
fair_14 <-
high_14 <-
low_14 <-
# Compute the posterior probability that the coin was fair