python - how to find area under curve?

22,966

Solution 1

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

Solution 2

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.

Share:
22,966
Admin
Author by

Admin

Updated on December 22, 2020

Comments

  • Admin
    Admin over 3 years

    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!