使用scipy.integrate.nquad的多重积分-Lambda函数的错误 [英] Multiple integral using scipy.integrate.nquad - errors on lambda function

查看:99
本文介绍了使用scipy.integrate.nquad的多重积分-Lambda函数的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在下面的代码中使用以下多个整数,以获得高于

 将numpy导入为np从scipy导入集成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)),范围=(0,1)** n,args =(theta,))[0]theta,n = 1,3打印(H(theta)) 

其中 * us 表示我可以传入的 u 的任意数量. integrate.nquad 的第二个输入参数,是 ranges =(0,1)** n ,由于 u n 维度而为整数的[0,1]支持,我尝试在上面的推导中进行解释.但是,这段代码给出了以下错误.

  • TypeError:**或pow()不支持的操作数类型:'tuple'和'int'

如果我按照错误建议删除指数 n ,从而将此输入更改为 ranges =(0,1),那么我将得到另一个错误:

  • 低,高= fn_range(* args)

    TypeError:无法解压缩不可迭代的int对象

我真的应该如何声明对 integrate.nquad 中的多重积分的 [(0,1)** n] 支持?文档没有给出匹配的示例

解决方案

由于评论部分已超载,因此发布一些评论作为答案.

(1)我想用(0,1)** 2 来表示乘积(0,1)x(0,1),即单位平方.我认为Python无法理解这一点.如果您需要告诉nquad积分的范围是单位平方,我想您需要说其他一些方式.

(2)请发布您实际使用的函数的公式,即c(u).请记住,当您省略细节或将其推迟到链接时,只会使他人更难以理解,因此获得有效帮助的可能性也较小.

(3)熵计算的积分范围是概率函数的所谓支持,即,概率大于零的集合.c(u)的支持是什么?这就定义了集成领域.在教科书或其他说明中,习惯上都说域是R或(-inf,+ inf)或以其他方式使其模棱两可,但是对于实际计算,您需要将其缩减为支持范围.

很抱歉,我无法提供更多帮助,这是一个有趣的问题.

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.

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.

解决方案

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.

这篇关于使用scipy.integrate.nquad的多重积分-Lambda函数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆