I am coding the derivation of a Dirichlet-Multinomial posterior in a paper that I've read and I am having trouble getting the distribution to sum to 1. Here is the code in its unsimplified form:
def pcn(X, n, N, c, alpha):
pnc = np.math.factorial(np.sum([n[i] for i in range(len(n))]))/ \
np.product([np.math.factorial(n[i]) for i in range(len(n))])* \
np.product([c[i]**n[i] for i in range(len(n))])
pc = G(len(X)*alpha)/ \
np.product([G(alpha) for i in range(len(n)) if i in X])* \
np.product([(c[i])**(alpha - 1) for i in range(len(n)) if i in X])
pn = np.math.factorial(N)/ \
np.product([np.math.factorial(n[i]) for i in range(len(n)) if i in X])* \
G(len(X)*alpha)/ \
G(len(X)*alpha + N)* \
np.product([G(alpha + n[i])/G(alpha) for i in range(len(n)) if i in X])
return pnc
which I've simplified here removing the parts that get divided out:
def pcns(X, n, N, c, alpha):
pnc = np.product([c[i]**n[i] for i in range(len(n))])
pc = np.product([(c[i])**(alpha - 1) for i in range(len(n))])/ \
np.product([G(alpha) for i in range(len(n))])
pn = np.product([G(alpha + n[i])/G(alpha) for i in range(len(n))])/ \
G(len(X)*alpha + N)
return pnc * pc / pn
I set the input variables and initialize the array of c to input:
X = [0,1]
n = [6.0, 3.0]
N = sum(n)
alpha = 20
c = np.linspace(0, 1, 1000)
and then I iterate over c and evaluate my posterior function at each c and plot:
dist = []
for i in c:
dist.append(pcns(X, n, N, [i, 1-i], alpha))
plt.plot(c,dist)
The value I get when I sum over dist
is 999 or len(c) - 1
. Would anyone happen to know why it doesn't some to 1?