Beta Binomial Function in Python

偶尔善良 提交于 2019-12-04 03:36:20

问题


I would like to calculate the probability given by a binomial distribution for predetermined x(successes), n(trials), and p(probability) - the later of which is given by a probability mass function Beta(a,b).

I am aware of scipy.stats.binom.pmf(x,n,p) - but I am unsure how I can replace p with a probability function. I am also wondering whether I could use the loc argument of scipy.stats.binom.pmf to emulate this behaviour.


回答1:


Wiki says that the compound distribution function is given by

f(k|n,a,b) = comb(n,k) * B(k+a, n-k+b) / B(a,b)

where B is the beta function, a and b are the original Beta parameters and n is the Binomial one. k here is your x and p disappears because you integrate over the values of p to obtain this (convolution). That is, you won't find it in scipy but it is a one-liner provided you have the beta function from scipy.




回答2:


If your values of n (total # trials) and x (# successes) are large, then a more stable way to compute the beta-binomial probability is by working with logs. Using the gamma function expansion of the beta-binomial distribution function, the natural log of your desired probability is:

ln(answer) = gammaln(n+1) + gammaln(x+a) + gammaln(n-x+b) + gammaln(a+b) - \
        (gammaln(x+1) + gammaln(n-x+1) + gammaln(a) + gammaln(b) + gammaln(n+a+b))

where gammaln is the natural log of the gamma function, given in scipy.special.

(BTW: The loc argument just shifts the distribution left or right, which is not what you want here.)



来源:https://stackoverflow.com/questions/26935127/beta-binomial-function-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!