Integral Calculus

Dive into Deep Learning · §34.6

Integral Calculus
Riemann sums · Fundamental Theorem · improper integrals · integration by parts & substitution · Fubini’s theorem · Jacobian determinants · Gaussian integral

Why Continuous Probabilities Require Integration

Motivation

  • The Standard Normal Paradox: For Z \sim \mathcal{N}(0, 1), density height peaks at p(0) \approx 0.40, yet single-point probability is P(Z = 0) = 0; only intervals carry positive area (P(-1 \le Z \le 1) = \int_{-1}^1 p(z)\,dz \approx 0.683).
  • Two Foundational Questions: How do we evaluate definite integrals via antiderivatives, and why does \int_{-\infty}^\infty e^{-z^2/2}\,dz = \sqrt{2\pi}?

Riemann Sums: Slicing Area into Thin Rectangles

The Definite Integral

  • Left-endpoint rectangles on f(x) = x^2 over [0, 1]: Dividing [0, 1] into N slices of width \epsilon = 1/N gives the exact closed-form sum: \sum_{i=0}^{N-1} \frac{1}{N}\left(\frac{i}{N}\right)^2 = \frac{1}{3} - \frac{1}{2N} + \frac{1}{6N^2} which converges to \tfrac{1}{3} with first-order error \approx \tfrac{1}{2N} = \tfrac{\epsilon}{2}.
  • General Riemann integral: A bounded function f is Riemann integrable on [a, b] if every tagged partition sum \sum_{i=1}^N f(\xi_i)(x_i - x_{i-1}) converges to the same limit \int_a^b f(x)\,dx as the maximum slice width \|P\| \to 0.

First-Order Discretization Error of Left Sums

Convergence Rate

On our running integral \int_0^2 \frac{x}{1 + x^2}\,dx = \tfrac{1}{2}\log 5 \approx 0.804719, cutting the slice width \epsilon tenfold reduces the left-sum error tenfold:

eps=1e-01  sum=0.783785  truth-sum=2.09e-02  0.2*eps=2e-02
eps=1e-02  sum=0.802710  truth-sum=2.01e-03  0.2*eps=2e-03
eps=1e-03  sum=0.804519  truth-sum=2.00e-04  0.2*eps=2e-04
eps=1e-04  sum=0.804699  truth-sum=2.00e-05  0.2*eps=2e-05
truth = (1/2) log 5 = 0.804719

Why the error equals \tfrac{\epsilon}{2}(f(a) - f(b)) = -0.2\epsilon: Summing the triangular excess \tfrac{\epsilon^2}{2}f'(x_i) above each left rectangle yields \tfrac{\epsilon}{2}\int_a^b f'(x)\,dx = \tfrac{\epsilon}{2}(f(b) - f(a)).

Signed Area and Monotonicity of the Integral

Integral Properties

  • Signed area above and below the axis: Lobes below the horizontal axis (f(\xi_i) < 0) enter Riemann sums with negative height and subtract from the total area.
  • Oriented limits: Integrating from right to left negates slice widths, so \int_b^a f(x)\,dx = -\int_a^b f(x)\,dx.
  • Monotonicity: If f(x) \le g(x) everywhere on [a, b], every tagged sum of f is bounded by that of g, so \int_a^b f(x)\,dx \le \int_a^b g(x)\,dx.

Interval Additivity and the Area-So-Far Function

Accumulated Area

  • Additivity over subintervals: Any partition containing b \in [a, c] splits into partitions of [a, b] and [b, c], giving \int_a^c f\,dx = \int_a^b f\,dx + \int_b^c f\,dx.
  • The area-so-far function F(x): Fix a base point a and treat the upper endpoint x as a variable: F(x) = \int_a^x f(y)\,dy
  • Every integral is a difference of F: By additivity, the area over [x_1, x_2] is simply \int_{x_1}^{x_2} f(x)\,dx = F(x_2) - F(x_1).

The Fundamental Theorem of Calculus (FTC)

Rate of Area Accumulation

  • Theorem (FTC Part 1): Let f be Riemann integrable on [a, b] and define F(x) = \int_a^x f(y)\,dy. At any interior point x \in (a, b) where f is continuous, F is differentiable and \frac{dF}{dx}(x) = f(x)
  • Squeeze proof via monotonicity & additivity: Nudging x \mapsto x+\epsilon (\epsilon > 0) adds a single sliver F(x+\epsilon) - F(x) = \int_x^{x+\epsilon} f(y)\,dy. Setting m_\epsilon = \inf_{[x, x+\epsilon]} f and M_\epsilon = \sup_{[x, x+\epsilon]} f, monotonicity traps the sliver between two rectangles: m_\epsilon \le \frac{F(x+\epsilon) - F(x)}{\epsilon} \le M_\epsilon By continuity of f at x, both m_\epsilon, M_\epsilon \to f(x) as \epsilon \to 0.

The Evaluation Rule and Antiderivative Pitfalls

Computing Integrals

  • Evaluation Rule (FTC Part 2): If f is continuous on [a, b] and G'(x) = f(x) for all x \in [a, b], then \int_a^b f(x)\,dx = G(b) - G(a).
  • Running Example: \int_0^2 \frac{x}{1+x^2}\,dx = [\tfrac{1}{2}\log(1+x^2)]_0^2 = \tfrac{1}{2}\log 5.
  • Singular Antiderivative Trap: Writing \int_{-1}^1 x^{-2}\,dx = [-1/x]_{-1}^1 = -2 is false because -1/x blows up at x = 0 \in [-1, 1].

Verifying the Area-So-Far Function Across [0, 2]

Numerical Check

Comparing the cumulative left-endpoint sum F(x) = \int_0^x \frac{y}{1+y^2}\,dy against G(x) - G(0) = \tfrac{1}{2}\log(1+x^2) across the entire grid [0, 2] confirms that the maximum gap sits at x = 1 (where f(1) = \tfrac{1}{2} peaks) and equals \tfrac{\epsilon}{2}f(1) = \tfrac{\epsilon}{4}:

eps=1e-02  max over x of |F(x) - (G(x) - G(0))| = 2.51e-03
eps=1e-03  max over x of |F(x) - (G(x) - G(0))| = 2.50e-04
eps=1e-04  max over x of |F(x) - (G(x) - G(0))| = 2.50e-05

Improper Integrals and Heavy-Tailed Power Laws

Unbounded Domains & Blow-Ups

  • Power-Law Convergence Thresholds:
    • Unbounded tail \int_1^\infty x^{-s}\,dx = \frac{1}{s-1} converges iff s > 1.
    • Endpoint blow-up \int_0^1 x^{-s}\,dx = \frac{1}{1-s} converges iff s < 1.
  • Heavy-Tailed Moment Existence (p(x) \propto x^{-s}): Normalizable for s > 1, finite mean for s > 2, and finite variance for s > 3.

Truncation Error vs. Discretization Error

Numerical Quadrature Pitfall

Approximating an improper integral \int_0^\infty e^{-x}\,dx = 1 on a finite grid [0, b] with slice width \epsilon = 10^{-3} introduces two independent errors—domain truncation error e^{-b} and slice discretization error \tfrac{\epsilon}{2} = 0.0005:

b=1    left sum=0.632437  exact 1-e^-b=0.632121
b=2    left sum=0.865097  exact 1-e^-b=0.864665
b=5    left sum=0.993759  exact 1-e^-b=0.993262
b=10   left sum=1.000455  exact 1-e^-b=0.999955
b=20   left sum=1.000500  exact 1-e^-b=1.000000

Increasing the cutoff b from 1 to 20 drives the truncation error e^{-b} below 10^{-8}, yet the left sum plateaus at 1.000500: pushing b \to \infty never reduces the discretization bias \tfrac{\epsilon}{2}(f(0) - 0) = 0.0005.

Integration by Parts and Stein’s Identity

Reversing the Product Rule

  • Integration by Parts: \int_a^b u(x)v'(x)\,dx = [u(x)v(x)]_a^b - \int_a^b u'(x)v(x)\,dx.
  • Stein’s Identity for w(z) = e^{-z^2/2} (w' = -zw): Integrating g'(z)w(z) by parts gives \mathbb{E}[Z\,g(Z)] = \mathbb{E}[g'(Z)] for Z \sim \mathcal{N}(0, 1), yielding \mathbb{E}[Z^2] = 1 and \mathbb{E}[Z^4] = 3 without antiderivatives.

Change of Variables (Substitution) in 1D

Reversing the Chain Rule

  • Chain rule read backward: If G' = f, then (G \circ u)'(x) = f(u(x))\,u'(x), so the evaluation rule yields the 1D change-of-variables formula: \int_{u(a)}^{u(b)} f(y)\,dy = \int_a^b f(u(x))\,\frac{du}{dx}\,dx
  • Geometric stretch factor du/dx: A slice of width \epsilon at x maps to an image slice at y = u(x) of width \Delta y \approx u'(x)\epsilon and height f(u(x)).
  • Worked example: In \int_0^1 x e^{-x^2}\,dx, setting y = u(x) = x^2 (dy = 2x\,dx) supplies the derivative factor u'(x) and yields \tfrac{1}{2}\int_0^1 e^{-y}\,dy = \tfrac{1 - e^{-1}}{2} \approx 0.316.

Double Integrals and Volume Under a Surface

Multiple Integrals

  • From intervals to rectangles R = [a, b] \times [c, d]: Tile the base plane with \epsilon \times \epsilon squares of area \epsilon^2, erect a vertical column of height f(x_i, y_j) on each cell, and take the limit as \epsilon \to 0: \iint_R f(x, y)\,dx\,dy = \lim_{\epsilon \to 0} \sum_{i, j} \epsilon^2 f(x_i, y_j)
  • Signed volume: Just as in 1D, regions where f(x, y) > 0 contribute positive volume and regions where f(x, y) < 0 contribute negative volume.
  • 2D Gaussian bell: The surface z = e^{-x^2 - y^2} over [-2, 2]^2 is rotationally symmetric and factors as e^{-x^2}e^{-y^2}.

Fubini’s Theorem and Factoring Product Integrals

Iterated Integration

  • Fubini’s theorem: If f is absolutely integrable on R (\iint_R |f| < \infty), or if f \ge 0 everywhere (Tonelli’s theorem), we can compute the double integral by slicing in either order: \iint_R f(x,y)\,dx\,dy = \int_a^b\!\left(\int_c^d f(x,y)\,dy\right)dx
  • Product integrands split completely: When f(x, y) = g(x)h(y), the inner factor pulls out so \iint_R g(x)h(y)\,dx\,dy = \bigl(\int_a^b g\,dx\bigr)\bigl(\int_c^d h\,dy\bigr).
  • Why \iint |f| < \infty matters: For f(x,y) = \frac{x^2-y^2}{(x^2+y^2)^2} on [0,1]^2, \iint |f| = \infty and the two orders disagree (+\frac{\pi}{4} vs. -\frac{\pi}{4}).

Multivariable Change of Variables & Jacobian Det

Volume Distortion in R^n

  • From 1D stretch |u'(x)| to n-D volume factor |\det \mathbf{J}_{\boldsymbol{\phi}}|: Let \boldsymbol{\phi} : \Omega \to \mathbb{R}^n be a C^1-diffeomorphism (continuously differentiable, injective, with \det \mathbf{J}_{\boldsymbol{\phi}}(\mathbf{x}) \ne 0 everywhere on open \Omega).
  • Change-of-variables theorem: For any absolutely integrable f, \int_{\boldsymbol{\phi}(\Omega)} f(\mathbf{y})\,d\mathbf{y} = \int_{\Omega} f(\boldsymbol{\phi}(\mathbf{x}))\,\bigl|\det \mathbf{J}_{\boldsymbol{\phi}}\bigr|\,d\mathbf{x}
  • Geometric meaning: Locally, \boldsymbol{\phi} acts like its Jacobian matrix \mathbf{J}_{\boldsymbol{\phi}}(\mathbf{x}), mapping an infinitesimal cube of volume d\mathbf{x} to a parallelepiped of volume |\det \mathbf{J}_{\boldsymbol{\phi}}(\mathbf{x})|\,d\mathbf{x}.

Density Transformation in Normalizing Flows

Deep Generative Models

  • Conservation of Probability Mass (\mathbf{Y} = \boldsymbol{\phi}(\mathbf{X})): P(\mathbf{Y} \in \boldsymbol{\phi}(\Omega)) = P(\mathbf{X} \in \Omega) forces p_{\mathbf{Y}}(\boldsymbol{\phi}(\mathbf{x}))\,|\det\mathbf{J}_{\boldsymbol{\phi}}(\mathbf{x})| = p_{\mathbf{X}}(\mathbf{x}).
  • Exact Log-Likelihood Formula: \log p_{\mathbf{Y}}(\boldsymbol{\phi}(\mathbf{x})) = \log p_{\mathbf{X}}(\mathbf{x}) - \log|\det\mathbf{J}_{\boldsymbol{\phi}}(\mathbf{x})|.
  • Architectural Impact: Triangular/coupling flow layers evaluate \log|\det\mathbf{J}_{\boldsymbol{\phi}}| in O(n) instead of O(n^3).

Evaluating the Gaussian Integral via Polar Coords

The √(2π) Normalizer

Because e^{-x^2} lacks an elementary antiderivative, we evaluate I = \int_{-\infty}^\infty e^{-x^2}\,dx by squaring it into a 2D integral and switching to polar coordinates \boldsymbol{\phi}(r, \theta) = (r\cos\theta, r\sin\theta):

  • 1. Square & factor (Fubini): I^2 = \iint_{\mathbb{R}^2} e^{-x^2-y^2}\,dx\,dy.
  • 2. Polar Jacobian determinant: A polar cell with sides dr and r\,d\theta has volume factor |\det \mathbf{J}_{\boldsymbol{\phi}}(r, \theta)| = r(\cos^2\theta + \sin^2\theta) = r.
  • 3. Integrate with the supplied r factor: I^2 = \int_0^{2\pi}\!\!\int_0^\infty r e^{-r^2}\,dr\,d\theta = 2\pi \cdot \frac{1}{2} = \pi Taking the square root gives I = \sqrt{\pi} \approx 1.7725.

Exhausting the Plane & Standard Normal Probabilities

Code Verification

Substituting z = \sqrt{2}\,x (dz = \sqrt{2}\,dx) into \int_{-\infty}^\infty e^{-x^2}\,dx = \sqrt{\pi} gives the standard normal normalizer \int_{-\infty}^\infty e^{-z^2/2}\,dz = \sqrt{2\pi} \approx 2.5066, and P(-1 \le Z \le 1) = \operatorname{erf}(1/\sqrt{2}) \approx 0.6827:

b=1  grid sum=2.230949  pi*erf(b)^2=2.230985  pi=3.141593
b=2  grid sum=3.112266  pi*erf(b)^2=3.112270  pi=3.141593
b=3  grid sum=3.141454  pi*erf(b)^2=3.141454  pi=3.141593
b=6  grid sum=3.141593  pi*erf(b)^2=3.141593  pi=3.141593

Why do the 2D grid sums on [-b, b]^2 at \epsilon = 0.01 match \pi\operatorname{erf}(b)^2 to 6 digits even though the left rule is normally O(\epsilon)? For an even integrand on a symmetric interval [-b, b], the first-order bias \tfrac{\epsilon}{2}(f(-b) - f(b)) cancels identically to zero.

Concept Check: Integral Calculus

Review & Practice

Test your understanding of the Fundamental Theorem, change of variables, and Gaussian moments:

  1. GELU & ReLU as integrals: Let \phi(t) = \frac{1}{\sqrt{2\pi}}e^{-t^2/2} be the standard normal density and \Phi(x) = \int_{-\infty}^x \phi(t)\,dt its CDF. Using the Fundamental Theorem and product rule, what is the derivative of the \mathrm{GELU} activation \mathrm{GELU}(x) = x\Phi(x)?
  2. Swapping integration order (Fubini): The inner integral of \int_0^1 \bigl(\int_y^1 e^{-x^2}\,dx\bigr)\,dy has no elementary antiderivative. Reverse the order of integration over the triangle 0 \le y \le x \le 1 and evaluate it in closed form.
  3. Multivariate Gaussian normalizer: For a d-dimensional covariance matrix \boldsymbol{\Sigma} = \mathbf{L}\mathbf{L}^\top (\det \mathbf{L} > 0), use \mathbf{x} = \mathbf{L}\mathbf{z} to evaluate \int_{\mathbb{R}^d} \exp(-\tfrac{1}{2}\mathbf{x}^\top\boldsymbol{\Sigma}^{-1}\mathbf{x})\,d\mathbf{x}.

Answers:

  1. By the Fundamental Theorem, \Phi'(x) = \phi(x), so the product rule gives \mathrm{GELU}'(x) = \Phi(x) + x\phi(x).
  2. The triangle 0 \le y \le x \le 1 slices vertically as 0 \le x \le 1 and 0 \le y \le x: \int_0^1 \bigl(\int_0^x e^{-x^2}\,dy\bigr)\,dx = \int_0^1 x e^{-x^2}\,dx = \tfrac{1}{2}(1 - e^{-1}), as the inner y-integral supplies the exact missing factor x.
  3. Under \mathbf{x} = \mathbf{L}\mathbf{z}, |\det \mathbf{J}| = |\det \mathbf{L}| = (\det \boldsymbol{\Sigma})^{1/2} and \mathbf{x}^\top\boldsymbol{\Sigma}^{-1}\mathbf{x} = \|\mathbf{z}\|_2^2, so Fubini factors the integral into (\det \boldsymbol{\Sigma})^{1/2}\prod_{i=1}^d \int_{-\infty}^\infty e^{-z_i^2/2}\,dz_i = (2\pi)^{d/2}(\det \boldsymbol{\Sigma})^{1/2}.

Summary: Integral Calculus Toolkit

Key Takeaways

Theorem / Technique Mathematical Formula Role in Probability & Deep Learning
Definite Integral & Bias \int_a^b f(x)\,dx = \lim_{\|P\|\to 0}\sum_i f(\xi_i)\Delta x_i Left-sum bias is \tfrac{\epsilon}{2}(f(a)-f(b)); vanishes on symmetric bells
Fundamental Theorem (FTC) \frac{d}{dx}\int_a^x f(y)\,dy = f(x); \int_a^b f(x)\,dx = G(b)-G(a) Replaces slice summation with antidifferentiation (G'=f on [a,b])
Improper Power-Law Test \int_1^\infty x^{-s}dx = \frac{1}{s-1} (s>1); \int_0^1 x^{-s}dx = \frac{1}{1-s} (s<1) Governs when heavy-tailed distributions have finite mean/variance
Integration by Parts & Stein \int_a^b u v'\,dx = [uv]_a^b - \int_a^b u'v\,dx; \mathbb{E}[g'(Z)] = \mathbb{E}[Z g(Z)] Shifts derivatives off unknown densities (score matching, moments)
Fubini’s Theorem \iint_R f\,dx\,dy = \int_a^b(\int_c^d f\,dy)dx (\iint\|f\|<\infty or f\ge 0) Marginalizes joint densities; factors independent product integrals
n-D Change of Variables \int_{\boldsymbol{\phi}(\Omega)} f(\mathbf{y})\,d\mathbf{y} = \int_\Omega f(\boldsymbol{\phi}(\mathbf{x}))\|\det\mathbf{J}_{\boldsymbol{\phi}}(\mathbf{x})\|\,d\mathbf{x} Adjusts densities by \log\|\det\mathbf{J}_{\boldsymbol{\phi}}\| in normalizing flows
Gaussian Integral \int_{-\infty}^\infty e^{-x^2}dx = \sqrt{\pi} \implies \int_{-\infty}^\infty e^{-z^2/2}dz = \sqrt{2\pi} Proved via 2D polar coordinates (\|\det\mathbf{J}\| = r); normalizes \mathcal{N}(0,1)