Taylor Expansion — Approximating any function with a polynomial
Taylor Expansion -- Approximating any function with a polynomial
Any smooth function can be approximated by a polynomial near a point.
The first-order Taylor expansion is the basis of gradient descent, and the second-order expansion introduces Newton's method. Understanding this chapter is sufficient; deep diving into the derivation details is not necessary.
Concept Analysis
First-order Taylor = Linear Approximation (Basis of Gradient Descent)
[ f(x) \approx f(a) + f'(a)(x - a) ]
This is "drawing a tangent line at point a and using the tangent line value to approximate the function value."
Second-order Taylor = Considering curvature
[ f(x) \approx f(a) + f'(a)(x - a) + \frac{f''(a)}{2}(x - a)^2 ]
Adding the second-order term can capture the "curvature" of the function—this is the idea behind Newton's method.
Taylor series expansion of an important function
| Function | Expansion at x=0 |
|---|---|
| ( e^x ) | ( 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots ) |
| ( \sin x ) | ( x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots ) |
Gradient descent only uses first-order information (direction). Newton's method uses second-order information (curvature), converging faster but with a higher computational cost.
Real-life examples
Estimating sin(0.1): sin(0)=0, sin'(0)=cos(0)=1. First-order approximation: 0 + 1×0.1 = 0.1.
The true value is approximately 0.0998—a high-precision approximation achieved using only addition and multiplication.
Python hands-on practice
Example
import
numpy
as
np
x0
=
0.5
true_val
=
np.
sin
(
x0
)
(
f
Taylor approximation of sin({x0}) vs true value {true_val:.8f}:
)
Manually calculate the first few terms of the Taylor expansion
import
math
approx
=
0.0
for
n
in
range
(
6
)
:
if
n %
2
==
1
:
Even terms are 0
coef
=
(
1
)
**
(
(
n-
1
)
//
2
)
/
math
.
factorial
(
n
)
approx +
=
coef * x0**n
err
=
abs
(
approx - true_val
)
(
f
" {n} item: {approx:.8f} error: {err:.2e}"
)
Output:
Original prompt (Chinese, left untranslated)
sin(0.5) 的泰勒近似 vs 真实值 0.47942554:
1项: 0.50000000 误差: 2.06e-02
3项: 0.47916667 误差: 2.59e-04
5项: 0.47942708 误差: 1.54e-06
Application scenarios in AI
Gradient descent = first-order Taylor approximation
Gradient descent assumes the loss function can be approximated locally by a first-order Taylor expansion (linear approximation). Taking a step in the direction of the negative gradient is equivalent to making the optimal update under the first-order approximation. This is why gradient descent requires a small learning rate—if the learning rate is too large, it exceeds the range where the linear approximation is valid.
Newton's method = Second-order Taylor optimization
If the second-order term is added (using the Hessian matrix), the loss can be accurately approximated by a quadratic function locally, allowing a jump to the minimum of the quadratic approximation in one step. Theoretically, convergence is faster, but the Hessian matrix is of the square of the number of parameters—a million-parameter model requires a trillion-level Hessian, which is impractical for computation and storage.
The objective function of XGBoost
XGBoost approximates the objective function in each iteration using the second-order Taylor expansion of the loss function, utilizing both the first-order gradient and the second-order Hessian (called hess in XGBoost). Compared to GBDT which only uses the first-order gradient, it converges faster and is more stable. This is the most successful application of second-order information in industrial-grade ML.