Search code examples
pythonscipynumerical-integration

How to integrate a function defined by an array


I constructed and plotted a function defined by an array, according to the following code:

# set parameters
mean   = np.array([0, 3])   
sigma  = np.array([1, .8])  
weight = np.array([.4, .6])  
factor = 4
points = 1000

# set limits for plot
min_plot = (mean - (factor*sigma)).min()
max_plot = (mean + (factor*sigma)).max()

# set normal  distributions
d0 = stats.norm(mean[0], sigma[0])
d1 = stats.norm(mean[1], sigma[1])

# set mixed normal
data = np.array([d0.pdf(x), d1.pdf(x)])
y = np.dot(weight, data)

# displays
x = np.linspace(min_plot, max_plot, points)
plt.plot(x, y, '-', color = 'black', label='Normal mixed')

Which gave me the following plot: enter image description here

Please, what would be the simplest way to integrate $y$ between two given values, for example $x=2$ and $x=4$? I am aware of scipy.integrate, but don't understand how to use it in this particular case...


Solution

  • You need to define a function. Then you can use some numerical method for integration of that function within the specified boundaries.

    I am giving an example with scipy.integrate.quad:

    from scipy.integrate import quad
    from scipy import stats
    
    # define function to be integrated:
    def f(x):
        
        mean   = np.array([0, 3])   
        sigma  = np.array([1, .8])  
        weight = np.array([.4, .6])  
        factor = 4
        points = 1000
        
        # set normal  distributions
        d0 = stats.norm(mean[0], sigma[0])
        d1 = stats.norm(mean[1], sigma[1])
    
        # set mixed normal
        data = np.array([d0.pdf(x), d1.pdf(x)])
        y = np.dot(weight, data)
        
        return y
    
    # integrate function from 2 to 4
    quad(f, 2, 4)
    

    returns (0.4823076558823121, 5.354690645135298e-15), i.e. the integral and the absolute error associated with the interval.