0

I can't understand what's wrong with this code. Can someone help me please? This is a Pareto type II integrand from 1 to infinite and a and b are the parameters of the distribution. TypeError: object of type 'int' has no len() -> that's the error when I try to compute E

import numpy as np
from scipy.integrate import quad
from mpmath import *

def integrand(a, b, x):
    return x*a(b**a)/((x+1)**a)

a = 3                                                                                     
b = 2

E = quad(integrand, 1, np.inf, args=(a, b))
E
  • Where are you calling len() at? Also, I would avoid using single-letter variable names as it makes reading and understanding the code much harder. It's really hard to tell what you're actually doing here and I can't give a great answer. – Kwright02 Jun 06 '21 at 17:04
  • This is a statistical problem. It's a Pareto type II intergrand from 1 to infinite and a and b are the parameters of the distribution. TypeError: object of type 'int' has no len() -> that's the error when I try to compute E – Spyridoula Georgiou Jun 06 '21 at 17:15
  • Why import `mpmath`? – hpaulj Jun 06 '21 at 18:40

2 Answers2

3

This looks like import error to me ,both scipy and mpmath have implementation for quad method so to make the code work,will have to remove mpmath import statement. I could run the code as below..getting overflow for large value of the upper limit

import numpy as np
from scipy import integrate
#from scipy.integrate import quad
from mpmath import *

def intergrand(a, b, x):
    return x*a*(b**a)/((x+b)**a)
a = 3                                                                                     
b = 2

E = integrate.quad(intergrand,1, 100, args=(a, b))
print(E)
2

Remove the line

from mpmath import *

from your code.

mpmath has a quad function, so when you do from mpmath import *, you are overwriting the name that you imported from SciPy. You got the error TypeError: object of type 'int' has no len() because mpmath's version of quad expects the second argument to have the form [a, b], but you passed in 1.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • Thank you for your answer. If I do so it gives me another error: TypeError: integrand() takes 2 positional arguments but 3 were given – Spyridoula Georgiou Jun 06 '21 at 17:45
  • There are other problems; it would help if you tested the function shown in the question to ensure that it correctly returns a value when called. When I do `integrand(1, 2, 3)`, I get an error, because there is a mistake in the expression being computed (probably a missing `*`). – Warren Weckesser Jun 06 '21 at 17:50
  • Also, take another look at the docstring for [`scipy.integrate.quad`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html). You'll see that it expects the integration variable to be the *first* parameter of the integrand function, so the signature of your integrand function should be `def integrand(x, a, b)`. – Warren Weckesser Jun 06 '21 at 17:52
  • Thank you very much, I will check everything you pointed! – Spyridoula Georgiou Jun 06 '21 at 17:55