Gradium × TensorTonic Voice Challenge is open for registration.Register now
Modules
05/09
Calculus

Contents

Taylor Series Approximation

Turning complex functions into simple polynomials. The mathematical foundation of second-order optimization.

By Pratham Grover

What is a Taylor series?

A Taylor series approximates a smooth function near a point with a polynomial built from its derivatives at that point, adding the value, then the slope, then the curvature and so on. In machine learning the first two orders matter most, because gradient descent relies on the linear approximation and Newton's method on the quadratic one, which uses the Hessian. On TensorTonic you build first and second-order Taylor approximations of a loss and use the quadratic one for Newton steps, with tests that check your output.

Computers are surprisingly limited. At the hardware level, they can only add, subtract, multiply, and divide. So how does your calculator compute sin⁡(1.5)\sin(1.5) or e2.5e^{2.5}?

It cheats. It replaces these complex transcendental functions with really long polynomials. Polynomials are just addition and multiplication, which computers understand.

Why This Matters for ML

  • Gradient Descent: First-order Taylor approx (linear) justifies the update rule.
  • Newton's Method: Second-order Taylor approx (quadratic) enables smarter steps.
  • XGBoost: Uses Taylor expansion of loss for efficient tree construction.
  • Understanding Optimization: Taylor series explains why optimization algorithms work.

The Core Idea

Imagine forging a signature. To make your forgery match the original at a specific point, you need to match several properties:

0

Position

Your approximation passes through the same point. f(a) = P(a)

1

Velocity (Slope)

The curve is going in the same direction. f'(a) = P'(a)

2

Acceleration (Curvature)

The curve bends at the same rate. f''(a) = P''(a)

Match enough derivatives at point a, and your polynomial becomes indistinguishable from the original function near that point.

The Taylor Formula

The Taylor series of f(x) centered at point a is:

f(x)=f(a)+f′(a)(x−a)+f′′(a)2!(x−a)2+f′′′(a)3!(x−a)3+⋯f(x) = f(a) + f'(a)(x-a) + \frac{f''(a)}{2!}(x-a)^2 + \frac{f'''(a)}{3!}(x-a)^3 + \cdots

General form: f(x)=∑n=0∞f(n)(a)n!(x−a)nf(x) = \sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}(x-a)^n

Maclaurin Series

When a = 0, we call it a Maclaurin series. Common examples you should know:

ex=1+x+x22!+x33!+⋯e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots

sin⁡x=x−x33!+x55!−⋯\sin x = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots

Convergence

The series converges to f(x) within a "radius of convergence." Further from a, more terms are needed for accuracy.

Interactive: Taylor Approximation

Watch how higher-order Taylor polynomials approximate sin(x) better and better near the center point.

Taylor Approximation

axy-2π-ππ2π

Configuration

Order (n)1
ConstantLinearQuadraticHigh Order
Center (a)0.00

Polynomial Terms

f(x)≈∑k=01f(k)(a)k!(x−a)kf(x) \approx \sum_{k=0}^{1} \frac{f^{(k)}(a)}{k!}(x-a)^k
k=00.00/ 0 • (x-a)^0
k=11.00/ 1 • (x-a)^1
Notice how the green curve "hugs" the blue curve closer as Order increases.

Orders of Approximation

In optimization, we typically use only the first few terms:

First-Order (Linear)

f(x)≈f(a)+f′(a)(x−a)f(x) \approx f(a) + f'(a)(x-a)

Approximates f as a straight line (tangent). This is what gradient descent "sees."

Second-Order (Quadratic)

f(x)≈f(a)+f′(a)(x−a)+f′′(a)2(x−a)2f(x) \approx f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2

Approximates f as a parabola. Includes curvature via Hessian.

Multivariate Taylor Series

Neural networks have vector inputs. The multivariate second-order Taylor expansion is:

f(x)≈f(a)+∇f(a)T(x−a)+12(x−a)TH(a)(x−a)f(\mathbf{x}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a})^T(\mathbf{x}-\mathbf{a}) + \frac{1}{2}(\mathbf{x}-\mathbf{a})^T H(\mathbf{a})(\mathbf{x}-\mathbf{a})

Constant term

f(a): current value

Linear term

gradient: direction of change

Quadratic term

Hessian: curvature info

Connection to Optimization

Why Gradient Descent Works

Near our current point theta, the loss looks like a plane: L(θ+ϵ)≈L(θ)+ϵT∇LL(\theta + \epsilon) \approx L(\theta) + \epsilon^T \nabla L

To minimize, we want ϵT∇L\epsilon^T \nabla L as negative as possible. This means pointing epsilon opposite to the gradient.

Newton's Method

Uses the quadratic approximation. If f is approximately a parabola, we can jump directly to its minimum:

θnew=θold−H−1∇f\theta_{new} = \theta_{old} - H^{-1} \nabla f

Much faster convergence than GD, but computing H⁻¹ is O(n³).

ML Applications

XGBoost's Second-Order Objective

XGBoost approximates the loss using second-order Taylor expansion: L≈∑[gift+12hift2]L \approx \sum[g_i f_t + \frac{1}{2}h_i f_t^2] where g is gradient and h is Hessian.

Natural Gradient

Uses Fisher information matrix (related to Hessian) to take steps in the "natural" parameter space. More efficient for probabilistic models.

BFGS / L-BFGS

Quasi-Newton methods that approximate the Hessian from gradient history. Get second-order benefits without computing full Hessian.

Activation Function Approximations

GELU can be approximated using Taylor series for efficient computation: GELU(x)≈0.5x(1+tanh⁡[...])\text{GELU}(x) \approx 0.5x(1 + \tanh[...])

Frequently asked questions about Taylor series

What is the difference between a Taylor series and a Maclaurin series?

A Maclaurin series is a Taylor series centered at a = 0. The common expansions of e^x and sin x are usually written in this form.

Why does gradient descent rely on the first-order Taylor approximation?

Near the current parameters the loss looks like a plane, so the change in loss is approximately the dot product of the step with the gradient. Pointing the step opposite to the gradient makes that change as negative as possible, which is the gradient descent update.

Practice this

On TensorTonic you implement these approximations from scratch and test them in the browser. The Calculus for ML study plan collects 30 calculus problems, from limits to Hessians.