【问题标题】:Double integration of x*np.log(x) in Python using scipy.integrate.dblquad使用 scipy.integrate.dblquad 在 Python 中对 x*np.log(x) 进行双重集成
【发布时间】:2021-04-16 01:57:47
【问题描述】:

下面的代码使用与scipy.integrate.dblquad 的双重积分来计算copula 密度函数c 的微分熵c*np.log(c),该函数有一个依赖参数theta,通常为正。公式可以找到here

import numpy as np
from scipy import integrate 

def copula_entropy(theta):
    c = lambda v, u: ((1+theta)*(u*v)**(-1-theta)) * (u**(-theta) 
        + v**(-theta) -1)**(-1/theta-2)
    return -integrate.dblquad(c*np.log(c), 0, 1, lambda u: 0, lambda u: 1)[0] 

调用函数

copula_entropy(1)

返回错误

TypeError: loop of ufunc does not support argument 0 of type function which has no callable log method

如何使该功能起作用?

【问题讨论】:

  • 不知道是否重要,但((1+theta)*(u*v)**(-1-theta)) * (u*(-theta) + v*(-theta) -1)**(-1/theta) 似乎与 c(u, v) 的公式不匹配。
  • 哦,最后缺少-2,谢谢
  • 嗯,它还有u*(-theta)v*(-theta)而不是u**(-theta)v**(-theta)

标签: python scipy statistics integral entropy


【解决方案1】:

第一个参数必须是可调用的,所以只需将其包装在 lambda 本身中:

import numpy as np
from scipy import integrate 

def copula_entropy(theta):
    c = lambda v, u: ((1+theta)*(u*v)**(-1-theta)) * (u**(-theta)+v**(-theta)-1)**(-1/theta-2)
    return -integrate.dblquad(lambda u,v: c(v,u)*np.log(c(v,u)), 0, 1, lambda u: 0, lambda u: 1)[0] 

(请注意,我还根据您提供的公式更改了c 的表达式。

【讨论】:

  • 谢谢,我没有意识到我可以将输入参数传递给 lambda 函数。想知道为什么当 c 被定义为一个定义函数时它不起作用,我也尝试过
  • 知道是否可以使用相同的 lambda-wrap-lambda 方法使用 dblquad 将积分扩展到两个以上的变量 (u1,u2,u3,u4,...)?
  • 它的工作方式应该与这里完全相同,只需使用nquad 处理 n 个维度
  • 但是 lambda 函数可以为任意数量的 n 维做到这一点吗? stackoverflow.com/questions/65688402/…
  • 是的,就像f = lambda *u: np.sum(np.array([*u])),但你也可以只使用普通函数
猜你喜欢
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 2020-09-05
  • 2020-04-26
  • 1970-01-01
  • 2011-09-26
相关资源
最近更新 更多