我正在尝试通过矩估计 alpha 和 beta 的方法来进行 beta 二项式分布。采取以下步骤: http ://en.wikipedia.org/wiki/Beta-binomial_distribution#Maximum_likelihood_estimation
我认为我已经能够在 python 中准确地编写代码:
import numpy as np
males = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=float)
fams = np.array([3, 24, 104, 286, 670, 1033, 1343, 1112, 829, 478, 181, 45, 7], dtype=float)
n = 12
k = 1
m_1 = (sum(fams*males**k))/(sum(fams))
k = 2
m_2 = (sum(fams*males**k))/(sum(fams))
alpha = (n*m_1-m_2)/(n*(m_2/m_1-m_1-1)+m_1)
beta = (n-m_1)*(n-m_2/m_1)/(n*(m_2/m_1-m_1-1)+m_1)
print "n =", n
print "m_1 =", m_1
print "m_2 =", m_2
print "alpha =", alpha
print "beta =", beta
输出:
n = 12
m_1 = 6.23058053966
m_2 = 42.3094031071
alpha = 34.135021177
beta = 31.6084920506
这与示例的结果相同。但是,如果我使用使用最大似然的 R 包 VGAM,则对 alpha 和 beta 的估计完全不同
x = c(0,1,2,3,4,5,6,7,8,9,10,11,12)
y = c(3,24,104,286,670,1033,1343,1112,829,478,181,45,7)
library("VGAM")
fit=vglm(cbind(x, y) ~ 1, betabinomialff, trace = TRUE)
Coef(fit)
shape1 shape2
0.4241806 4.9069560
难道我做错了什么?