Skip to content
LeetQuant
QR04 Regression and Numerical Optimisation

QR04 · Regression and Numerical Optimisation · Lesson 2 of 6

Root Finding & Numerical Stability

Bisection and Newton's method, floating-point pitfalls, stable variance and log-sum-exp.

25 min

Root finding

Many quant computations invert a function: implied volatility from a price, yield from a bond price, a threshold from a probability.

  • Bisection keeps an interval [a,b][a, b] where ff changes sign and halves it. It always converges if you start with a sign change, gaining one binary digit per step: about 20 steps per factor of 10610^6.
  • Newton's method iterates xn+1=xn−f(xn)/f′(xn)x_{n+1} = x_n - f(x_n)/f'(x_n). Near a simple root it converges quadratically (the number of correct digits roughly doubles each step), but it can overshoot or diverge from a poor start, or where f′f' is small.
  • Brent's method combines the two: the safety of bisection with fast steps when they work. It is the default choice in libraries.

For implied volatility, Newton uses vega as the derivative. Deep in or out of the money, vega is tiny and Newton steps explode, so production code falls back to bisection there.

Worked example

Newton for √2

Apply Newton's method to f(x)=x2−2f(x) = x^2 - 2 starting from x0=1x_0 = 1.

Show solution

The update is xn+1=12(xn+2/xn)x_{n+1} = \tfrac12(x_n + 2/x_n):

x1=1.5,x2≈1.416667,x3≈1.414216.x_1 = 1.5, \quad x_2 \approx 1.416667, \quad x_3 \approx 1.414216 .

The errors are about 0.0860.086, 0.00250.0025 and 0.0000020.000002: each is roughly the square of the one before, as quadratic convergence predicts.

Floating point

Double precision carries about 16 significant digits, so machine epsilon is about 2.2×10−162.2 \times 10^{-16}. Consequences:

  • Decimal fractions are not exact: 0.1 + 0.2 == 0.3 is False. Compare floats with a tolerance, and store prices as integer ticks or decimals when exactness matters.
  • Catastrophic cancellation: subtracting two nearly equal numbers loses most of their significant digits.
  • Adding many small numbers to a large total loses the small ones; sum in pairs or use compensated (Kahan) summation.
  • Overflow and underflow: e1000e^{1000} is infinite, and a product of thousands of probabilities rounds to zero. Work in logs.

Two stable recipes

Variance. The textbook formula x2‾−xˉ2\overline{x^2} - \bar x^2 cancels catastrophically when the mean is large relative to the spread (prices around 10,000 that move by 0.01). Welford's one-pass update is stable:

def welford(xs):
    n, mean, m2 = 0, 0.0, 0.0
    for x in xs:
        n += 1
        d = x - mean
        mean += d / n
        m2 += d * (x - mean)
    return mean, m2 / (n - 1)   # sample variance

Log-sum-exp. To compute ln⁡∑ieai\ln\sum_i e^{a_i} without overflow, factor out the maximum mm:

ln⁡∑ieai=m+ln⁡∑ieai−m.\ln\sum_i e^{a_i} = m + \ln\sum_i e^{a_i - m} .

Every term is now at most 1. This appears in softmax, likelihoods and mixture models.

Knowledge check

Roughly how many bisection steps shrink an interval of width 1 to width 10⁻⁶?

Knowledge check

When does Newton's method for implied volatility tend to fail?

Knowledge check

Why does the formula mean(x²) − mean(x)² give poor variances for prices near 10,000 with tiny moves?

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).