起因看onlineldavb.py的时候看到求E[log(theta)] 期望的函数如下:def dirichlet_expectation(alpha):
"""
For a vector theta ~ Dir(alpha), computes E[log(theta)] given alpha.
"""
if (len(alpha.shape) == 1):
return(psi(alpha) - psi(n.sum(alpha)))
return(psi(alpha) - psi(n.sum(alpha, 1))[:, n.newaxis])今天看了半天都不理解为何dirichlet的期望可以由上面的函数来实现,后来看到几个公式之后才明白,这里记录一下过程。psi函数psi应该是gamma函数的一阶导数,有以下代码可以理解:>>> from scipy import special
>>> x = [2, 3, 25.5]
>>> special.polygamma(1, x)
array([ 0.64493407, 0.39493407, 0.03999467])
>>> special.polygamma(0, x) == special.psi(x)
array([ True, True, True], dtype=bo
...
继续阅读
(31)