BRAGA

02 Writing02-02 Notes02-02-257

Product Rule

Published 16 Apr 20269 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
ddx[uv]=udvdx+vdudx=u′v+uv′\frac{d}{dx}[uv] = u\frac{dv}{dx} + v\frac{du}{dx} = u'v + uv'
(1)

Deep Breakdown

What it tells me (Situation Awareness): The product rule gives the derivative of a product of two functions. Unlike the sum rule (where derivatives simply add), the product of two functions produces two terms via the product rule: d/dx[uv]=u′v+uv′d/dx[uv] = u'v + uv'. Mnemonic: "first times derivative of second, plus second times derivative of first." In DA/DS: the product rule is applied in deriving the Mish activation gradient (Mish(x)=x⋅tanh⁡(softplus(x))\text{Mish}(x) = x \cdot \tanh(\text{softplus}(x)), where the product x⋅f(x)x \cdot f(x) triggers the product rule); in the entropy derivative d/dp(−plog⁡p)=−(log⁡p+1)d/dp(-p\log p) = -(\log p + 1) (product p⋅log⁡pp \cdot \log p); in computing gradients through attention scores (product of query and key vectors scaled by a function); and in deriving integration by parts (∫u dv=uv−∫v du\int u \, dv = uv - \int v \, du).

Flowchart

h(x) = u(x) * v(x)First principles:[u(x+h)*v(x+h) -u(x)*v(x)] / hAdd andsubtractu(x)*v(x+h):[u(x+h)*v(x+h) -u(x)*v(x+h) +u(x)*v(x+h) -u(x)*v(x)] / h=v(x+h)*[u(x+h)-u(x)]/h +u(x)*[v(x+h)-v(x)]/hlim: v(x)*u'(x) +u(x)*v'(x)d/dx[uv] = u'v +uv'Memory aid:first*deriv(second) +second*deriv(first)

Real-world use

Where it is used

Mish activation gradient: Mish(x)=x⋅tanh⁡(softplus(x))\text{Mish}(x) = x \cdot \tanh(\text{softplus}(x)), product rule gives Mish′(x)=tanh⁡(softplus(x))+x⋅sech2(softplus(x))⋅σ(x)\text{Mish}'(x) = \tanh(\text{softplus}(x)) + x \cdot \text{sech}^2(\text{softplus}(x)) \cdot \sigma(x); entropy derivative: d/dp(−plog⁡p)=−log⁡p−1d/dp(-p\log p) = -\log p - 1; attention score: d/dx(qk/d)d/dx(qk/\sqrt{d}); logarithmic differentiation uses product rule implicitly; integration by parts is product rule reversed.

Problem it solves

When two non-constant functions are multiplied together, the derivative cannot simply be the product of the individual derivatives: (uv)′≠u′v′(uv)' \ne u'v' in general. The product rule provides the correct formula.

How it solves the problem

Add and subtract the bridge term u(x)v(x+h)u(x)v(x+h) in the numerator: [u(x+h)v(x+h)−u(x)v(x)]=[u(x+h)−u(x)]v(x+h)+u(x)[v(x+h)−v(x)][u(x+h)v(x+h) - u(x)v(x)] = [u(x+h)-u(x)]v(x+h) + u(x)[v(x+h)-v(x)]. Dividing by hh and taking the limit: first term gives u′(x)⋅v(x)u'(x) \cdot v(x) (since v(x+h)→v(x)v(x+h) \to v(x) as h→0h \to 0); second term gives u(x)⋅v′(x)u(x) \cdot v'(x).

Example in a business

A DS derives the gradient of the entropy term −plog⁡p-p \log p used in a decision tree impurity criterion. Let u=pu = p, v=log⁡pv = \log p: d/dp(plog⁡p)=(log⁡p)(1)+p(1/p)=log⁡p+1d/dp(p \log p) = (\log p)(1) + p(1/p) = \log p + 1. So d/dp(−plog⁡p)=−log⁡p−1d/dp(-p \log p) = -\log p - 1. This is used to compute the optimal split threshold for information gain maximization.

The formula, piece by piece

  • u=u(x)u = u(x): first function; differentiable
  • v=v(x)v = v(x): second function; differentiable
  • u′=du/dxu' = du/dx: derivative of the first function
  • v′=dv/dxv' = dv/dx: derivative of the second function
  • u′vu'v: "derivative of first, times second"
  • uv′uv': "first, times derivative of second"
  • (uv)′=u′v+uv′(uv)' = u'v + uv': the full product rule
  • Extension (three functions): (uvw)′=u′vw+uv′w+uvw′(uvw)' = u'vw + uv'w + uvw'
  • Common mistake: (uv)′≠u′v′(uv)' \ne u'v'. Always two terms, not one.

The formula in words

"The derivative of a product has two parts: put a prime on the first factor and leave the second alone, then leave the first alone and put a prime on the second. Add the two parts. This is because both factors are changing, and each factor's change contributes to the overall rate of change of the product." Tiny trace: h(x)=x2sin⁡xh(x) = x^2 \sin x. u=x2u = x^2, v=sin⁡xv = \sin x, u′=2xu' = 2x, v′=cos⁡xv' = \cos x. (x2sin⁡x)′=2xsin⁡x+x2cos⁡x(x^2 \sin x)' = 2x \sin x + x^2 \cos x. At x=π/2x = \pi/2: =2(π/2)(1)+(π/2)2(0)=π= 2(\pi/2)(1) + (\pi/2)^2(0) = \pi.

Critical Concepts

  • Two terms, not one: (uv)′=u′v+uv′(uv)' = u'v + uv'. A common beginner error is writing (uv)′=u′v′(uv)' = u'v'. Always check: there must be two terms.
  • Mish activation: Mish(x)=x⋅tanh⁡(softplus(x))\text{Mish}(x) = x \cdot \tanh(\text{softplus}(x)). Product rule: Mish′(x)=tanh⁡(softplus(x))+x⋅sech2(softplus(x))⋅σ(x)\text{Mish}'(x) = \tanh(\text{softplus}(x)) + x \cdot \text{sech}^2(\text{softplus}(x)) \cdot \sigma(x), where σ(x)=d/dx(softplus(x))=1/(1+e−x)\sigma(x) = d/dx(\text{softplus}(x)) = 1/(1+e^{-x}).
  • Integration by parts: ∫u dv=uv−∫v du\int u \, dv = uv - \int v \, du is the product rule rearranged and integrated: ∫(uv)′dx=uv=∫u′v dx+∫uv′ dx\int (uv)' dx = uv = \int u'v \, dx + \int uv' \, dx. Rearranging: ∫uv′ dx=uv−∫u′v dx\int uv' \, dx = uv - \int u'v \, dx. Used in computing moments, computing the expectation of complex distributions.
  • Entropy derivative: d/dp(plog⁡2p)=log⁡2p+1/ln⁡2d/dp(p\log_2 p) = \log_2 p + 1/\ln 2 (product rule with u=pu=p, v=log⁡2pv=\log_2 p). This appears in gradient boosting impurity and information gain.
  • Triple product rule: (uvw)′=u′vw+uv′w+uvw′(uvw)' = u'vw + uv'w + uvw'.

Three terms. In general, for nn factors, the product rule gives nn terms, each with exactly one factor differentiated.

Step-by-step calculation (tiny example)

Differentiate f(x)=x3⋅exf(x) = x^3 \cdot e^x using the product rule.

Step 1: Identify uu and vv. u=x3u = x^3, v=exv = e^x.

Step 2: Compute u′u' and v′v'. u′=3x2u' = 3x^2, v′=exv' = e^x.

Step 3: Apply product rule. f′(x)=u′v+uv′=3x2ex+x3ex=ex(3x2+x3)=x2ex(3+x)f'(x) = u'v + uv' = 3x^2 e^x + x^3 e^x = e^x(3x^2 + x^3) = x^2 e^x(3 + x). Verify at x=1x = 1: f′(1)=1⋅e⋅4=4e≈10.873f'(1) = 1 \cdot e \cdot 4 = 4e \approx 10.873. Numerical: (23e2−03e0)/2≈(8⋅7.389−0)/2≈(2^3 e^2 - 0^3 e^0) / 2 \approx (8 \cdot 7.389 - 0)/2 \approx wait, use h=0.001h = 0.001: f(1.001)=1.0013⋅e1.001≈1.003⋅2.7205=2.7286f(1.001) = 1.001^3 \cdot e^{1.001} \approx 1.003 \cdot 2.7205 = 2.7286. f(0.999)=0.9993⋅e0.999≈0.997⋅2.7151=2.7069f(0.999) = 0.999^3 \cdot e^{0.999} \approx 0.997 \cdot 2.7151 = 2.7069. (2.7286−2.7069)/0.002≈10.85≈4e(2.7286 - 2.7069)/0.002 \approx 10.85 \approx 4e. ✓\checkmark

Code examples

Beginner example (Python)

python
import numpy as np   # Product rule: d/dx[u*v] = u'v + uv'

def product_rule_deriv( u: float, v: float, u_prime: float, v_prime: float ) -> float:
    """d/dx[u*v] = u'*v + u*v'."""
    return u_prime * v + u * v_prime   # Example 1: d/dx[x^3 * e^x] at
x=1
x = 1.0
u, v = x**3, np.exp(x)
u_prime, v_prime = 3*x**2, np.exp(x)
analytic = product_rule_deriv(u, v, u_prime, v_prime)
print(f"d/dx[x^3 * e^x] at x=1: {analytic:.6f} (= 4e = {4*np.e:.6f})")   # Verify numerically
h = 1e-7
f = lambda x: x**3 * np.exp(x)
numerical = (f(x + h) - f(x - h)) / (2 * h)
print(f"Numerical check: {numerical:.6f}")   # Example 2: d/dp[-p*log(p)] = entropy gradient
print("\nEntropy gradient: d/dp[-p*log(p)] = -(log(p) + 1)")
for p in [0.1, 0.25, 0.5, 0.75, 0.9]:
    # Product rule: u=p, v=-log(p); u'=1, v'=-1/p
    u, v = p, -np.log(p)
u_prime, v_prime = 1.0, -1.0/p
grad = product_rule_deriv(u, v, u_prime, v_prime)
analytic_formula = -(np.log(p) + 1)
print(f" p={p:.2f}: product rule={grad:.4f}, formula -(log(p)+1)={analytic_formula:.4f}")   # Mish activation and its product-rule gradient

def mish(x: np.ndarray) -> np.ndarray:
    """Mish(x) = x * tanh(softplus(x))."""
    return x * np.tanh(np.log1p(np.exp(x)))

def mish_gradient(x: np.ndarray) -> np.ndarray:
    """d/dx[Mish(x)] via product rule: u=x, v=tanh(softplus(x)). = tanh(softplus(x)) + x * sech^2(softplus(x)) * sigmoid(x). """
    sp = np.log1p(np.exp(x))
    tanh_sp = np.tanh(sp)
    sech2_sp = 1.0 - tanh_sp**2
    sig_x = 1.0 / (1.0 + np.exp(-x))
    return tanh_sp + x * sech2_sp * sig_x
x_test = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
h = 1e-7
print("\nMish activation gradient (product rule):")
print(f"{'x':>6} {'Analytic':>12} {'Numerical':>12}")
for xi in x_test:
    analytic_g = mish_gradient(np.array([xi]))[0]
numerical_g = (mish(np.array([xi + h]))[0] - mish(np.array([xi - h]))[0]) / (2 * h)
print(f"{xi:>6.1f} {analytic_g:>12.6f} {numerical_g:>12.6f}")

Beginner example (R)

r
# Product rule: d/dx[u*v] = u'*v + u*v'
product_rule <- function(u, v, u_prime, v_prime) u_prime * v + u * v_prime  # d/dx[x^3 * exp(x)] at
x=1
x <- 1.0
cat(sprintf("d/dx[x^3*exp(x)] at x=1: %.6f (= 4e = %.6f)\n", product_rule(x^3, exp(x), 3*x^2, exp(x)), 4*exp(1)))  # Entropy gradient: d/dp[-p*log(p)] = -(log(p)+1)
cat("\nEntropy gradient d/dp[-p*log(p)]:\n")
for (p in c(0.1, 0.25, 0.5, 0.75, 0.9)) { grad <- product_rule(p, -log(p), 1, -1/p)
  cat(sprintf(" p=%.2f: %.4f\n", p, grad))
}

Professional example (Python)

python
import numpy as np
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
np.random.seed(42)   # -- Product rule: Mish activation forward and backward pass --
# Use case: a DS implements Mish from scratch and verifies its gradient
# using the product rule. Mish(x) = x * tanh(softplus(x)).

def softplus(x: np.ndarray) -> np.ndarray:
    """log(1 + e^x), numerically stable."""
    return np.log1p(np.exp(np.clip(x, -500, 500)))

def sigmoid(x: np.ndarray) -> np.ndarray:
    """Numerically stable sigmoid."""
    return np.where(x >= 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x)))

def mish_forward(x: np.ndarray) -> np.ndarray:
    """Mish(x) = x * tanh(softplus(x)). Product of u=x and v=tanh(softplus(x))."""
    return x * np.tanh(softplus(x))

def mish_backward(x: np.ndarray) -> np.ndarray:
    """d/dx[Mish(x)] via product rule: d/dx[u*v] = u'v + uv'. u = x, u' = 1 v = tanh(softplus(x)), v' = sech^2(softplus(x)) * sigmoid(x) Result: tanh(softplus(x)) + x * sech^2(softplus(x)) * sigmoid(x) """
    sp = softplus(x)
    tanh_sp = np.tanh(sp)
    sech2_sp = 1.0 - tanh_sp**2   # d/d(sp)[tanh(sp)] = sech^2(sp) = 1 - tanh^2(sp)
    dsig = sigmoid(x)   # d/dx[softplus(x)] =
    sigmoid(x)   # Product rule: u'*v + u*v'
    return tanh_sp * 1.0 + x * sech2_sp * dsig   # Verify gradient against finite differences
x_range = np.linspace(-4, 4, 200)
h = 1e-5
grad_analytic = mish_backward(x_range)
grad_numerical = (mish_forward(x_range + h) - mish_forward(x_range - h)) / (2 * h)
max_error = np.max(np.abs(grad_analytic - grad_numerical))
print("Mish Activation Gradient: Product Rule d/dx[x * tanh(softplus(x))]")
print(f"\nMax gradient error (analytic vs numerical): {max_error:.2e}")
print(f"Gradient check: {'PASSED' if max_error < 1e-4 else 'FAILED'}")   # Show gradient values at key points
print(f"\n{'x':>6} {'Mish(x)':>10} {'Mish\'(x)':>12} {'Numerical':>12}")
for xi in [-3.0, -1.0, 0.0, 1.0, 2.0, 3.0]:
    mish_val = mish_forward(np.array([xi]))[0]
grad_val = mish_backward(np.array([xi]))[0]
num_val = grad_numerical[np.argmin(np.abs(x_range - xi))]
print(f"{xi:>6.1f} {mish_val:>10.4f} {grad_val:>12.6f} {num_val:>12.6f}")
print(f"\nKey: Mish gradient uses product rule on u=x and v=tanh(softplus(x)).")
print(f" u'=1 (constant), v'=sech^2(softplus)*sigmoid (chain rule on v).")

Source: 1300 p. 218

#calculus#differential-calculus#definition-and-properties-of-the-derivative

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close