Velamuvi AIAcademy

Lesson 16 of 16 · Learning by Descent

Integrals in Brief

A short, visual introduction to integrals: area under a curve, Riemann sums, definite integrals and antiderivatives, the fundamental theorem of calculus, and where integrals appear in machine learning, from probabilities to AUC.

  • 9 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • What an integral measures: area and accumulation
  • How to approximate an integral with rectangles (a Riemann sum)
  • How to read integral notation
  • How integration and differentiation undo each other
  • Where integrals appear in machine learning

The other half of calculus

Derivatives answer "how fast is it changing?". Integrals answer the opposite question: "how much has built up in total?"

If you know a car's speed at every moment, the total distance travelled is the accumulated speed over time. On a graph of speed against time, that total is the area under the curve.

Approximating with rectangles

Areas under curves are hard to measure directly, but rectangles are easy. Split the interval into n strips, make each strip a rectangle as tall as the curve at its middle, and add up the rectangle areas. Increase the number of rectangles and watch the total approach the true area of 9:

-0.500.511.522.533.50246810
f(x) = x²
sum of 4 rectangles = 8.859
The area under x² from 0 to 3 is exactly 9. With more, thinner rectangles, the sum gets closer and closer to it.
Try it yourselfPython
def f(x):
    return x**2

a, b = 0, 3
for n in [4, 10, 100, 1000]:
    width = (b - a) / n
    total = 0
    for k in range(n):
        middle = a + (k + 0.5) * width      # middle of the k-th strip
        total += f(middle) * width          # height × width
    print(f"{n:>4} rectangles: area ≈ {total:.6f}")

Notation

 3
 ∫  x² dx  =  9         read: "the integral from 0 to 3 of x squared, d x"
 0

The ∫ sign is a stretched S, for "sum". dx stands for the tiny width of each strip. The numbers 0 and 3 are the limits of integration: where the area starts and ends.

Integration undoes differentiation

Here is the surprising connection at the heart of calculus. To find the area under f, find a function F whose derivative is f. Then:

 b
 ∫  f(x) dx  =  F(b) - F(a)
 a
Try it yourselfPython
import numpy as np
import sympy as sp

# Numerically, with NumPy's trapezoid rule (slanted strips instead of flat rectangles)
x = np.linspace(0, 3, 1001)
print("NumPy:", round(float(np.trapezoid(x**2, x)), 6))

# Exactly, with SymPy
t = sp.symbols("x")
print("antiderivative:", sp.integrate(t**2, t))
print("SymPy:", sp.integrate(t**2, (t, 0, 3)))

Integrals in machine learning

You will rarely compute integrals by hand in ML, but you will meet them constantly:

  • Probability. For a continuous quantity such as height, probability is the area under a probability density curve. The whole area is always exactly 1.
  • Expected value. The average outcome of a random quantity is an integral: each value weighted by its probability.
  • AUC. A classifier's quality is often reported as the area under the ROC curve, a number between 0.5 (guessing) and 1 (perfect).
  • Generative models. Diffusion models, which create images, are described with integrals over time.
Try it yourselfPython
import numpy as np

# The standard normal distribution (the bell curve) and its density formula
def normal_density(x):
    return np.exp(-x**2 / 2) / np.sqrt(2 * np.pi)

x = np.linspace(-1, 1, 2001)
probability = np.trapezoid(normal_density(x), x)
print(f"P(-1 < X < 1) = {probability:.4f}")    # about 68% of values lie within 1 standard deviation

x = np.linspace(-10, 10, 20001)
print(f"total area = {np.trapezoid(normal_density(x), x):.4f}")

Common mistakes

Practice exercise

Approximate an integral with a Riemann sum and watch it approach the exact answer, 9.

Your solutionPython
def f(x):
    return x**2

a, b = 0, 3

# For n = 10, 100 and 1000, approximate the area under f from a to b
# with n midpoint rectangles, and print it rounded to 4 places.

# Output must be exactly:
# 10 rectangles: 8.9775
# 100 rectangles: 8.9998
# 1000 rectangles: 9.0
Knowledge check

What does the fundamental theorem of calculus say?

Summary

  • An integral is accumulated total: the (signed) area under a curve.
  • A Riemann sum approximates it with rectangles, and the limit of thinner rectangles gives the exact value.
  • The fundamental theorem of calculus: area = F(b) - F(a), where F is an antiderivative of f.
  • In ML, integrals give probabilities, expected values and AUC, and are often estimated by sampling.

You have finished Calculus for Machine Learning

You can now explain limits and derivatives, use the derivative rules, compute derivatives in Python three ways, work with partial derivatives, gradients, Jacobians and Hessians, apply the chain rule through computation graphs, understand how automatic differentiation powers PyTorch, and train models with gradient descent while avoiding the classic traps. That is the complete calculus behind how machine learning models learn.

Where next?

  • Take the final quiz to check what you remember.
  • Continue with Probability, the maths of uncertainty that every model's predictions rely on.
  • Follow a career roadmap to see which maths each AI job needs.

Finished reading? Tick it off to track your progress.