Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - how to find area under curve? [closed]

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!

like image 839
SamKJ Avatar asked Mar 14 '23 20:03

SamKJ


1 Answers

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
like image 155
xnx Avatar answered Mar 17 '23 14:03

xnx