Introduction to Integration For Machine Learning
Tags: Mathematics, Machine Learning, Colab, Scipy

Working in machine learning we some need to use integration to do our work more easily. Mainly integrals are the inverses of derivatives. More importantly, using integration provides a way to compute the area under the curve of almost any function. There are many applications for integration. For example, if you need to compute a probability of some occurrence between limits (which we’ll discuss later in this course), then you will use an integral.

Let’s start with a simple function:

f(x)=x

We can plot this function as a line. Run the code below to plot the function for the range 0 to 10:

The output of this code will look like this :

Plotting the 0–10 points

Performing Integration

The integral of a function is the area under it — in this case, the area under the purple diagonal line down to the x-axis.

So how do you find the integral of a function? well, for our simple function f(x)=x, the formula for an integral is written as follows:

The ∫ symbol shows that this formula is integral. The dx indicates that the integration is with respect to the x variable. Note that since f(x)=x, we could also write this integral formula as ∫xdx

So, what is the integral of x dx? To answer this question, we need the antiderivative off — in other words we need to find a function that has a derivative matching the output off, which is just x. Using the power rule in reverse, a function that has the derivative x would be :

So, the unbound integral formula for f with respect to x can be written as:

Integration between Limits

Now that we have the unbound integral formula, we can use it to find the integral between specific start and endpoints. Let’s suppose we want to find the area under the function between the x values 0 and 2. In other words, the integral of f for the range 0 to 2 with respect to x.

Run the following code to re-plot the function and show the area we’re interested in:

The integral area is orange

We call the start and end point the limits of the integral. The lower limit is placed as a subscript of the integral sign. The upper limit is placed as a superscript of the integral sign. Using this notation the integral of f(x) from 0 to 2 is written as follows:

The integral is evaluated by subtracting the value of the integrand at the lower limit from the integrand at the upper limit; and since we know the formula based on our antiderivative function, the integral can be evaluated in the following manner.

Execute the code in the cell below and verify that the result returned by the



scipy.integrate.quad

function in Python is approximately the same as we computed analytically.

Another Integral

Here is another example for a slightly more complex function. What is the area under the curve of the function 3x^2+2x+1 between 0 and 3?

let’s look at that function and the area in question:

Output Graph

We can evaluate this integral just as before, this time using function:

Now, execute the code in the cell below to verify the result:

Note that the result from the scipy.integrate.quad function is approximate — the function actually returns an estimated integral (i in this case) and also a measure of absolute error (e). Run the following code to see what the absolute error was in this case:

The absolute error, in this case, is extremely small (around 4.32 * 10^-13)

Infinite limits

In many cases the limits of an integral can be +/−∞. Perhaps surprisingly, this situation is not really a problem if the function being integrated converges to 0 at the infinite limit.

Here is an example. The function e^−5x→0 as x→∞. Therefore, the integral of this function from some limit to ∞.

This integral can be written as follows:

This integral converges to a small number with a much smaller error estimate.

Here is another example that illustrates why having infinite integration limits is so useful. When computing probabilities it is often necessary to have infinite limits. Don’t worry too much about the details of probability theory. This is covered in a later lesson.

A Normal distribution with zero mean and a standard deviation of 1 has the following density function:

It makes sense that the integral of this probability density function from −∞ to ∞ must be 1.0. In other words the probability of a Normally distributed event ocurring at all possible values must be 1.0.

The code in the cell below computes the following integral:

Execute this code and verify that the result is approximately 1.0

That’s For today . We will more advance tommorow.

Every code in the exapmle is execute in Google Colab .

Github Repo Link : https://github.com/CREVIOS/Python-machine-learning-math/blob/master/Introduction_to_Integration.ipynb

.