问题描述
下面的代码使用与 scipy.integrate.dblquad
的双重积分来计算具有一个依赖参数 c*np.log(c)
的 copula 密度函数 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),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
的表达式)。