\n
import numpy as np\nfrom scipy import integrate \n\ndef copula_entropy(theta):\n c = lambda v, u: ((1+theta)*(u*v)**(-1-theta)) * (u**(-theta) \n + v**(-theta) -1)**(-1/theta-2)\n return -integrate.dblquad(c*np.log(c), 0, 1, lambda u: 0, lambda u: 1)[0] \n
\nCalling the function with
\ncopula_entropy(1)\n
\nreturns the error
\nTypeError: loop of ufunc does not support argument 0 of type function which has no callable log method\n
\nHow can the function be made to work?
\n","author":{"@type":"Person","name":"develarist"},"upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The first argument must be a callable, so just wrap it in a lambda
itself:
import numpy as np\nfrom scipy import integrate \n\ndef copula_entropy(theta):\n c = lambda v, u: ((1+theta)*(u*v)**(-1-theta)) * (u**(-theta)+v**(-theta)-1)**(-1/theta-2)\n return -integrate.dblquad(lambda u,v: c(v,u)*np.log(c(v,u)), 0, 1, lambda u: 0, lambda u: 1)[0] \n
\n(Please note that I also changed the expression for c
according to the formula you gave).
Reputation: 1375
The code below uses double integration with scipy.integrate.dblquad
to calculate the differential entropy, c*np.log(c)
, of a copula density function c
, which has one dependence parameter, theta
, usually positive. Formula can be found 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]
Calling the function with
copula_entropy(1)
returns the error
TypeError: loop of ufunc does not support argument 0 of type function which has no callable log method
How can the function be made to work?
Upvotes: 1
Views: 321
Reputation: 30669
The first argument must be a callable, so just wrap it in a lambda
itself:
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]
(Please note that I also changed the expression for c
according to the formula you gave).
Upvotes: 1