scipy.miscb,返回 n 选择 k,使用 gammaln 函数实现.是否有保留在日志空间中的函数?我看到没有 scipy.miscbln 或任何类似的.自己实现很简单,但如果它已经在某个地方的包中会很方便.我在scipy.misc里没有看到,只是感觉转换成普通空间再回log很浪费.
scipy.miscb, returning n choose k, is implemented using the gammaln function. Is there a function that stays in log space? I see there is no scipy.miscbln or any similar. It is trivial to implement myself, but it would be convenient if it were already in a package somewhere. I don't see it in scipy.misc, and it just feels wasteful to convert to normal space and then back to log.
推荐答案可以使用 gammaln,但是当 N >>k.这可以通过与 beta 函数的关系来避免:
It's possible to use gammaln, but precision is lost in subtractions when N >> k. This can be avoided via the relation to the beta function:
from numpy import log from scipy.special import betaln def binomln(n, k): # Assumes binom(n, k) >= 0 return -betaln(1 + n - k, 1 + k) - log(n + 1)更多推荐
python日志n选择k
发布评论