ML Foundations Lecture 6 of 14
Part II — Calculus of One Variable · Lecture 6

Taylor Series and Local Approximation

The tangent line is the best straight-line approximation to a curve. Taylor's theorem says what the best parabola is, the best cubic, and exactly how wrong each of them is — which is where second-order optimisers, learning-rate bounds, and half of numerical analysis come from.

By the end you can

  • Construct the degree-\(n\) Taylor polynomial by matching derivatives at a point
  • State Taylor's theorem with the Lagrange remainder and use it to bound an approximation error
  • Derive the series for \(e^x\), \(\sin x\), \(\ln(1+x)\), \(\tfrac1{1-x}\)
  • Explain gradient descent as first-order and Newton's method as second-order Taylor
  • Recognise catastrophic cancellation and know why a mathematically exact series can be numerically useless

Assumed

  • Lectures 4–5: derivatives, the chain rule, higher derivatives
  • Factorials and summation notation

1How wrong is the tangent line?

Lecture 4 gave us \(f(x+h)\approx f(x)+f'(x)h\), and we used it to justify gradient descent while noting the word "approximately" was carrying weight. Now we make the error precise.

The tangent line agrees with \(f\) in two respects at the base point: same value, same slope. It disagrees in a third: curvature. A line has zero second derivative; a curve generally does not. So the natural repair is to add a quadratic term chosen to match the second derivative too. Then a cubic term to match the third. And so on.

Intuition

A Taylor polynomial is a forgery. You are asked to fake a function using only polynomials, given complete information at a single point. All you can copy is the derivatives there — the value, the slope, the curvature, the rate of change of curvature. The remarkable fact is that for many functions, copying enough derivatives at one point reproduces the function over a whole interval.

Constructing the Taylor polynomial by matching derivatives0 / 6
\[P_n(x) = c_0 + c_1(x-a) + c_2(x-a)^2 + \cdots + c_n(x-a)^n\]
The ansatz. A general degree-\(n\) polynomial, deliberately written in powers of \((x-a)\) rather than powers of \(x\). This is the crucial choice: it makes every derivative at \(x=a\) pick out exactly one coefficient, because every other term still carries a factor of \((x-a)\) and therefore vanishes there.
\[P_n(a) = c_0 \quad\Longrightarrow\quad c_0 = f(a)\]
Match the value. Setting \(x=a\) kills every term except the first. Demanding \(P_n(a)=f(a)\) fixes \(c_0\).
\[P_n'(x) = c_1 + 2c_2(x-a)+\cdots \quad\Longrightarrow\quad P_n'(a)=c_1 = f'(a)\]
Match the slope. Differentiate term by term (linearity plus the power rule), then set \(x=a\). Again everything but one term dies. So \(c_1=f'(a)\), and \(P_1\) is exactly the tangent line — Lecture 4's approximation, recovered.
\[P_n''(x) = 2c_2 + 6c_3(x-a)+\cdots \quad\Longrightarrow\quad P_n''(a)=2c_2 = f''(a) \;\Rightarrow\; c_2 = \frac{f''(a)}{2}\]
Match the curvature. Differentiating \(c_2(x-a)^2\) twice gives \(2c_2\) — note the factor of 2 appearing from the power rule. This is where the factorials come from.
\[\frac{d^k}{dx^k}\Big[c_k (x-a)^k\Big]_{x=a} = k!\,c_k \quad\Longrightarrow\quad c_k = \frac{f^{(k)}(a)}{k!}\]
The general pattern. Differentiating \((x-a)^k\) exactly \(k\) times gives \(k(k-1)\cdots 1 = k!\), a constant; differentiating it more times gives 0; differentiating it fewer times leaves a factor of \((x-a)\) that vanishes at \(a\). So the \(k\)-th derivative of \(P_n\) at \(a\) is precisely \(k!c_k\).
\[\boxed{\;P_n(x) = \sum_{k=0}^{n}\frac{f^{(k)}(a)}{k!}(x-a)^k\;}\]
The Taylor polynomial of degree n about a. When \(a=0\) it is also called the Maclaurin polynomial. Nothing here was guessed: each coefficient was forced by a matching condition, exactly as the exponent laws forced \(a^0=1\) in Lecture 2.
Taylor polynomials closing in on the true functionInteractive
f(x)Pₙ(x)expansion point a
0
1
0.00
Raise the degree and watch the purple polynomial peel away from the base point more and more slowly. Two things to notice. First: agreement is always best near a and degrades as you move away. Second: for ln(1+x) and 1/(1−x) the approximation NEVER converges beyond a fixed distance, no matter how high the degree — those functions have a singularity nearby, and it caps the radius of convergence. eˣ and sin x have no such wall.

2Taylor's theorem: the error term

Matching derivatives gives a polynomial that looks right near \(a\). Taylor's theorem turns "looks right" into an exact statement with a computable bound.

Theorem — Taylor with Lagrange remainder

Suppose \(f\) is \((n+1)\)-times differentiable on an interval containing \(a\) and \(x\). Then there exists some point \(\xi\) strictly between \(a\) and \(x\) such that

\[ f(x) \;=\; \underbrace{\sum_{k=0}^{n}\frac{f^{(k)}(a)}{k!}(x-a)^k}_{P_n(x)} \;+\; \underbrace{\frac{f^{(n+1)}(\xi)}{(n+1)!}(x-a)^{n+1}}_{R_n(x)}. \]

Three observations, in increasing order of usefulness:

  1. This is an equality, not an approximation. The remainder \(R_n\) is exactly the shortfall.
  2. The remainder looks like one more Taylor term, except that the derivative is evaluated at a mystery point \(\xi\) rather than at \(a\). We are not told where \(\xi\) is — only that it lies between \(a\) and \(x\).
  3. That is enough. If we can bound \(|f^{(n+1)}|\) by some \(M\) on the whole interval, then \(|R_n(x)| \le \frac{M}{(n+1)!}|x-a|^{n+1}\), regardless of where \(\xi\) hides. This is how error bounds are actually produced.
On the proof

The proof is a clever application of Rolle's theorem to an auxiliary function, and it is about half a page. I am deferring it rather than skipping it: it adds no conceptual content you need right now, and the statement is what does the work in everything below. Ask and we will do it properly.

Use the theorem

Estimate \(\sqrt{e} = e^{0.5}\) using \(P_3\) about \(a=0\), and give a rigorous bound on the error using the Lagrange remainder. Then check your bound against the true error.

Hint: every derivative of \(e^x\) is \(e^x\), and on \([0,0.5]\) you can bound \(e^\xi\) by something concrete without knowing \(\xi\).

The polynomial. All derivatives of \(e^x\) at 0 equal 1, so \(P_3(x)=1+x+\frac{x^2}{2}+\frac{x^3}{6}\). At \(x=0.5\):

\[P_3(0.5)=1+0.5+0.125+0.0208\overline{3}=1.6458\overline{3}.\]

The bound. The remainder is \(R_3(0.5)=\frac{e^{\xi}}{4!}(0.5)^4\) for some \(\xi\in(0,0.5)\). We do not know \(\xi\), but \(e^x\) is increasing, so \(e^\xi < e^{0.5}\). And \(e^{0.5}<e<3\), a bound we can assert without circularity. Hence

\[|R_3(0.5)| < \frac{3}{24}\cdot 0.0625 = 0.0078125.\]

Check. The true value is \(\sqrt e = 1.6487212707\ldots\), so the actual error is \(1.6487213-1.6458333=0.0028879\). Our bound said "less than \(0.0078\)" ✓ — correct, and about a factor of 2.7 loose, which is the price of replacing \(e^\xi\) by its worst case.

That looseness is normal and acceptable. The value of the bound is that it is guaranteed: it holds without ever computing \(\sqrt e\). This is exactly how numerical libraries decide how many terms to use.

3Four series worth knowing

Each is derived the same way: compute derivatives at 0, divide by factorials.

\[\begin{aligned} e^{x} &= 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots = \sum_{k=0}^{\infty}\frac{x^k}{k!} && \text{for all } x\in\RR \\[4pt] \sin x &= x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots = \sum_{k=0}^{\infty}\frac{(-1)^k x^{2k+1}}{(2k+1)!} && \text{for all } x\in\RR \\[4pt] \ln(1+x) &= x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots = \sum_{k=1}^{\infty}\frac{(-1)^{k+1}x^k}{k} && \text{for } -1 < x \le 1 \\[4pt] \frac{1}{1-x} &= 1 + x + x^2 + x^3 + \cdots = \sum_{k=0}^{\infty}x^k && \text{for } |x| < 1 \end{aligned}\]

The domain restrictions on the last two are not decorative. The geometric series \(\sum x^k\) diverges for \(|x|\ge1\) — try \(x=2\) and you get \(1+2+4+8+\cdots\), which is not \(1/(1-2)=-1\). The radius of convergence is determined by how far the nearest singularity is from the expansion point: \(1/(1-x)\) blows up at \(x=1\), which sits at distance 1 from \(a=0\), and that distance is exactly the radius. You saw this in the figure above: raising the degree improved things inside the radius and made them dramatically worse outside it.

Check yourself
Use \(\ln(1+x)\approx x\) for small \(x\) to explain why, for a probability \(p\) very close to 1, \(-\ln p \approx 1-p\).
Correct. Correct. Substituting \(x=-\epsilon\) into \(\ln(1+x)\approx x\) gives \(\ln(1-\epsilon)\approx -\epsilon\), so \(-\ln(1-\epsilon)\approx\epsilon\). Since \(\epsilon = 1-p\), we get \(-\ln p\approx 1-p\).

This is a genuinely useful fact: cross-entropy loss \(-\ln p\) for a confident correct prediction is approximately the error probability \(1-p\). At \(p=0.99\), \(-\ln p = 0.01005\) versus \(1-p=0.01\) — agreement to three digits. It is also why log1p exists in every maths library: computing \(\ln(1+x)\) as log(1+x) loses precision when \(x\) is tiny, because \(1+x\) rounds to 1.
Not quite. They are not equal — \(\ln(1)=0\) while the identity gives 1. The correct statement is about \(\ln(1+x)\) versus \(x\), which agree to first order at \(x=0\): both are 0 there and both have slope 1 there. That is a Taylor statement, and it is why the substitution \(p=1-\epsilon\) is the necessary first move.
Not quite. Test it: \(\ln(0.99)=-0.01005\), which is nowhere near \(0.99\). The approximation \(\ln(1+x)\approx x\) is anchored at \(x=0\), i.e. at argument \(1\), and only holds for arguments close to 1. Anchoring matters: a Taylor approximation is local to its expansion point.

4Second order: where Newton's method comes from

Now the payoff for optimisation. Take the Taylor expansion of a loss \(f\) about the current point \(w\), keeping two terms:

\[f(w+h) \;\approx\; f(w) + f'(w)\,h + \tfrac12 f''(w)\,h^2 \;=:\; q(h).\]

This is a parabola in \(h\), and we know how to minimise a parabola exactly.

Minimising the quadratic model0 / 4
\[q(h) = f(w) + f'(w)h + \tfrac12 f''(w)h^2\]
The local model. The best quadratic approximation to \(f\) near \(w\). Note that \(f(w)\), \(f'(w)\), \(f''(w)\) are all just numbers once \(w\) is fixed; \(h\) is the only variable.
\[q'(h) = f'(w) + f''(w)\,h \;\stackrel{!}{=}\; 0\]
Set the derivative to zero. The minimum of a parabola is at its unique critical point — provided it opens upwards, which needs \(f''(w)>0\). We will return to that condition.
\[h^{\star} = -\frac{f'(w)}{f''(w)}\]
Solve. One line of algebra, valid whenever \(f''(w)\neq0\).
\[w_{t+1} = w_t - \frac{f'(w_t)}{f''(w_t)} \qquad \textbf{(Newton's method)}\]
Iterate. Compare with gradient descent \(w_{t+1}=w_t-\eta f'(w_t)\). The step direction is the same — downhill — but Newton computes the step size \(1/f''\) instead of guessing it. Where the curve is sharply curved (\(f''\) large) it takes small steps; where the curve is flat (\(f''\) small) it takes large ones. This is exactly the adaptive behaviour that learning-rate schedules try to imitate by hand.
Gradient descent vs Newton on f(w) = ¼w⁴ − w² + 0.3w + 2Interactive
f(w)gradient descentNewtonNewton's quadratic model
2.00
0.120
4
The green dashed parabola is Newton's local model at its current point; Newton jumps straight to that parabola's minimum. It typically reaches the answer in a handful of steps where gradient descent needs dozens. But look what happens if you start near w = 0, where f″ turns negative: Newton's model opens DOWNWARDS, so its 'minimum' is a maximum and the step runs uphill. That failure mode is why second-order methods are not the default in deep learning.
Where this shows up in ML

In one dimension Newton needs one number, \(f''\). In \(d\) dimensions it needs the Hessian: a \(d\times d\) matrix of second partial derivatives, which must then be inverted. For a model with \(d=10^9\) parameters that is \(10^{18}\) entries — not storable, let alone invertible. That, plus the negative-curvature failure you can see in the figure, is why deep learning uses first-order methods.

But the idea survives in disguise. Adam, RMSProp and their relatives maintain a running estimate of the diagonal of something Hessian-like and divide the gradient by it. They are cheap approximations to Newton, and the family name for such methods is quasi-Newton. You will build them in Phase D, and you now know what they are approximating.

5The learning-rate bound, derived

We can also now derive the bound that Lecture 4's exercise had you discover numerically. Take \(f\) with \(f''(w) = L\) roughly constant near the minimum \(w^\star\) (i.e. \(f\) is locally quadratic). Gradient descent gives

\[\begin{aligned} w_{t+1}-w^\star &= w_t - \eta f'(w_t) - w^\star \\ &= (w_t - w^\star) - \eta\,L\,(w_t-w^\star) \qquad \text{since } f'(w)\approx L(w-w^\star)\\ &= (1-\eta L)\,(w_t-w^\star). \end{aligned}\]

So the distance to the optimum is multiplied by \(|1-\eta L|\) every step. Convergence requires that factor to be less than 1:

\[|1-\eta L| < 1 \quad\Longleftrightarrow\quad -1 < 1-\eta L < 1 \quad\Longleftrightarrow\quad 0 < \eta < \frac{2}{L}.\]

In many dimensions \(L\) becomes the largest eigenvalue of the Hessian, \(\lambda_{\max}\), and the bound becomes \(\eta<2/\lambda_{\max}\). We prove that in Lecture 11, once we have eigenvalues. The one-dimensional case here is the whole idea, with the linear algebra stripped away.

6When an exact series is numerically useless

Catastrophic cancellation in the exponential seriesPython · NumPy

7Exercises

★

Find \(P_3(x)\) about \(a=0\) for each, by computing derivatives:

(a) \(f(x)=\cos x\)    (b) \(f(x)=\sqrt{1+x}\)    (c) \(f(x)=\sigma(x)=\frac{1}{1+e^{-x}}\)

(a) \(f=\cos,\ f'=-\sin,\ f''=-\cos,\ f'''=\sin\). At 0: \(1,0,-1,0\). So \(P_3(x)=1-\frac{x^2}{2}\). (The cubic coefficient is zero — \(\cos\) is even, so all odd terms vanish.)

(b) \(f=(1+x)^{1/2}\), \(f'=\frac12(1+x)^{-1/2}\), \(f''=-\frac14(1+x)^{-3/2}\), \(f'''=\frac38(1+x)^{-5/2}\). At 0: \(1,\ \frac12,\ -\frac14,\ \frac38\). Divide by factorials:

\[P_3(x)=1+\frac{x}{2}-\frac{x^2}{8}+\frac{x^3}{16}.\]

Check at \(x=0.1\): \(P_3=1.0488125\) versus \(\sqrt{1.1}=1.0488088\) — four correct digits.

(c) Use \(\sigma'=\sigma(1-\sigma)\) from Lecture 5. At \(x=0\), \(\sigma=\frac12\), so \(\sigma'=\frac14\). Differentiating again: \(\sigma''=\sigma'(1-2\sigma)\), which at 0 is \(\frac14(1-1)=0\). And \(\sigma'''=\sigma''(1-2\sigma)-2(\sigma')^2\), which at 0 is \(0-2\cdot\frac1{16}=-\frac18\). So

\[P_3(x)=\frac12+\frac{x}{4}-\frac{x^3}{48}.\]

The vanishing quadratic term reflects the fact that \(\sigma(x)-\frac12\) is an odd function.

★★

Use the Lagrange remainder to determine how many terms of the series for \(e^x\) are needed to compute \(e^{1}\) with error below \(10^{-10}\).

Then verify your answer by direct computation.

With \(a=0\), \(x=1\), and \(f^{(n+1)}(\xi)=e^{\xi}\) for some \(\xi\in(0,1)\):

\[|R_n(1)| = \frac{e^{\xi}}{(n+1)!}\cdot 1^{n+1} < \frac{3}{(n+1)!},\]

using \(e^\xi < e < 3\). We need \(\frac{3}{(n+1)!} < 10^{-10}\), i.e. \((n+1)! > 3\times10^{10}\).

Factorials: \(13!=6.227\times10^{9}\) (too small), \(14!=8.718\times10^{10}\) ✓. So \(n+1=14\), giving \(n=13\) — that is terms \(k=0\) through \(13\), fourteen terms.

import numpy as np, math
for n in (11, 12, 13, 14):
    s = sum(1.0/math.factorial(k) for k in range(n+1))
    print(n, f"{s:.15f}", f"err {abs(s-np.e):.2e}")

You will find \(n=13\) gives error \(1.2\times10^{-11}\) — comfortably inside the target, and the bound correctly predicted it. \(n=12\) gives \(1.7\times10^{-10}\), just outside. So the bound identified the right cutoff exactly. It is loose by about a factor of 8 on the error itself, which is the price of replacing \(e^\xi\) by 3.

★★

Show that for gradient descent on the exact quadratic \(f(w)=\tfrac{L}{2}(w-w^\star)^2\), the error after \(t\) steps is exactly

\[ w_t - w^\star = (1-\eta L)^{t}\,(w_0-w^\star). \]

Then answer: with \(L=10\), how many steps are needed to reduce the error by \(10^{6}\), at (i) \(\eta=0.01\), (ii) \(\eta=0.1\), (iii) \(\eta=0.19\)? Comment on the pattern.

Derivation. \(f'(w)=L(w-w^\star)\), so

\[w_{t+1}-w^\star = w_t - \eta L(w_t-w^\star) - w^\star = (1-\eta L)(w_t-w^\star).\]

Each step multiplies the error by the same constant \(r=1-\eta L\), so by induction \(w_t-w^\star = r^t(w_0-w^\star)\). ∎ (For an exact quadratic there is no approximation: the relation is an identity, not a local model.)

Step counts. Need \(|r|^t \le 10^{-6}\), i.e. \(t \ge \frac{-6\ln 10}{\ln|r|}\).

  • \(\eta=0.01\): \(r=0.9\), \(\ln|r|=-0.10536\), \(t\ge 131.1\) → 132 steps.
  • \(\eta=0.1\): \(r=0\) → 1 step. Exact convergence, because \(\eta=1/L\) is the Newton step.
  • \(\eta=0.19\): \(r=-0.9\), \(|r|=0.9\), \(t\ge131.1\) → 132 steps, but with the error alternating in sign — overshooting past the minimum every step.

The pattern. Convergence speed is governed by \(|1-\eta L|\), which is a V-shape in \(\eta\) with its minimum (zero) at \(\eta=1/L\) and roots of the boundary at \(\eta=0\) and \(\eta=2/L\). Speed is symmetric about \(\eta=1/L\): being 90% too small and 90% too large cost exactly the same. This is why learning-rate tuning has a sweet spot rather than a "bigger is faster" regime, and why the standard advice is to increase \(\eta\) until training becomes unstable and then back off — you are searching for \(2/\lambda_{\max}\) empirically.

★★★

Coding + analysis. Newton's method for root-finding solves \(g(w)=0\) via \(w_{t+1}=w_t - g(w_t)/g'(w_t)\). Applying it to \(g=f'\) gives the optimiser above.

  1. Implement it for \(g(w)=w^2-2\) (so the root is \(\sqrt2\)) starting from \(w_0=1\). Print the error at each step. How many correct digits do you gain per iteration?
  2. This is quadratic convergence: \(|e_{t+1}|\approx C|e_t|^2\). Estimate \(C\) numerically and compare with the theoretical \(C = \frac{|g''|}{2|g'|}\) at the root.
  3. Now apply Newton to minimise \(f(w)=\frac14 w^4 - w^2 + 0.3w + 2\) starting from \(w_0=0.1\). What happens, and why? Relate your answer to the sign of \(f''\).
import numpy as np
g, gp = lambda w: w*w - 2, lambda w: 2*w
w, root = 1.0, np.sqrt(2)
for t in range(6):
    e = abs(w - root)
    print(f"t={t}  w={w:.18f}  err={e:.3e}")
    w = w - g(w)/gp(w)

1. The errors run \(4.14\times10^{-1},\ 8.58\times10^{-2},\ 2.45\times10^{-3},\ 2.12\times10^{-6},\ 1.59\times10^{-12},\ 0\). The number of correct digits doubles each iteration — from 0 to 1 to 2 to 5 to 11 to full machine precision. Five iterations from a poor starting guess.

2. \(C \approx e_{t+1}/e_t^2\). The successive ratios are \(0.500,\ 0.333,\ 0.353,\ 0.354\dots\) — settling on about \(0.354\). Theory: at \(w=\sqrt2\), \(g''=2\) and \(g'=2\sqrt2\), so \(C=\frac{2}{2\cdot 2\sqrt2}=\frac{1}{2\sqrt2}=0.35355\) ✓. The first ratio is off because the early iterates are still far from the root, where the quadratic model of the error is not yet accurate.

3. \(f'(w)=w^3-2w+0.3\) and \(f''(w)=3w^2-2\). At \(w_0=0.1\), \(f''(0.1)=0.03-2=-1.97\), which is negative. The quadratic model \(q(h)=f+f'h+\frac12 f''h^2\) therefore opens downwards: its critical point is a maximum, not a minimum. Newton dutifully jumps to it, moving uphill.

Concretely, \(f'(0.1)=0.001-0.2+0.3=0.101\), so the step is \(-0.101/(-1.97)=+0.0513\) — a step in the positive gradient direction, i.e. increasing \(f\). From there behaviour is erratic: the iterates wander in the negative-curvature region, and where \(f''\) passes through zero (at \(w=\pm\sqrt{2/3}\approx \pm0.8165\)) the step size blows up.

The general lesson. Newton finds critical points, not minima. It is attracted to maxima and saddle points just as readily. In high dimensions almost all critical points of a deep network's loss are saddles, which makes raw Newton actively harmful. Practical second-order methods therefore modify the curvature — trust regions, damping \((\mathbf{H}+\lambda \mI)^{-1}\), or Gauss–Newton approximations that are positive semi-definite by construction. Every one of those is a fix for the failure you just observed.


Next: Lecture 7 makes optimisation in one dimension complete and rigorous — critical points, the second-derivative test, convexity, and the guarantee that separates a problem you can solve from one you can only attempt.