Improper Integrals
The definite integral was defined on a closed, bounded interval for a bounded function. Probability, physics and signal processing constantly need more: integrals over , or of functions that blow up at an endpoint. Improper integrals extend the definition with a limit. This lesson defines both types, proves the -integral test, gives the comparison test, evaluates the Gaussian integral, and shows why every probability density is an improper integral that must equal 1.
Type 1: infinite intervals
Definition. If exists for every , then
provided the limit exists (as a finite number). Then the integral converges; otherwise it diverges. Similarly for . For split at any point : it converges only if both halves converge.
The last rule matters: for every , but diverges, so diverges. The symmetric limit is called the Cauchy principal value, and it is a different (weaker) notion.
Type 2: discontinuous integrands
Definition. If is continuous on and unbounded near , then , if the limit exists. Similarly at the left end, and an interior discontinuity at splits the integral into two, both of which must converge.
This is the fix for the false computation from the Fundamental Theorem lesson: , so the integral diverges.
The p-integrals
Theorem. converges if (to ) and diverges if . converges if (to ) and diverges if .
Proof (first part). For , . If , and the limit is . If , . For , . The second part is the same computation with . ∎
is the borderline in both directions: just a little faster decay () at infinity converges, and a little slower blow-up () at zero converges.
import sympy as sp
x, t = sp.symbols('x t', positive=True)
for p in [sp.Rational(1, 2), 1, sp.Rational(3, 2), 2]:
tail = sp.limit(sp.integrate(x**-p, (x, 1, t)), t, sp.oo)
head = sp.limit(sp.integrate(x**-p, (x, t, 1)), t, 0, '+')
print(f"p = {str(p):4}: ∫_1^∞ = {str(tail):4} ∫_0^1 = {head}")p = 1/2 : ∫_1^∞ = oo ∫_0^1 = 2
p = 1 : ∫_1^∞ = oo ∫_0^1 = oo
p = 3/2 : ∫_1^∞ = 2 ∫_0^1 = oo
p = 2 : ∫_1^∞ = 1 ∫_0^1 = oo
The comparison test
Comparison Theorem. Let be continuous with for . If converges, so does . If diverges, so does .
(The proof uses that is increasing and bounded above, hence has a limit: the completeness of the reals again.) It decides convergence without computing the value:
The Gaussian integral
The bell curve has no elementary antiderivative, yet its improper integral has an exact value:
Proof sketch. Call the integral . Then over the whole plane. In polar coordinates, and the area element is , so . The extra factor is what makes the inner integral elementary (Double Integrals in Polar Coordinates makes this rigorous).
import math
import sympy as sp
from scipy.integrate import quad
x = sp.symbols('x', real=True)
print("SymPy:", sp.integrate(sp.exp(-x**2), (x, -sp.oo, sp.oo)))
val, err = quad(lambda s: math.exp(-s * s), -math.inf, math.inf)
print(f"quad: {val:.15f} (estimated error {err:.1e})")
print(f"√π: {math.sqrt(math.pi):.15f}")
# a finite window already captures almost everything: the tails are tiny
print(f"∫_-6^6 e^(-x^2) dx = {quad(lambda s: math.exp(-s * s), -6, 6)[0]:.15f}")SymPy: sqrt(pi)
quad: 1.772453850905516 (estimated error 1.4e-08)
√π: 1.772453850905516
∫_-6^6 e^(-x^2) dx = 1.772453850905516
Probability densities
A probability density function (pdf) satisfies and ; probabilities are areas, , and the mean is the improper integral , when it converges. Rescaling the Gaussian integral gives the normal density
whose constant is exactly what makes the total area 1. The exponential density () models waiting times; the Cauchy density is a warning: it integrates to 1, but , so it has no mean, and averages of Cauchy samples never settle down.
import numpy as np
from scipy.integrate import quad
normal = lambda x, mu=1.0, s=2.0: np.exp(-(x - mu)**2 / (2 * s * s)) / (s * np.sqrt(2 * np.pi))
expo = lambda x, lam=0.5: lam * np.exp(-lam * x)
cauchy = lambda x: 1 / (np.pi * (1 + x * x))
print("normal: total =", round(quad(normal, -np.inf, np.inf)[0], 10), " mean =", round(quad(lambda x: x * normal(x), -np.inf, np.inf)[0], 10))
print("expo: total =", round(quad(expo, 0, np.inf)[0], 10), " mean =", round(quad(lambda x: x * expo(x), 0, np.inf)[0], 10))
print("cauchy: total =", round(quad(cauchy, -np.inf, np.inf)[0], 10), " but ∫_0^t x f(x) dx grows like ln t:")
for t in (1e2, 1e4, 1e6):
print(f" t = {t:.0e}: {np.log1p(t * t) / (2 * np.pi):.4f}")
rng = np.random.default_rng(11)
for n in (10**3, 10**5, 10**6):
print(f"mean of {n:>7} samples: normal {float(rng.normal(1, 2, n).mean()):+.4f} cauchy {float(rng.standard_cauchy(n).mean()):+.4f}")normal: total = 1.0 mean = 1.0
expo: total = 1.0 mean = 2.0
cauchy: total = 1.0 but ∫_0^t x f(x) dx grows like ln t:
t = 1e+02: 1.4659
t = 1e+04: 2.9317
t = 1e+06: 4.3976
mean of 1000 samples: normal +1.0327 cauchy +1.5179
mean of 100000 samples: normal +0.9974 cauchy +3.2799
mean of 1000000 samples: normal +1.0040 cauchy -1.7165
The normal sample means settle at the true mean 1; the Cauchy ones keep jumping, however many samples you take.
In software
- Machine learning. Likelihoods, the normalising constants of densities, KL divergences and expected losses are improper integrals. Diffusion models and VAEs are built on Gaussians precisely because their integrals have closed forms.
- Heavy tails in systems. Latencies, file sizes and request rates often have power-law tails like . The -integral test predicts when the mean ( for a density tail ) or variance () exists; when they do not, averages are unstable and percentiles are the right metric.
- Numerical tails.
quadhandles infinite limits by substituting to map to . Rapidly decaying tails can also be cut at a finite point, as the example shows.
Exercises
Exercise 1: Type 1
Evaluate .
Answer
Solution
By parts, (exponentials beat powers). This is the mean of the exponential distribution with .
Exercise 2: Type 2
Evaluate .
Answer
Solution
as .
import sympy as sp
x = sp.symbols('x')
print(sp.integrate(1 / sp.sqrt(4 - x), (x, 0, 4)))4
Exercise 3: Comparison
Does converge?
Answer
Yes.
Solution
For , , and converges ().
Exercise 4: Normalise a density
Find so that is a probability density on .
Answer
.
Solution
, so (the Cauchy density).
Exercise 5: A Gaussian moment
Using , compute .
Answer
.
Solution
By parts with , , : the boundary term vanishes at , leaving .
import sympy as sp
x = sp.symbols('x', real=True)
print(sp.integrate(x**2 * sp.exp(-x**2), (x, -sp.oo, sp.oo)))sqrt(pi)/2
Test yourself
For which does converge?
- All
Why does diverge even though for every ?
- It does not diverge
- Both halves must converge separately, and
- is odd
- Because of the Cauchy principal value
Why does the Cauchy distribution have no mean?
- over diverges
- Its total area is not 1
- It is not symmetric
- It has no density