python - 微分熵是用 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
解决方案
无论如何,您必须以某种参数化的方式存储一个连续的随机变量,除非您使用近似值。在这种情况下,您通常使用分发对象;对于已知分布,存在关于参数的微分熵公式。
因此,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)
推荐阅读
- mysql - MySQL 查询性能非常依赖于 Where 子句
- c# - 在数据类型不固定的情况下读取 SQL 查询的结果
- python - 熊猫数据框中每个时期的一个图
- tableau-api - 如何使用 Tableau 修复字段或列值中的拼写错误
- css - 滑动手势菜单
- regex - 需要从 cmd 输出中截断无用的文本
- sql - oracle如何从case语句中获取函数返回结果
- sql-server - SQL Server Management Studio 拒绝查看对所有数据库的访问权限,但用户有权访问但没有使用户成为所有者的数据库
- ios - Nativescript-Vue IOS安全区布局问题
- python - 如果至少一个单元格是 NaN,则 Pandas 加入行