James Mertz
James Mertz

Reputation: 8789

Mathematical Integration of a function in Python

I'm trying to integrate this function:

enter image description here

However I'm running into an error of:

Traceback (most recent call last):

  File "<ipython console>", line 1, in <module>

  File "siestats.py", line 349, in NormalDistro

    P_inner = scipy.integrate(NDfx,-dev,dev)

TypeError: 'module' object is not callable

My code runs this:

# Definition of the mathematical function:
def NDfx(x):

    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))

# This Function normailizes x, u, and o2 (position of interest, mean and st dev) 
# and then calculates the probability up to position 'x'

def NormalDistro(u,o2,x):


    dev = abs((x-u)/o2)


    P_inner = scipy.integrate(NDfx,-dev,dev)

    P_outer = 1 - P_inner

    P = P_inner + P_outer/2

    return(P)

Function NormalDistro is mean for import and used like this:

foo.NormalDistro(30,2.5,1.25)

As an example.

Upvotes: 3

Views: 5986

Answers (3)

tillsten
tillsten

Reputation: 14878

The integral of a gaussian is defined as the errorfunction. It is available from pythons math libaray or scipy.special (if you need a vectorized function). Its called erf. Also available is erfc, the complementary error function.

Upvotes: 2

DMA57361
DMA57361

Reputation: 3690

The module you are attempting to call is scipy.integrate, you need to call one of the functions within the module. Based on previous comments on chat you are probably wanting to use scipy.integrate.quad().

Also, it returns a tuple of (Result,MaximumError), so you shouldn't use P_inner directly in the sum for P, you want P = P_inner[0] + P_outer/2

Upvotes: 7

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56428

return doesn't require (), simply return P works just fine

Though this actually wouldn't fix the issue you're seeing, as return(foo) should also work.

More information on the error would be helpful.

Upvotes: 2

Related Questions