Product Rule

The product rule tells you how to differentiate a product of two functions. It shows up whenever a model multiplies quantities that both change — for example a time-varying rate times a shrinking susceptible pool. The incidence term βSI/N\beta S I / N in an epidemic model is a product of the changing susceptible and infected counts, so differentiating it — say to build the Jacobian for a stability analysis — requires the product rule.

The rule#

ddx[f(x) g(x)]=f′(x) g(x)+f(x) g′(x)\frac{d}{dx}\big[f(x)\,g(x)\big] = f'(x)\,g(x) + f(x)\,g'(x)

A common mistake is to guess f′g′f' g'; the derivative of a product is not the product of derivatives.

Intuition#

Think of a rectangle with width ff and height gg, so its area is fgf g. Nudge xx a little: the width grows by about f′ dxf'\,dx and the height by about g′ dxg'\,dx. The area gains two thin strips — one of size f′ gf'\,g (from the wider width) and one of size f g′f\,g' (from the taller height). The tiny corner f′g′(dx)2f' g' (dx)^2 is negligible, leaving f′g+fg′f' g + f g'.

Worked example: x2exx^2 e^x#

Let f(x)=x2f(x) = x^2 and g(x)=exg(x) = e^x, so f′(x)=2xf'(x) = 2x and g′(x)=exg'(x) = e^x:

ddx[x2ex]=(2x) ex+x2 ex=ex (2x+x2)=xex(x+2).\begin{aligned} \frac{d}{dx}\big[x^2 e^x\big] &= (2x)\,e^x + x^2\,e^x \\ &= e^x\,(2x + x^2) = x e^x (x + 2) . \end{aligned}

At x=1x = 1 this equals e(1)(3)=3e≈8.155e(1)(3) = 3e \approx 8.155.

Computing it#

R#

R
# Symbolic
D(expression(x^2 * exp(x)), "x")
#   2 * x * exp(x) + x^2 * exp(x)

# Numeric check at x = 1
library(numDeriv)
grad(function(x) x^2 * exp(x), 1)   # 8.15485  == 3*e
3 * exp(1)                          # 8.154845

Python#

Python
import sympy as sp
x = sp.symbols("x")
sp.diff(x**2 * sp.exp(x), x)        # x**2*exp(x) + 2*x*exp(x)

# Numeric check at x = 1
import numpy as np
f = lambda x: x**2 * np.exp(x)
h = 1e-6
(f(1 + h) - f(1 - h)) / (2 * h)     # ~8.1548  == 3*e

Julia#

Julia
using Symbolics
@variables x
Symbolics.derivative(x^2 * exp(x), x)   # 2x*exp(x) + (x^2)*exp(x)

using ForwardDiff
ForwardDiff.derivative(x -> x^2 * exp(x), 1.0)   # 8.15485  == 3e

Why it matters for statistics#

Likelihoods and moment calculations are full of products — a density times a weight, a rate times an exposure, or x f(x)x\,f(x) inside an expected value. The product rule (together with the chain rule) is what lets you differentiate these expressions to derive estimators and their variances.