python日志n选择k

编程入门 行业动态 更新时间:2024-10-16 00:22:10
本文介绍了python日志n选择k的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-30 00:20:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648094.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!