首页 > 解决方案 > 微分熵是用 scipy.stats 中的integrate.quad 计算的?

问题描述

scipy.stats.entropy计算连续随机变量的微分熵. 究竟是用什么估计方法,用什么公式计算微分熵的?(即分布的微分熵与norm分布的熵beta

下面是它的github代码。微分熵是 pdf 乘以 log pdf 的负积分和,但我看不到这个或写的 log。可能是在调用 tointegrate.quad吗?

def _entropy(self, *args):
    def integ(x):
        val = self._pdf(x, *args)
        return entr(val)

    # upper limit is often inf, so suppress warnings when integrating
    _a, _b = self._get_support(*args)
    with np.errstate(over='ignore'):
        h = integrate.quad(integ, _a, _b)[0]

    if not np.isnan(h):
        return h
    else:
        # try with different limits if integration problems
        low, upp = self.ppf([1e-10, 1. - 1e-10], *args)
        if np.isinf(_b):
            upper = upp
        else:
            upper = _b
        if np.isinf(_a):
            lower = low
        else:
            lower = _a
        return integrate.quad(integ, lower, upper)[0]

来源(第 2501 - 2524 行):https ://github.com/scipy/scipy/blob/master/scipy/stats/_distn_infrastructure.py

标签: pythonentropycontinuousinformation-theoryscipy.stats

解决方案


无论如何,您必须以某种参数化的方式存储一个连续的随机变量,除非您使用近似值。在这种情况下,您通常使用分发对象;对于已知分布,存在关于参数的微分熵公式。

因此,Scipy 提供了一种在可能的情况下计算微分熵的entropy方法rv_continuous

In [5]: import scipy.stats as st                                                                                                                             

In [6]: rv = st.beta(0.5, 0.5)                                                                                                                               

In [7]: rv.entropy()                                                                                                                                         
Out[7]: array(-0.24156448)

推荐阅读