Two jobs: find the lowest point, find where a curve crosses zero
scipy.optimize solves two kinds of problems. The first is minimising a function , that is, finding the input with the smallest output. You call minimize(f, x0) where x0 is your starting guess. The method BFGS follows the slope of downhill. Nelder-Mead needs no slope and just compares values. SLSQP also accepts bounds and constraints, such as weights that must add up to 1. The answer is only a local minimum, the bottom of the valley where you started, and a different x0 can give a different answer. Always check res.success.
The second problem is root finding: solving . brentq(g, a, b) needs a bracket, two points where has opposite signs, and then it is sure to find a root by repeatedly halving the interval. After halvings the interval is wide. newton(g, x0, fprime) uses the slope of and is much faster, but it needs a good start and can jump far away when the slope is nearly flat. In finance the yield to maturity of a bond is a root: it is the rate that makes the bond's price formula equal to its market price. A Newton step is .
brentq on the bracket confirms the root . For bisection on the bracket to be within you need , so halvings. For a minimum, two assets with volatilities and and correlation have a covariance of , so the minimum-variance weight in the first is . On real data, SPY and TLT over days from February 2005 have volatilities of and a year and a correlation of . The minimum-variance weight in SPY is , giving a volatility of . For a function with two valleys, such as , minimize from stops at and from at , which is the lower one.- Minimise or solve:
minimizefinds a lowest point,brentqandnewtonfind where a function is zero. - A bracket is a guarantee:
brentqalways converges when the signs differ, whilenewtonis faster but needs a good start. - A minimum can be local: try several starting points and read
res.successbefore trustingres.x.
Lab · what's graded
- One Newton step15 pts
- Bisection halvings10 pts
- A minimum-variance weight15 pts
- The right tool for each job15 pts
- An implied-volatility workflow10 pts
- A possibly local minimum10 pts
- Why a bracket, and why Newton can fail25 pts