Integration by Parts
Integration by parts is the reverse of the product rule. It turns an integral of a product into an easier one, and it is the standard way to compute the mean of the exponential and gamma distributions. In survival analysis it yields a clean fact: the mean waiting time is the area under the survival curve, — for example the mean time to infection or to pathogen clearance.
The formula#
Starting from the product rule and integrating both sides, then rearranging, gives
Here you split the integrand into a part (to differentiate) and a part (to integrate): compute and , then apply the formula and hope the new integral is simpler.
Choosing and : LIATE#
A useful priority for picking (differentiate first what comes earliest):
- Logarithmic —
- Inverse trig —
- Algebraic —
- Trigonometric —
- Exponential —
Whatever is left becomes . The goal is for to be easier than the original.
Worked example#
Compute . By LIATE, the algebraic factor is and is :
Apply the formula:
Definite version over :
Computing it#
R#
# Numeric check: integral of x*exp(-x) from 0 to Inf equals 1
integrate(function(x) x * exp(-x), 0, Inf)$value # 1
Python#
import sympy as sp
from scipy.integrate import quad
import numpy as np
x = sp.symbols("x")
print(sp.integrate(x*sp.exp(-x), x)) # -x*exp(-x) - exp(-x)
print(sp.integrate(x*sp.exp(-x), (x, 0, sp.oo))) # 1
val, _ = quad(lambda t: t*np.exp(-t), 0, np.inf)
print(val) # 1.0000000000000002
(-x - 1)*exp(-x)
1
0.9999999999999998
Julia#
using Symbolics, QuadGK
@variables x
# Numeric verification
println(quadgk(t -> t*exp(-t), 0, Inf)[1]) # 1.0
# Symbolic check: d/dx[-(x+1)e^{-x}] = x e^{-x}
D = Differential(x)
expand_derivatives(D(-(x+1)*exp(-x))) # x*exp(-x)
Why it matters for statistics#
The exponential distribution with rate has density , and its mean is
which is exactly the integration-by-parts computation above (with a rescaling). The same technique produces the moments of the gamma distribution and appears whenever you integrate a polynomial against an exponential kernel.