簡體   English   中英

β二項式和β分布的alpha和beta估計值

[英]alpha and beta estimates for beta binomial and beta distributions

我試圖將我的數據擬合到β二項分布並估計alpha和beta形狀參數。 對於此分布,先驗取自beta分布。 Python沒有beta-binomial的擬合函數,但它適用於beta。 python beta擬合和R beta二項式擬合是緊密但系統性的。

R:

library("VGAM")
x = c(222,909,918,814,970,346,746,419,610,737,201,865,573,188,450,229,629,708,250,508)
y = c(2,18,45,11,41,38,22,7,40,24,34,21,49,35,31,44,20,28,39,17)
fit=vglm(cbind(y, x) ~ 1, betabinomialff, trace = TRUE)
Coef(fit)
   shape1    shape2 
  1.736093 26.870768

蟒蛇:

import scipy.stats
import numpy as np
x = np.array([222,909,918,814,970,346,746,419,610,737,201,865,573,188,450,229,629,708,250,508], dtype=float)
y = np.array([2,18,45,11,41,38,22,7,40,24,34,21,49,35,31,44,20,28,39,17])
scipy.stats.beta.fit((y)/(x+y), floc=0, fscale=1)
    (1.5806623978910086, 24.031893492546242, 0, 1)

我已經多次這樣做了,似乎python系統性地比R結果略低一點。 我想知道這是我輸入錯誤還是只是計算方式的差異?

您的問題是,擬合β二項式模型與使用等於比率的值擬合Beta模型不同。 我將在這里用bbmle包來說明,它將類似的模型適用於VGAM (但我更熟悉它)。

預賽:

library("VGAM")  ## for dbetabinom.ab
x <- c(222,909,918,814,970,346,746,419,610,737,
       201,865,573,188,450,229,629,708,250,508)
y <- c(2,18,45,11,41,38,22,7,40,24,34,21,49,35,31,44,20,28,39,17)

library("bbmle")

適合beta-binomial模型:

mle2(y~dbetabinom.ab(size=x+y,shape1,shape2),
     data=data.frame(x,y),
     start=list(shape1=2,shape2=30))
## Coefficients:
##    shape1    shape2 
##  1.736046 26.871526 

這與您引用的VGAM結果或多或少完全一致。

現在使用相同的框架來代替Beta模型:

mle2(y/(x+y) ~ dbeta(shape1,shape2),
     data=data.frame(x,y),
     start=list(shape1=2,shape2=30))
## Coefficients:
##    shape1    shape2 
## 1.582021 24.060570 

這符合您的Python,beta-fit結果。 (我敢肯定,如果你使用VGAM來適應Beta,你也會得到同樣的答案。)

您可以使用conjugate_prior包進行python

有關硬幣翻轉示例,請參閱代碼:

from conjugate_prior import BetaBinomial
heads = 95
tails = 105
prior_model = BetaBinomial() #Uninformative prior
updated_model = prior_model.update(heads, tails)
credible_interval = updated_model.posterior(0.45, 0.55)
print ("There's {p:.2f}% chance that the coin is fair".format(p=credible_interval*100))
predictive = updated_model.predict(50, 50)
print ("The chance of flipping 50 Heads and 50 Tails in 100 trials is {p:.2f}%".format(p=predictive*100))

代碼來自這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM