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
Motivation
The Definite Integral
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)).
Integral Properties
Accumulated Area
Rate of Area Accumulation
Computing Integrals
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
Unbounded Domains & Blow-Ups
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.
Reversing the Product Rule
Reversing the Chain Rule
Multiple Integrals
Iterated Integration
Volume Distortion in R^n
Deep Generative Models
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):
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.
Review & Practice
Test your understanding of the Fundamental Theorem, change of variables, and Gaussian moments:
Answers:
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) |