jpm*_*coy 6 python statistics numpy probability scipy
我想在python中计算Dirichlet发行版的pdf,但是无法在任何标准库中找到代码.scipy.stats包含一长串分布,但似乎不包括Dirichlet,numpy.random.mtrand允许一个样本,但不提供pdf.
由于Dirichlet很常见,我想知道是否有其他名称我应该通过scipy.stats或类似的搜索它,或者我是否只是错过了某种方式.
我在 numpy 中找不到一个,但它看起来足以实现。这是一个丑陋的小单线。(我遵循维基百科上给出的函数,除了你必须提供 x = [x1, ..., xk] 和 alpha = [a1, ..., ak])。
import math
import operator
def dirichlet_pdf(x, alpha):
return (math.gamma(sum(alpha)) /
reduce(operator.mul, [math.gamma(a) for a in alpha]) *
reduce(operator.mul, [x[i]**(alpha[i]-1.0) for i in range(len(alpha))]))
Run Code Online (Sandbox Code Playgroud)
警告:我还没有测试过这个。让我知道它是否有效。