Approximating Functions with Taylor Series
Intuition
Near a point, smooth functions look like polynomials. A Taylor series builds that polynomial term by term; adding more terms extends the region where it matches.
Precise explanation
The Taylor series of around 0:
Truncating after terms gives an approximation. Compute terms recursively rather than with powers and factorials:
This avoids overflow in and is cheaper.
Stopping rule: keep adding terms until the next term's magnitude is below a tolerance.
For large negative , alternating terms cancel badly (loss of precision). A standard fix is for — you can try it as an extension.
Worked example
with 5 terms: ; true value 2.71828…
Common mistakes
- Starting the sum at and missing the constant term 1.
- Off-by-one in "number of terms" (5 terms means ).
- Using
math.factorialandx**kfor large k →OverflowErroror slow code.
Why this matters
Linear (first-order) Taylor approximations justify gradient descent, and second-order ones justify Newton's method. Softmax and sigmoid are built on .
References
- Mathematics for Machine Learning — Marc Peter Deisenroth, A. Aldo Faisal, Cheng Soon Ong (Cambridge University Press) (
S009)