4

would like to ask if it is possible to calculate the area under curve for a fitted distribution curve?

The curve would look like this

enter image description here

I've seen some post online regarding the usage of trapz, but i'm not sure if it will work for a curve like that. Please enlighten me and thank you for the help!

Tim
  • 41,901
  • 18
  • 127
  • 145
SamKJ
  • 41
  • 1
  • 1
  • 3
  • 4
    what is your input? function or set of points? – HadiRj Dec 21 '15 at 08:29
  • 2
    Possible duplicates: http://stackoverflow.com/questions/13320262/calculating-the-area-under-a-curve-given-a-set-of-coordinates-without-knowing-t http://stackoverflow.com/questions/22238489/python-integrating-area-under-curve-with-uneven-steps-in-x http://stackoverflow.com/questions/10814353/using-scipy-to-perform-discrete-integration-of-the-sample – albert Dec 21 '15 at 08:33
  • 3
    It's math stuff, just integrate the function. – Netwave Dec 21 '15 at 10:17
  • Please give the (minimal) code you have so far. – Martin Thoma Dec 21 '15 at 10:31
  • I'd guess the sum is exactly 1 and the answer to your question is yes. – Ulrich Eckhardt Dec 21 '15 at 10:33

2 Answers2

7

If your distribution, f, is discretized on a set of points, x, that you know about, then you can use scipy.integrate.trapz or scipy.integrate.simps directly (pass f, x as arguments in that order). For a quick check (e.g. that your distribution is normalized), just sum the values of f and multiply by the grid spacing:

import numpy as np
from scipy.integrate import trapz, simps

x, dx = np.linspace(-100, 250, 50, retstep=True)
mean, sigma = 90, 20
f = np.exp(-((x-mean)/sigma)**2/2) / sigma / np.sqrt(2 * np.pi)
print('{:18.16f}'.format(np.sum(f)*dx))
print('{:18.16f}'.format(trapz(f, x)))
print('{:18.16f}'.format(simps(f, x)))

Output:

1.0000000000000002
0.9999999999999992
1.0000000000000016
xnx
  • 24,509
  • 11
  • 70
  • 109
1

Firstly, you have to find a function from a graph. You can check here. Then you can use integration in python with scipy. You can check here for integration. It is just math stuff as Daniel Sanchez says.

umuur
  • 91
  • 1
  • 11