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 where 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 .
- Newton's method iterates . 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 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 starting from .
Show solutionHide solution
The update is :
The errors are about , and : 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 . Consequences:
- Decimal fractions are not exact:
0.1 + 0.2 == 0.3isFalse. 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: is infinite, and a product of thousands of probabilities rounds to zero. Work in logs.
Two stable recipes
Variance. The textbook formula 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 without overflow, factor out the maximum :
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).