Integrals defined in python - Area under standard normal curve

1

I have two arrays with values x and y, "x" (value of random variable) are input values, and "y" are values calculated as a function of x (normal probability density function for x). How can I calculate the integral of "y" for a range of values in "x". I've seen how to do it, but they all involve writing the function in abstract form and that of the normal distribution is complicated.

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as st

# valores-puntos (x,y) de la función
x=np.linspace(-4,4,num=1000)
y=st.norm.pdf(x,0,1)
print(x,y)

I would like to calculate the integral between -1 and 0 for example. Maybe my only alternative is to write the function as f=lambda x: f(x) and use integrate.quad(f,lb,ub) ?

    
asked by Héctor Alonso 28.05.2018 в 16:13
source

1 answer

1

You do not need to write the function analytically, if that is your concern. It is enough that you have some way of computing the value of y , given x . Then to integrate.quad you pass the name of that function. And that's exactly what the st.norm.pdf() function does, right?

If the mean is 0 and the deviation is 1, you do not need to pass those parameters (they are the ones assumed by default). So, for example, the integral between -1 and 0 would be:

>>> integrate.quad(st.norm.pdf, -1, 0)
(0.341344746068543, 3.789687964201238e-15)

In case you want to pass a mean and deviation from the ones you use by default, you can program a fairly trivial lambda function, for example, for mean = 1, deviation = 2

>>> integrate.quad(lambda x: st.norm.pdf(x, 1, 2), -1, 0)
(0.14988228479452986, 1.6640276356231206e-15)

Or if you do not like to use lambdas, define your own separate function and pass it to integrate.quad() :

>>> def mydist(x):
...   return st.norm.pdf(x, 1, 2)
...
>>> integrate.quad(mydist, -1, 0)
(0.14988228479452986, 1.6640276356231206e-15)
    
answered by 28.05.2018 / 16:58
source