使用 scipy.integrate.dblquad 在 Python 中对 x*np.log(x) 进行双重积分

问题描述

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

enter image description 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),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

如何使函数起作用?

解决方法

第一个参数必须是可调用的,因此只需将其包装在 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)),1,lambda u: 0,lambda u: 1)[0] 

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