Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.9k views
in Technique[技术] by (71.8m points)

python - Multiple integral using scipy.integrate.nquad - errors on lambda function

I attempt the following multiple integral in the code below for a higher-than-bivariate version (n=2) of a copula density function, c(u1,u2). In other words, n>2 dimensions.

enter image description here

import numpy as np
from scipy import integrate

def H(theta):
    c = lambda *us: ((1+theta)*np.prod(*us)**(-1-theta)) * (np.sum(*us **(-theta))-1)**(-1/theta-2)
    return -integrate.nquad(
        func   = lambda *us : c(*us)*np.log(c(*us)), 
        ranges = (0,1)**n, 
        args   = (theta,) 
        )[0] 

theta, n = 1, 3
print(H(theta))

where *us represents the arbitrary number of u's I can pass in. The second input argument to integrate.nquad, which is ranges=(0,1)**n, is the [0,1] support of the integral due to n dimensions of u's, which I try to explain in the derivation above. However, this part of the code gives the following error.

  • TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'

If I change this input to ranges=(0,1) by removing the exponent n as suggested by the error, then I get a different error:

  • low, high = fn_range(*args)

    TypeError: cannot unpack non-iterable int object

How am I really supposed to declare the [(0,1)**n] support for a multiple integral in integrate.nquad? The documentation does not give a matching example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Posting some comments as an answer since the comments section is getting overloaded.

(1) I think by (0, 1)**2 you mean to say the product (0, 1) x (0, 1), i.e., the unit square. I don't think Python is going to understand that. If you need to tell nquad that the domain of integration is the unit square, I think you need to say that some other way.

(2) Please post the formula for the function you are actually working with, namely c(u). Bear in mind that when you omit details or defer them to links, it only makes it harder for others to understand and therefore less likely that you can get effective help.

(3) The domain of integration for entropy calculation is the so-called support of the probability function, that is, the set on which the probability is greater than zero. What is the support of c(u)? That defines the domain of integration. It is customary in textbooks or other expositions to say the domain is R or (-inf, +inf) or otherwise leave it ambiguous, but for an actual calculation you need to cut it down to the support.

I'm sorry I can't be more helpful, it's an interesting question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...