Math for Causal Inference

Day 23. Derivatives, Partial Derivatives & Taylor Series

Rates of change, marginal effects and elasticities, and local polynomial approximations of any smooth function.

Video coming soon
Learning outcomes
  • ●Differentiate with the product, quotient and chain rules and compute partial derivatives.
  • ●Compute marginal effects and elasticities in linear, log and non-linear models.
  • ●Build first- and second-order Taylor approximations in one and several variables.
  • ●Explain the log-point approximation and when it breaks down.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

The derivative

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0}\frac{f(x + h) - f(x)}{h}

is the instantaneous rate of change: the slope of the tangent line. Rules you need:

RuleFormula
Power(xn)′=nxn−1(x^n)' = nx^{n-1}
Exponential, log(ex)′=ex(e^{x})' = e^{x}, (ln⁡x)′=1/x(\ln x)' = 1/x
Product(fg)′=f′g+fg′(fg)' = f'g + fg'
Quotient(f/g)′=(f′g−fg′)/g2(f/g)' = (f'g - fg')/g^2
Chain(f(g(x)))′=f′(g(x)) g′(x)(f(g(x)))' = f'(g(x))\,g'(x)

Partial derivatives and the gradient

For f(x1,…,xk)f(x_1, \dots, x_k), the partial derivative ∂f/∂xj\partial f/\partial x_j holds every other variable fixed: ceteris paribus in symbols. The gradient ∇f\nabla f stacks the partials; the Hessian HH collects the second partials ∂2f/∂xj∂xℓ\partial^2 f/\partial x_j\partial x_\ell (symmetric for smooth ff).

Marginal effects and elasticities

  • Marginal effect: ∂E[y∣x]/∂xj\partial E[y \mid x]/\partial x_j. In a linear model it is βj\beta_j; with interactions or non-linear links it depends on xx.
  • Elasticity: ε=∂y∂xxy=∂ln⁡y∂ln⁡x\varepsilon = \frac{\partial y}{\partial x}\frac{x}{y} = \frac{\partial\ln y}{\partial\ln x}.
  • Semi-elasticity: ∂ln⁡y/∂x\partial\ln y/\partial x, the coefficient in a log-level regression.

Taylor series

Near a point aa,

f(x)≈f(a)+f′(a)(x−a)+12f′′(a)(x−a)2,f(x) \approx f(a) + f'(a)(x - a) + \tfrac12 f''(a)(x - a)^2,

and in several variables

f(x)≈f(a)+∇f(a)⊤(x−a)+12(x−a)⊤H(a)(x−a).f(x) \approx f(a) + \nabla f(a)^{\top}(x - a) + \tfrac12(x - a)^{\top}H(a)(x - a).

The first-order term is the delta method (Day 12); the second-order term is the curvature that Newton's method (Day 25) and Fisher information (Day 11) use. The famous log-point approximation is the first-order expansion ln⁡(1+x)≈x\ln(1 + x) \approx x, good when xx is small.

Part 1 · Warm-up

Exercise 1 · A polynomial

Differentiate f(x)=3x2+2xf(x) = 3x^2 + 2x.

Show solution

f′(x)=6x+2f'(x) = 6x + 2.

Exercise 2 · Chain rule

Differentiate e2xe^{2x} and ln⁡(x2+1)\ln(x^2 + 1).

Show solution

2e2x2e^{2x} and 2x/(x2+1)2x/(x^2 + 1).

Exercise 3 · Partial derivatives

For f(x,y)=x2y+3y2f(x, y) = x^2y + 3y^2, find fxf_x, fyf_y and the Hessian.

Show solution

fx=2xyf_x = 2xy, fy=x2+6yf_y = x^2 + 6y. H=(2y2x2x6)H = \begin{pmatrix}2y & 2x\\2x & 6\end{pmatrix} (note fxy=fyx=2xf_{xy} = f_{yx} = 2x).

Exercise 4 · A constant elasticity

Demand is Q=AP−1.5Q = AP^{-1.5}. Find the price elasticity.

Show solution

Step 1. Take logs: ln⁡Q=ln⁡A−1.5ln⁡P\ln Q = \ln A - 1.5\ln P.

Step 2. Elasticity =∂ln⁡Q/∂ln⁡P=−1.5= \partial\ln Q/\partial\ln P = -1.5.

Answer: −1.5 at every price: a 1% price rise cuts quantity demanded by 1.5%.

Exercise 5 · A logit marginal effect

In a logit, P(y=1∣x)=Λ(β0+β1x)P(y = 1 \mid x) = \Lambda(\beta_0 + \beta_1x) with Λ′(z)=Λ(z)(1−Λ(z))\Lambda'(z) = \Lambda(z)(1 - \Lambda(z)). If β1=0.8\beta_1 = 0.8 and the predicted probability is 0.3, what is the marginal effect of xx?

Show solution

Step 1. Chain rule: ∂P/∂x=Λ′(β0+β1x) β1=p(1−p)β1\partial P/\partial x = \Lambda'(\beta_0 + \beta_1x)\,\beta_1 = p(1 - p)\beta_1.

Step 2. At p=0.3p = 0.3: 0.3×0.7×0.8=0.1680.3 \times 0.7 \times 0.8 = 0.168.

Answer: about 17 percentage points per unit of xx at this point; at p=0.5p = 0.5 it would be 0.25×0.8=0.20.25 \times 0.8 = 0.2.

Part 2 · Proofs

Exercise 6 · The product rule

Prove (fg)′=f′g+fg′(fg)' = f'g + fg' from the definition.

Show solution

f(x+h)g(x+h)−f(x)g(x)=[f(x+h)−f(x)]g(x+h)+f(x)[g(x+h)−g(x)]f(x + h)g(x + h) - f(x)g(x) = [f(x + h) - f(x)]g(x + h) + f(x)[g(x + h) - g(x)]. Divide by hh and let h→0h \to 0; continuity of gg gives g(x+h)→g(x)g(x + h) \to g(x), so the limit is f′g+fg′f'g + fg'.

Exercise 7 · The derivative of the log

Using eln⁡x=xe^{\ln x} = x and the chain rule, prove (ln⁡x)′=1/x(\ln x)' = 1/x.

Show solution

Differentiate both sides: eln⁡x(ln⁡x)′=1e^{\ln x}(\ln x)' = 1, so (ln⁡x)′=1/eln⁡x=1/x(\ln x)' = 1/e^{\ln x} = 1/x.

Exercise 8 · The error of a linear approximation

Show that if f′′f'' is bounded by KK near aa, the first-order approximation error is at most K2(x−a)2\frac K2(x - a)^2.

Show solution

Step 1. Taylor's theorem with remainder: f(x)=f(a)+f′(a)(x−a)+12f′′(c)(x−a)2f(x) = f(a) + f'(a)(x - a) + \tfrac12 f''(c)(x - a)^2 for some cc between aa and xx.

Step 2. So the error is 12∣f′′(c)∣(x−a)2≤K2(x−a)2\tfrac12|f''(c)|(x - a)^2 \le \tfrac K2(x - a)^2.

Step 3. It shrinks quadratically: halve the distance, quarter the error.

Exercise 9 · Cobb–Douglas and Euler's theorem

For Y=AKαL1−αY = AK^{\alpha}L^{1-\alpha}, show ∂ln⁡Y/∂ln⁡K=α\partial\ln Y/\partial\ln K = \alpha and KYK+LYL=YKY_K + LY_L = Y.

Show solution

Step 1. ln⁡Y=ln⁡A+αln⁡K+(1−α)ln⁡L\ln Y = \ln A + \alpha\ln K + (1 - \alpha)\ln L, so the elasticity is α\alpha.

Step 2. Then YK=αY/KY_K = \alpha Y/K and YL=(1−α)Y/LY_L = (1 - \alpha)Y/L, so KYK+LYL=αY+(1−α)Y=YKY_K + LY_L = \alpha Y + (1 - \alpha)Y = Y: with constant returns, paying factors their marginal products exhausts output.

Exercise 10 · Second-order Taylor of the log

Derive the second-order Taylor expansion of ln⁡(1+x)\ln(1 + x) at 0 and evaluate it at x=0.1x = 0.1.

Show solution

Step 1. Derivatives of g(x)=ln⁡(1+x)g(x) = \ln(1 + x): g′=1/(1+x)g' = 1/(1 + x), g′′=−1/(1+x)2g'' = -1/(1 + x)^2.

Step 2. At 0: g(0)=0g(0) = 0, g′(0)=1g'(0) = 1, g′′(0)=−1g''(0) = -1.

Step 3. Second-order expansion: ln⁡(1+x)≈0+x−12x2\ln(1 + x) \approx 0 + x - \tfrac12x^2.

Step 4. At x=0.1x = 0.1: 0.1−0.005=0.0950.1 - 0.005 = 0.095, versus the true ln⁡1.1≈0.09531\ln 1.1 \approx 0.09531.

Answer: x−x2/2x - x^2/2; error 0.0003 at x=0.1x = 0.1, against 0.005 for the first-order xx.

Part 3 · Applied

Exercise 11 · Log points versus percent

A log-wage regression gives a training coefficient of 0.25. What percentage effect is that, and how wrong is the "25%" reading?

Show solution

Step 1. A log-point coefficient bb means wages are multiplied by ebe^{b}.

Step 2. 100(e0.25−1)=100(1.284−1)≈28.4%100(e^{0.25} - 1) = 100(1.284 - 1) \approx 28.4\%.

Answer: 28.4%, not 25%. The approximation ln⁡(1+x)≈x\ln(1 + x) \approx x understates large positive effects; below about 0.1 the gap is under half a point.

Exercise 12 · Returns to experience

Wages follow w=10+2 exp−0.05 exp2w = 10 + 2\,\text{exp} - 0.05\,\text{exp}^2. Find the marginal effect of experience at 10 years and where it peaks.

Show solution

Step 1. ∂w/∂ exp=2−2(0.05) exp=2−0.1 exp\partial w/\partial\,\text{exp} = 2 - 2(0.05)\,\text{exp} = 2 - 0.1\,\text{exp}.

Step 2. At 10 years: 2−1=12 - 1 = 1 per extra year.

Step 3. Peak: 2−0.1 exp=0⇒exp=202 - 0.1\,\text{exp} = 0 \Rightarrow \text{exp} = 20 years (second derivative −0.1<0-0.1 < 0).

Answer: the return is 1 per year at 10 years of experience, and the wage profile peaks at 20 years.

Exercise 13 · Heterogeneous treatment effects via interactions

y=β0+β1T+β2X+β3TXy = \beta_0 + \beta_1T + \beta_2X + \beta_3TX with β1=2\beta_1 = 2, β3=0.5\beta_3 = 0.5. What is the effect of TT for a unit with X=4X = 4? With X=0X = 0?

Show solution

Step 1. Differentiate with respect to TT: ∂y/∂T=β1+β3X\partial y/\partial T = \beta_1 + \beta_3X.

Step 2. At X=4X = 4: 2+0.5(4)=42 + 0.5(4) = 4. At X=0X = 0: 22.

Answer: 4 and 2. The "main effect" β1\beta_1 is the effect only where X=0X = 0, which is why covariates are often centred before interacting.

Exercise 14 · Approximating the wage bill

A region's wage bill (in $ billion) is f(g,L)=egLf(g, L) = e^{g}L, where gg is the log growth of the average wage and LL is employment (millions). Start from (g,L)=(0,1)(g, L) = (0, 1). Approximate ff to first order and evaluate it at 10% wage growth (g=0.1g = 0.1) and employment of 1.2 million; compare with the exact value.

Show solution

Step 1. Value at the base point: f(0,1)=e0×1=1f(0, 1) = e^0 \times 1 = 1.

Step 2. Partial derivatives: fg=egL=1f_g = e^{g}L = 1 and fL=eg=1f_L = e^{g} = 1 at (0,1)(0, 1).

Step 3. First-order approximation: f≈1+1(g−0)+1(L−1)f \approx 1 + 1(g - 0) + 1(L - 1). At (0.1,1.2)(0.1, 1.2): 1+0.1+0.2=1.31 + 0.1 + 0.2 = 1.3.

Step 4. Exact: e0.1×1.2≈1.105×1.2=1.326e^{0.1} \times 1.2 \approx 1.105 \times 1.2 = 1.326.

Answer: $1.30 billion approximately versus $1.326 billion exactly. The gap is the second-order (interaction) term: wage growth applies to the extra workers too.

Exercise 15 · Back to the delta method

Use a first-order Taylor expansion of g(p)=p/(1−p)g(p) = p/(1 - p) around p=0.6p = 0.6 to recover Day 12's standard error for the odds when SE⁡(p^)=0.0894\operatorname{SE}(\hat p) = 0.0894.

Show solution

Step 1. g(p)=p/(1−p)g(p) = p/(1 - p) has g′(p)=1/(1−p)2=1/0.16=6.25g'(p) = 1/(1 - p)^2 = 1/0.16 = 6.25 at p=0.6p = 0.6.

Step 2. First-order Taylor: g(p^)−g(p)≈6.25(p^−p)g(\hat p) - g(p) \approx 6.25(\hat p - p).

Step 3. So SE⁡(g(p^))≈6.25×0.0894≈0.559\operatorname{SE}(g(\hat p)) \approx 6.25 \times 0.0894 \approx 0.559.

Answer: 0.559, Day 12's result: the delta method is Taylor's theorem plus the CLT.

Verify in Python

import numpy as np

# Numerical derivatives (central differences) versus the formulas
d = lambda f, x, h=1e-6: (f(x + h) - f(x - h)) / (2 * h)
print("d/dx ln(x^2+1) at 1:", round(d(lambda x: np.log(x**2 + 1), 1.0), 6), "formula:", 2 * 1 / 2)

lam = lambda z: 1 / (1 + np.exp(-z))
z0 = np.log(0.3 / 0.7)                              # where the predicted probability is 0.3
print("logit marginal effect:", round(0.8 * d(lam, z0), 4))

for x in (0.01, 0.1, 0.25, 0.5):
    print(f"x={x}: ln(1+x)={np.log1p(x):.5f}, 1st order={x:.5f}, 2nd order={x - x**2/2:.5f}")
print("exact % effect of 0.25 log points:", round(100 * (np.exp(0.25) - 1), 2))
print("e^x y at (0.1, 1.2):", round(np.exp(0.1) * 1.2, 4), " first-order approx: 1.3")

Key takeaways

  • Derivatives give marginal effects; logs turn them into elasticities and semi-elasticities.
  • Taylor expansions replace any smooth function with a local line or parabola; the error shrinks quadratically.
  • The delta method, Newton's method and Fisher information are all Taylor expansions.

Simulation Lab: Taylor Approximations

Open full screen →

Approximate eˣ, ln(1+x), sin x or the logistic function by polynomials of order 1–4 around any point.

-2020123xf(x)
f(x) = ln(1+x)Taylor polynomial of order 1 at a
error at x = a + 0.39
0.06070
f′(a) (slope)
1.0000
the delta-method multiplier

The Taylor polynomial matches the function's value and first few derivatives at the point a. Near a the error shrinks like |x − a|^(order+1); far away the approximation can be poor. Hover to read the error.

Day 22Day 24