◈ quant roadmapPart 0 · Ch 08/45
Quantitative Finance — the Mathematics of Markets · chapter 08

08Multivariable Calculus & Optimization

Chapter 7 closed on a shape. A positive-definite matrix pushes every direction of a circle outward, and the surface behind such a matrix curves upward everywhere, like a bowl. That told us a bottom exists. It never told us which way to walk to reach it, and reaching it is the entire job of this chapter. Fitting a model and choosing a portfolio turn out to be the same physical act. You walk a landscape of many variables toward its lowest point. The whole of it hangs on one arrow. The gradient is a vector of ordinary slopes, and it always points the steepest way uphill, so you reach the bottom by moving against it until the arrow shrinks to nothing. A constraint changes almost nothing about that story. It just says stop the moment your arrow lines up with the constraint's arrow. By the end you will be able to take an optimisation you have never seen, name its surface, find where its gradient dies, check whether that flat spot is really the bottom, and say what a budget line does to the answer.

This chapter sits at a junction. Almost everything in Parts 2 through 5 is an optimisation in disguise, wearing a different name each time. Click a downstream node in the panel below and watch the road run back to this page.

You are here — every optimisation in the course runs through this page
Part 0 MODEL ESTIMATE LEARN PRICE ACT you are here Ch 08 Ch 11 expectation Ch 20 OLS Ch 23 boosting Ch 28 Greeks Ch 30 Markowitz tap a blue chapter — see the exact debt it owes this page What Chapter 7 handed us — the same object, two ways: seen from above the level rings of a bowl a circle the matrix squashed to an ellipse ? seen side-on it slopes down in every direction — but from up here, which way? positive-definite ⇒ every slice curves upward
Chapter 08 is the hinge. Every fit and every allocation ahead is an optimisation decided right here.
tap a chapter ↓ · or a blue dot on the map
pick a chapter to trace its debt
What you’re looking at — the whole course funnels through this one page.
each blue chapter secretly runs an optimisation — tap it to read the debt
gold = this page; the road each debt traces back to, and the answer it’s waiting on
Ch 7 handed us a bowl — a bottom exists, but not which way to walk
Fig. 1.  1 — The course, mapped. Chapter 08 is where every downstream fit and allocation is actually decided: tap a blue chapter and a gold road traces the exact debt it owes this page — OLS is ∇(squared error) = 0, boosting is descent in function space, Markowitz is a Lagrange problem, the Greeks are partial derivatives. Then flip the toggle: Chapter 7 proved this matrix makes a bowl, so a bottom exists — but knowing a surface curves up in every direction says nothing about which way to walk. That walk is the whole chapter.

Four of those roads are worth naming now, because they are the reason this chapter earns its length. OLS regression in Chapter 20 is what you get when you set the gradient of the squared error to zero. Gradient boosting in Chapter 23 is descent performed in the space of functions rather than numbers. Markowitz in Chapter 30 is a constrained quadratic, solved with the Lagrange multiplier we build here. And the double integral at the end becomes expectation itself in Chapter 11.

01The landscape you are standing on

For five chapters a function has meant one thing to us. You feed in a number, you get a number back, and the picture is a curve drawn over a line of inputs. That picture has to break now, because a portfolio has a weight for every asset and a model has a parameter for every feature. One input is not enough.

So add a second input. The inputs no longer sit on a line. They sit on a flat plane, one axis for each input, and the output becomes a height above every point of that plane. The curve has become a surface. That is the whole change, and it is one extra direction rather than a new kind of object.

You already own the reading device for this, and it has been in your pocket for years. A weather map paints temperature over a country, where the flat map is the input and the colour is the output. A hiker's map does the same job with altitude, and it uses a trick worth stealing right now. It draws contour lines, each one joining every place at the same height.

Tilt the hill — a surface and its contour map are the same function said twice.
x 0.00 y 0.00 height 30.00 the hill f(x, y) — tilt it → drag the pin ✦ the contour map · rings every 2 m
Drag the pin on the map. Then slide viewing angle to straight overhead and watch the wireframe flatten into the very same rings.
height 30.0 → one contour ring
What you're looking at — one function f(x, y), drawn twice
the flat page is the input plane (x, y) — every spot you could stand
gold rings join inputs of equal height, the output — that's the map
the cyan ring = every input at the pin's exact height — one output, one loop
Fig. 2. A function of two inputs is a landscape: the flat plane is where you stand, the height is what it returns. Tilt the hill to straight overhead and the wireframe collapses onto its own contour map — the same function said twice. Drag the pin: wherever it lands, the gold dot walks the hill and the readout prints x, y and height, while the cyan ring traces every other input that returns that identical height. The topographic map in your pocket was drawing multivariable functions all along.

Tilt the hill in that panel and watch the rings on the flat map beneath it move in step. The two pictures are the same function, said twice. Here is the one confusion worth killing before it starts. The flat map is not the surface. The flat map is the input plane, every point of it is a pair of numbers you are allowed to feed in, and the height is what comes back out.

Let's pin a specific hill down so every number in this chapter is checkable. Take x as hundreds of metres east of a trailhead and y as hundreds of metres north, with the height in metres given by

f(x, y) = 30 + 3x + 4y − x² − y² + xy

At the trailhead itself both inputs are zero, so the height is 30 metres. Walk east and the 3x term lifts you. Walk north and the 4y term lifts you faster. The two squared terms bend the whole thing over into a dome, and the xy term is the one that makes east and north interact. Keep an eye on that last term, because it is the source of the only genuine subtlety in the first half of this chapter.

Crowded rings mean a hard climb — walk a line across the hill and read the profile it carves.
peak 100 m drag either end ⟵ ⟶ the hill, seen from above height as you walk the line start end 0 m each dot = one 10 m ring
Two ways to cross the same hill:
4 rings · len 7.0 · 6 m per unit
Gentle: the line skims the rings — few crossings, so few metres per step.
What you're looking at — the SAME hill from above, and the climb your line carves through it.
rings, drawn every 10 m of height
your walking line — drag either end
every ring it crosses, dropped onto the profile
Crowded rings aren't a convention: squeezing the same 10 m into less ground is the definition of steep.
Fig. 3. The rings are the hill's height lines, one every 10 m. Drag either end of your walking line — the strip beside it plots how high you are at each step, and every ring you cross drops a gold mark onto that climb. Run along the ridge and you skim between rings: the profile stays nearly flat, few marks. Turn straight up the face and you slam across every ring at once: the profile rockets, marks everywhere. Same 10 m per ring both times — so packing them into less ground is steepness. Hit shade the gaps and the wide bands (gentle) and thin bands (steep) show it with no numbers at all. That is why a cartographer can point at the hard climb before anyone writes down a single derivative.

Now drag the walking line across the contour map in that panel and read the height profile that draws itself beside it. Where the rings are far apart the profile is nearly flat, and where they crowd together the profile climbs hard. That is not a coincidence and it is worth saying out loud. The rings are drawn at equal height steps, so squeezing the same climb into less ground is what steepness means.

Which means the map was already encoding slope before we did any calculus at all. A cartographer with no derivatives can look at a contour map and tell you where the hard climbing is. Our job in the next three sections is to turn that visual fact into a number, and then into an arrow.

02One axis at a time

Stand at the trailhead and ask the simplest possible slope question. Face east, ignore north entirely, and ask how steeply the ground rises under your feet. You have not left Chapter 3 to ask that. You have restricted yourself to a single line of inputs, so there is only one variable left moving, and the slope of a one-variable function is a thing you already know how to compute.

Slice the hill with a vertical plane and an ordinary Chapter-3 curve falls out — the partial is just its slope.
x·east y·north the cut edge, as a flat graph ∂f/∂x = 3.00 height walk east → x this panel is Chapter 3. nothing else is happening.
Pick a way to face, then slide the plane to move the frozen value.
cut edge tilts up at slope 3.00
What you're looking at — a vertical plane cuts the hill, and the edge it exposes is an ordinary one-variable curve
the cyan plane freezes one variable; sliding it walks the frozen value from 0 to 4 — that is what "held constant" looks like
the blue cut edge on the hill is redrawn as the same blue curve in the panel: height versus how far you walk
the gold tangent is that curve's Chapter-3 slope — the partial derivative, printed. Face east → 3.00; face north → 4.00
aha — a partial derivative is not a new operator. It is the ordinary derivative you already own, taken along one axis with the others watching. The curly only announces which way you sliced.
Fig. 4. Slice the hill with a vertical plane and the cut edge is an ordinary curve; its slope is the partial derivative — 3.00 facing east, 4.00 facing north. The curly only names which way you sliced.

Watch what the panel does with that. It cuts the surface with a vertical plane running east to west, and where the plane meets the surface you get a curve. An ordinary curve, the kind Chapter 3 spent its whole length on. Slide the plane north and south and the curve changes shape, because you are slicing a different part of the hill. But at any fixed position it is just a curve, and its slope at your feet is just a derivative.

That number has a name and a slightly theatrical symbol. It is the partial derivative of f with respect to x, written ∂f/∂x, and the curly means nothing more than "there were other variables in the room, and I held them still." Flip the panel's toggle to slice the other way and you get ∂f/∂y, the slope facing north. Same operation, different plane.

Now for the step that people quietly refuse to believe. To compute ∂f/∂x you treat y as a constant, exactly as you would treat the 3 in 3x. It feels arbitrary. Who gave us permission to freeze a variable that is obviously still there?

Freeze it and see — is treating y as a constant a licence you were granted, or one you just took?
substitute first put the number in for y, THEN d/dx freeze, then differentiate d/dx with y held fixed, THEN put y in collapse to one variable needs a number then differentiate ↓ ? sum the surviving pieces 3 − 2x + y the rule, for any y 3 − 2x + y ?
set a value for y ↓ — it drives both lanes
Predict, before you switch: does the −y² term survive differentiating by x?
 
freeze gives the rule 3 − 2x + y
What you're looking at — one derivative, ∂f/∂x, computed two ways on f = 30 + 3x + 4y − x² − y² + xy
a live term keeps an x — it survives ∂/∂x
a constant has no x — it dies to 0
the answer both roads reach
identical → freezing y was legal, not invented
Fig. 5. Two roads to ∂f/∂x: substitute a number for y first and collapse f to a one-variable polynomial, or freeze y and differentiate first, then drop the number in. Pick any y — both roads land on the same line. Freezing isn't a licence you took; it's what the arithmetic already does.

The panel settles it by doing the experiment rather than arguing. Set y to the literal number 2 and substitute it everywhere in our hill. The 4y becomes 8, the −y² becomes −4, and the xy becomes 2x. Collect the terms and the whole surface has collapsed into

f(x, 2) = 34 + 5x − x²

There is nothing multivariable left. It is a one-variable polynomial, and Chapter 3's power rule differentiates it in one line to give 5 − 2x. Now do it the other way, keeping y as a symbol and treating it as a constant. The 3x gives 3, the 4y and −y² are constants so they give 0, the −x² gives −2x, and the xy gives y. So ∂f/∂x = 3 − 2x + y. Set y = 2 in that and you get 5 − 2x.

The same answer, by two roads. Freezing y was never a special rule you had to be granted. It is what the arithmetic already does when you walk along a line where y genuinely does not change. The other partial comes out the same way, and for our hill the pair is

∂f/∂x = 3 − 2x + y  ·  ∂f/∂y = 4 − 2y + x

At the trailhead, where x and y are both zero, those read 3 and 4. So the ground rises three metres per hundred metres east, and four metres per hundred metres north. Two honest numbers, both measured with tools you already had.

One notation note so nothing surprises you later. Because ∂f/∂x is itself a function of both variables, you can differentiate it again, and differentiating it with respect to y gives ∂²f/∂x∂y. For our hill that is 1, the coefficient on the xy term. It measures how much the east slope changes as you move north, and it is about to matter.

03★ Two slopes, added

Nobody walks due east. A real step goes diagonally, some east and some north at the same time, so we need to know what a diagonal step costs in height. The obvious guess is that you pay for each direction separately and add the bills:

Δheight ≈ (east slope × east step) + (north slope × north step)

Your gut should fight that, and the objection is a good one. The two directions are not independent on this hill. We put an xy term in on purpose, which means the north slope 4 − 2y + x genuinely depends on where you are east-west. Step east first and the northward climb you were quoted is no longer the climb you get. So why is adding two separately-measured effects allowed at all?

The missing 0.01 — add two frozen slopes, and meet the h² you pay for it
f(x,y) = 30 + 3x + 4y − x² − y² + xy · trailhead (0,0), step (h, h) UNDERSHOOT EXACT OVERSHOOT PREDICTED 3h + 4h ? TRUE f(h,h) − f(0,0) ? MISS true − predicted ? MISS ÷ h² ? pinned · the h² signature halve h → miss ÷ 4, ratio unmoved Where the missing 0.01 lives — step east, and the north slope tilts east step Δx=h 4.000 steeper north slope before 4.000 → after ? drift = ∂²f/∂x∂y × Δx = 1 × h = ? cost = 1 × h × h = ? (an h² term)
Predict first — no take-backs. Does 3h + 4h beat the true climb?
h = 0.100
Lock your call, then drag h.
What you're looking at — adding two frozen slopes, and the h² you must pay for it
PREDICTED = east-slope×step + north-slope×step = 3h+4h, the naive sum you bet on
TRUE = the real climb f(h,h)−f(0,0); it falls a hair short of the prediction
MISS = the missing 0.01 — MISS÷h² pins at −1.000, so it dies like h²: halve h, quarter the miss
coupling: stepping east tilts the north slope 4.000→4.100 — ∂²f/∂x∂y×Δx×Δy, an h² term the size of the miss
Fig. 6. Lock a guess — does adding the two frozen slopes overshoot, undershoot, or nail the true climb? Then shrink h and audit it. The prediction 0.70 always tops the true 0.69 by a whisker, yet the last column MISS÷h² refuses to leave −1.000: the error is second-order, an honest remainder — halve the step and it quarters. Step east and the north slope tilts 4.000→4.100: that coupling, ∂²f/∂x∂y×Δx×Δy, is exactly the h² term Ch. 4 taught us to drop. That is why the two slopes may simply be added — proven, not assumed.

Commit to a guess before you hit reveal, then let the panel run the numbers. Take a step of 0.1 east and 0.1 north from the trailhead. The prediction is 3(0.1) + 4(0.1) = 0.70 metres. The true height change, computed straight from the formula, is 0.69. So the recipe is wrong by 0.01, and the objection was real.

Now halve the step and watch the miss, because this is where the argument is won. At 0.05 each way the prediction is 0.35 and the truth is 0.3475. The miss went from 0.01 to 0.0025. You halved the step and the error fell to a quarter. That is the fingerprint of a term proportional to , and Chapter 4 taught us exactly what to do with those.

Here is where the missing 0.01 comes from, named precisely. The north slope at the trailhead is 4. After stepping 0.1 east, the north slope has become 4 − 0 + 0.1 = 4.1, because ∂²f/∂x∂y = 1. So the slope you were quoted drifted during the walk, by 1 × 0.1, and over a northward step of 0.1 that drift costs 1 × 0.1 × 0.1 = 0.01. The interaction is real, and it is exactly one in size.

So the addition is not an assumption and it is not a lie. It is a local linearization, correct to first order, with an honest second-order remainder that dies faster than the step itself. Chapter 4 already licensed this move for one variable. All we did is apply it once per axis.

Worth stating plainly so nobody over-claims it. This approximation is only good for small steps. Take a step of size 1 across this hill and the term is the same size as the linear one, and the prediction is worthless. Everything in the rest of this chapter takes small steps, and where an algorithm takes big ones it breaks in exactly this way.

Bundle the two bars — fx·Δx + fy·Δy is a dot product
↕ drag the tip to swing the step the trailhead · a contour map Δx Δy f_x·Δx + f_y·Δy 3·(0.130) + 4·(0.060) predicted Δf = 0.6300 ↑ this number never moves when you flip the notation Δf 0 true 0.720 +0.39 +0.24 stacked = predicted
Same sum, three costumes — flip the notation:
∇f·step = 0.6300 — just shorthand
What you're looking at — the sum of partial×step, drawn as two bars
blue bar = fx·Δx, the x-leg's contribution
green bar = fy·Δy, stacked on top
gold = their total, the predicted Δf = ∇f·step
cyan = the step (Δx,Δy) you swing on the map
so far this is only shorthand — packing the two partials into ∇f buys nothing yet. the payoff → why ∇f points steepest
Fig. 7. Swing the step arrow at the trailhead. Its horizontal Δx leg drives the blue bar (fx·Δx = 3·Δx) and its vertical Δy leg drives the green bar (fy·Δy = 4·Δy); stacked, their length is the predicted height change, the gold total ∇f·step — with the true change a muted tick just above (the bowl curves up, so the linear guess falls a hair short). Now flip the switch: 3·Δx + 4·Δy, then (3,4)·(Δx,Δy), then ∇f·step — three costumes, and the number never moves. At the tiny step (0.1, 0.1) all three read 0.7000. That is the whole point: fx·Δx + fy·Δy is not like a dot product, it is one, so naming the pair of partials "the gradient" is pure bookkeeping — honest shorthand, waiting to be cashed in.

Now look at the shape of what we wrote, because it is begging to be tidied. The expression f_x·Δx + f_y·Δy is a sum of products, taken slot by slot, between two pairs of numbers. Chapter 6 gave that operation a name and a symbol. It is the dot product of (f_x, f_y) with (Δx, Δy).

Drag the step arrow in that panel and watch the two contribution bars respond, then watch the dot-product readout agree with their sum. Nothing new is being claimed. We are writing the same arithmetic in fewer symbols. Give the first of those two vectors a name and the tidying is done:

∇f = (∂f/∂x, ∂f/∂y) — the gradient, the partials packed into one vector

At the trailhead our gradient is (3, 4). The whole linearization compresses to one line:

Δheight ≈ ∇f · step

Let me be honest about what has been earned so far. Bundling numbers into a vector and drawing an upside-down triangle on it is not yet an insight. It is bookkeeping. The steepest-direction question is where the bookkeeping pays, and it pays in something you can see.

04★★ The steepest way up

Here is the question the whole chapter turns on. You are standing at the trailhead and you may take one step of fixed length in any direction you like. Which direction gains you the most height?

Set it up with what you already hold. Write the direction as a unit vector û, meaning an arrow of length exactly one that only carries a heading. Then the height gained is ∇f · û by the line we just wrote. And Chapter 6 gave us a second way to read every dot product, in terms of the angle between the two arrows:

∇f · û = |∇f| |û| cos θ = |∇f| cos θ

Read the right-hand side slowly, because the answer is sitting in plain sight. The length |∇f| is fixed by where you are standing, and you cannot change it by choosing a direction. The length |û| is 1 by construction. So cos θ is the only thing in that expression you control.

The direction dial — climb = |∇f|·cosθ, and cosθ is the only part you can steer
|∇f| = 5 ∇f θ guess û click the rose ↻ Δheight for a unit step = ∇f · û 3·ûx + 4·ûy = +3.0000 5·cos(+53.1°) = +3.0000 ↓ same number at every θ, to 4 dp ✓ climb rate this heading +3.0000 −5 0 +5 5·cosθ across every heading θ θ=0 ⇒ peak 5 cosθ=1 · can’t beat it −180° +180°
You’re at a trailhead; the gradient is ∇f = (3,4), so |∇f| = 5 — fixed. Predict first: click the rose to guess the steepest way up, then spin û and watch.
Click the rose: guess steepest UP
Locked yet? Pick a heading to commit.
What you’re looking at — the steepest climb was never a rule; it’s just where cosθ hits 1.
∇f = (3,4) — fixed by where you stand, length |∇f| = 5
û — your unit heading; climb = ∇f·û = 5·cosθ
your pinned guess — and cosθ, the one movable part
θ=0: û along ∇f ⇒ climb = |∇f| = 5, the best there is
Fig. 8. — The climb for a unit step in direction û is ∇f·û = |∇f|·cosθ. Standing here fixes |∇f| = 5 and û has length 1, so cosθ is the only movable part — and the most a cosine can be is 1, at θ=0. So the steepest climb is simply straight along ∇f, at rate exactly |∇f| = 5. “The gradient points uphill” was never a rule to memorise — it’s a one‑line dot‑product fact wearing a disguise.

Predict before you touch the dial. Of every possible heading, which one makes |∇f| cos θ as large as it can be? Commit to an answer, then spin it.

Cosine is largest when the angle is zero. There is no cleverness in that, it is just what the cosine curve looks like. And θ = 0 means your step points in exactly the same direction as ∇f. So the steepest uphill direction is straight along the gradient, and the climb rate you get there is cos 0 = 1 times |∇f|, which is |∇f| itself.

That is the keystone of this chapter, and notice that it was never a rule to memorise. It is one substitution and one glance at a cosine.

∇f points the steepest way uphill, and |∇f| is that steepest slope.

Check it on our hill. The numbers here are unusually kind. At the trailhead ∇f = (3, 4), so |∇f| = √(9 + 16) = 5. The unit vector along it is (0.6, 0.8). Feed that in and the dot product is 3(0.6) + 4(0.8) = 1.8 + 3.2 = 5, matching the length exactly.

Now spin the dial elsewhere and watch the readout fall off. Point due east and you get (3,4)·(1,0) = 3, which is the east partial, as it must be. Point at (−0.8, 0.6), which is square across the gradient, and you get −2.4 + 2.4 = 0, so that heading gains no height at all. Point at (−0.6, −0.8) and you get −5, the fastest possible way down.

That last reading is the one this chapter runs on. The steepest descent direction is −∇f, for exactly the same reason and with no extra work. Cosine is smallest at 180°.

The general name for ∇f · û is the directional derivative, the rate of change along a chosen heading. The two partials are simply the special cases where û is due east or due north. So the partials were never the fundamental objects. They were two samples of a whole circle of slopes, and the gradient is the compact object that generates all of them.

05Where the arrow dies

Now read the keystone backwards, and an optimisation condition falls straight out of it. Suppose you are standing somewhere and ∇f is not the zero vector. Then it names a direction, and stepping that way gains height, so you cannot possibly be at the top. The only place with no direction left to climb is the place where the arrow has no length at all.

The arrow dies at the summit — a nonzero gradient always names a way uphill, so the only place with nowhere left to climb is where the arrow has no length at all.
0 x=6 y=6 a hill you cannot see — walk it by the arrow summit |∇f| = 5.00 the gradient ∇f ∂f/∂x = +3.00 ∂f/∂y = +4.00 arrow length |∇f| 5.00 still climbable → height f = 30.00 m the summit itself is 42.33 m
climbable · |∇f| = 5.00
∇f = 0 does not mean f = 0 — height here is 42.33 m
drag the blue walker on the map
What you're looking at — the same optimum, found two ways, read backwards from the keystone.
the walker — where you stand on the hill
the gradient arrow — points uphill, its length is the steepness |∇f|
the summit — the one place the arrow dies to zero (∇f=0), at height 42.33 m
the algebra — fₓ=0 and f_y=0 cross at exactly the point the walking found
Fig. 9. Read the optimum condition backwards from the keystone. A nonzero gradient always names a direction that gains height — so anywhere you can still climb is not the top. Drag the walker and watch the arrow shrink: |∇f| falls 5.00 → 2.24 → 1.00 → 0.00 as the summit arrives. The only place with nowhere left to climb is where the arrow has no length at all: ∇f = 0. Hit follow the arrow to walk uphill and see it starve; flip on algebra and the two lines fₓ=0, f_y=0 cross at the very same point. That is why ∇f = 0 is the optimum condition — no new idea, just the keystone turned around. (And ∇f=0 does not mean f=0: the height there is a healthy 42.33 m.)

Drag the marker across the contour map in that panel and watch the arrow shrink as you climb. At the trailhead it reads |∇f| = 5.00. At (2, 2) the gradient is (1, 2) and the length has fallen to 2.24. At (3, 3) it is (0, 1) with length 1.00. Keep going and at (3.33, 3.67) the arrow vanishes and the height reads 42.33 metres. That is the summit.

Solve for it directly and it agrees. Set both partials to zero, so 3 − 2x + y = 0 and 4 − 2y + x = 0, and the pair gives x = 10/3 and y = 11/3. A point where the gradient is the zero vector is called a stationary point, and the condition is written the way you will see it everywhere from here on:

∇f = 0 — the optimum condition

Two traps worth naming while we are here. First, ∇f = 0 does not mean f = 0. Our summit has a gradient of zero and a height of 42.33 metres, and those are different quantities entirely. Second, ∇f = 0 is a vector equation, so it is really n separate equations, one per input. For a model with a million parameters that is a million equations at once.

The mountain pass — where ∇g = 0 yet one way climbs and the other falls
the surface x²−y², seen from above E climbs N falls drag me ✕ ∇g = ( 2x , −2y ) = ( +2.60 , −1.40 ) height felt as you walk out high low t → the same shape in the world the pass
∇g = 0 at centre — a flat spot
What you’re looking at — a dead gradient that is still not an optimum
the surface x²−y² from above — its contour hyperbolas
∇g = (2x, −2y), the uphill arrow — it starves to (0,0) at the centre
walk E or W and it CLIMBS (+t²) — the centre is a valley floor
walk N or S and it FALLS (−t²) — a hilltop; the 45° lines stay flat
So ∇g = 0 is a candidate, never a verdict. In a big model most stationary points are saddles like this, not minima.
Fig. 10. The surface is g(x,y) = x²−y², drawn from above as contour hyperbolas: the pale green lobes east and west are high ground, the pale red lobes north and south are low ground, and the two dashed 45° diagonals are exactly sea level. Drag the point to the middle and the gold gradient arrow — which always points straight uphill and whose length is the steepness — shrinks to nothing: ∇g = (2x, −2y) = (0,0), the identical reading you got at a true summit. Now aim a heading and press walk out. Head East or West and the ground climbs as +t²; head North or South and it falls as −t²; along the diagonals it stays dead flat. The centre is the bottom of one journey and the top of another at the same time — a mountain pass. So a vanishing gradient is a candidate, not a verdict. In one variable this trap is rare; in the many variables a real model lives in, most stationary points are saddles exactly like this one.

And now the exception, because ∇f = 0 is a candidate rather than a verdict. Look at the surface g(x, y) = x² − y² in that panel. Its gradient is (2x, −2y), which is dead zero at the origin, so the origin passes the test. Walk east from there and the ground rises like a parabola. Walk north and it falls like an upside-down one.

That is a saddle point, and the everyday version of it is a mountain pass. From the pass you go up toward either peak and down toward either valley, and you are at the bottom of one journey and the top of another at the same time. In one variable this barely happens. In many variables it is the normal state of affairs, and in a large neural network the overwhelming majority of stationary points are saddles rather than minima.

So the gradient alone cannot finish the job. It tells you where the ground is flat. It cannot tell you which way the ground is curving, and curvature is a second derivative.

Three curvature signs — the Hessian is Chapter 7’s positive-definite test wearing a new hat
the surface from above — contour lines λ₁ λ₂ height felt as you walk this heading walk → high low H = ∇²f — matrix of second derivatives 4 2 2 4 λ₁ = +6 along (1,1) λ₂ = +2 along (1,−1) → MINIMUM
climbs ↑  κ = +6.0  (= λ₁)
Ch 7: all λ > 0 → positive-definite → curves up every way
What you’re looking at — ∇f = 0 can’t tell a bowl from a dome from a saddle; the Hessian’s eigenvalues can
the contour map of the chosen surface — the two dashed eigenvector axes are the purest-curvature headings
spin the heading θ: κ(θ) = ûᵀHû is the curvature that way, and it equals an eigenvalue when you aim along an eigen-axis
the track cups up (green) where the walk climbs — a valley floor direction (κ > 0)
it caps down (red) where the walk falls (κ < 0) — on the saddle, one axis cups and the other caps
That’s Chapter 7 in a new costume. “Curves up in every direction” is exactly “all eigenvalues of H are positive” — the positive-definite condition, unchanged. Same matrix, read as curvature instead of stretching. (Our hill: the eigen-axes are the contour ellipse’s long & short axes.)
Fig. 11. A flat gradient (∇f = 0) marks a candidate, but it can’t tell a bowl (minimum) from a dome (maximum) from a saddle. The second derivatives can — and they are packed into one matrix, the Hessian H = ∇²f. Pick a surface and spin the heading dial: the curvature that way is κ(θ) = ûᵀHû, and the track cups green where the walk climbs, caps red where it falls. Aim along an eigenvector and κ lands exactly on an eigenvalue — those two diagonals are the headings of purest curvature. On the bowl every heading cups: all eigenvalues positive. That is word-for-word Chapter 7’s positive-definite condition — the same matrices that “push every direction outward,” now read as “curve upward in every direction.” A minimum is a bowl; a bowl is positive-definite; positive-definite is Chapter 7. Same fact, new costume.

Chapter 4 already handled this in one variable. If f′(a) = 0 and f″(a) > 0 the curve is cup-shaped and you are at a minimum, and if f″(a) < 0 it is a cap and you are at a maximum. We need the same test with more directions available, so collect every second partial into a square grid:

H = [ f_xx   f_xy ;   f_yx   f_yy ] — the Hessian, the matrix of second partials

This is where the definition becomes a picture, and it is Chapter 7 in a new costume. A minimum needs the surface to curve upward in every direction, not just along the two axes. Chapter 7 told us exactly which matrices push every direction outward. Those are the positive-definite ones, the ones whose eigenvalues are all positive.

all eigenvalues of H > 0 ⟹ bowl, a minimum  ·  all < 0 ⟹ dome, a maximum  ·  mixed signs ⟹ a saddle

Step the three cards in that panel and check each one against the surface it draws. The mixed case is the most instructive, so take f = 2xy, whose Hessian is [0  2 ;  2  0] with eigenvalues +2 and −2 along (1,1) and (1,−1). Walk the (1,1) diagonal and the height goes as +2t², climbing. Walk the (1,−1) diagonal and it goes as −2t², falling. The eigenvectors told you where to look and the signs told you what you would find.

Our own hill closes the loop nicely. Its Hessian is [−2  1 ;  1  −2], with eigenvalues −1 and −3 along (1,1) and (1,−1). Both negative, so the summit really is a maximum, which the walking confirmed. And those same eigen-directions are the long and short axes of every contour ellipse on the map, which is why the rings came out tilted.

Drop the marbles — on a convex bowl, local IS global; on a bumpy one it is not
a convex bowl a bumpy surface click a hill ▾ drops one marble on BOTH the one flat spot   global min       drag ◂▸ the chord — does the surface stay below it? one dip ⇒ a local min IS the global min stationary point ends the search four dips ⇒ you found A min, not THE min honesty = say which
Drop the same marbles on both, release, and watch the bowl agree while the bumpy surface splits.
drop a few marbles, then release
What you’re looking at — the same marbles, two fates
the surface (a height profile) — roll each marble along −∇f, straight downhill
marbles rolling — each stops where its local gradient dies
the global minimum — on the bowl every marble finds it; on the bumps most miss
trapped in a shallower well — a true local min that is not the answer
The lamp under each surface is Ch 4’s convexity test, unchanged: a convex surface stays below every chord, so it has exactly one dip. That is why quants engineer convex objectives on purpose — mean–variance and ridge are convex BY DESIGN, so a stationary point ends the search.
Fig. 12. Two height profiles, one release button. Click either hill and the same marble drops onto both surfaces; press release and each rolls straight downhill along −∇f until its local gradient dies, leaving a cyan trail. On the convex bowl there is only one dip, so every marble — wherever it started — ends at the same flat spot: the counter reads distinct rests: 1, and that one local minimum is the global one. On the bumpy surface the four wells have different depths; marbles settle into whichever basin they happened to start in, the counter climbs toward 4, and only the ones that reach the gold deepest well found the true answer — the rest are trapped. Hit scatter 50 to make the statistic brutal. Underneath, drag the dashed chord on each surface: the lamp asks Ch 4’s convexity question unchanged — does the surface stay below the chord? It is always green on the bowl and flickers red on the bumps. That is the whole reason quants build convex objectives on purpose: mean–variance and ridge are convex by design, so setting one gradient to zero ends the search — and when a surface is not convex, honesty means saying you found a local answer, not the answer.

One more property earns its own beat, because it explains a habit that runs through the rest of this course. A surface that curves upward everywhere, at every point rather than just at one, is called convex. Chapter 4 defined that for a curve and the meaning is unchanged. It is a bowl.

Drop marbles from random starting points in that panel and compare the two landscapes. On the convex bowl every marble arrives at the same place, because there is only one flat spot for it to find. On the bumpy surface beside it the marbles scatter into different dents, and each one stops somewhere that is lowest only in its own neighbourhood.

That is the gift, and it is worth stating as a sentence you can carry:

On a convex surface, a local minimum is the global minimum.

Which is why quants engineer their objectives to be convex whenever they possibly can. Mean-variance portfolio optimisation is convex, and so is ridge regression, and both were designed that way rather than discovered that way. When the surface is convex, finding one stationary point ends the search. When it is not, you have found a local answer and you should say so out loud.

06Walking down the hill

We have a condition for the answer and a test for whether it is the right kind. What we do not have is a way to actually solve ∇f = 0 when the problem is real.

On our hill it was two linear equations and a minute of algebra. Now count what a neural network asks for. Millions of parameters, so millions of simultaneous equations, all of them nonlinear because of the activation functions stacked between the layers. There is no closed form. Nobody is solving that system, ever.

So stop trying to solve it and use what the keystone already gives you for free. You may not know where the bottom is, but at any point you are standing on, the gradient tells you which way is down. Take a small step that way. Then look again and repeat.

w ← w − η ∇f(w)gradient descent, with η the learning rate

The minus sign is the keystone doing its work, since −∇f is the steepest way down. The η is yours to choose. It says how far to trust a slope measured at a single point before you go and measure again.

The descent loop, executed — and the learning rate that blows it up
stdout — the real run i w1 w2 L (loss) contour bowl — L(w) = w1² + 4·w2² min · ∇L = 0
this loop actually runs ↓
w = [2.0, 2.0]
eta = 0.10
for i in range(8):
  L = w[0]**2 + 4*w[1]**2
  print(i, w[0], w[1], L)
  g = [2*w[0], 8*w[1]]  # ∇L
  w[0] -= eta*g[0]
  w[1] -= eta*g[1]
loss falls 20 → 3.2 → 1.66 → … ✓
What you're looking at — a real run, not a claim: every number left came out of the loop above
the loss L — the number to watch; it should fall each pass
the path = the printed iterates, plotted on the bowl
the lamp reddens the first pass the loss rises
the contours — the elongated bowl you're walking down

∇L = 0 is one tidy equation here — millions of tangled ones in a real model, with no formula to solve. So nobody solves it. The loop never solves anything: it asks which way is down? and steps. At η = 0.10 the loss falls 20 → 3.2 → 1.664 → 1.0496. Push η to 0.30 and the very same loop prints a loss that grows without bound — that's the goldilocks knob, and too big diverges.

Fig. 13. Gradient descent, executed: the loop on the right actually runs, and the trace on the left is its real stdout — plotted as a path on the bowl. At η = 0.10 the loss falls 20 → 3.2 → 1.664 → 1.0496; drag η to 0.30 and the same loop prints a loss that grows without bound. Nobody solves ∇L = 0 — the loop just walks downhill.

That panel is a genuine CodeRun. The loop you can read is the loop that executes, and the trace printed underneath is its real output rather than a drawing of one. It minimises L(w₁, w₂) = w₁² + 4w₂², an elongated bowl, starting from (2, 2) where the loss is 20.

Run it at η = 0.1 and follow the numbers. The gradient at the start is (4, 16), so the first step lands on (1.6, 0.4) and the loss prints 3.2. The next two iterations print 1.664 and 1.0496. The loop never solved anything. It just kept asking which way was down and going there.

Now push the learning-rate slider up and break it deliberately, because the failure teaches more than the success. At η = 0.3 the w₂ coordinate overshoots the valley floor and lands further out on the far wall, so the next step overshoots harder. The printed loss grows without bound. Too small a rate fails the other way, quietly. The loop converges, but it crawls, and you pay for every wasted iteration in compute.

A lock worth carrying, because this pattern shows up far outside finance. A ball rolling in a bowl with friction is doing the same thing physically, since gravity supplies −∇f and the ball has no idea where the bottom is either. A PID controller in a robot is the same shape again, measuring an error and feeding a correction proportional to it. Measure the local error, move against it, repeat.

One thing is still missing before this is usable on a real model. Our loss was written directly in terms of the two weights, so the gradient was easy to write down. A real model is built in layers, where the loss depends on the output, which depends on the last layer, which depends on the one before it. How does a slope travel back through a stack like that?

Two roads to 8 — derive the chain rule, check it twice, then run it backwards as backprop.
Divide the linear approximation by Δt contours of the hill f, seen from above ∇f v Δf ≈ fx·Δx + fy·Δy ↓ divide every term by Δt Δf/Δt ≈ fx(Δx/Δt) + fy(Δy/Δt) ↓ let Δt → 0 (Ch3’s slope) df/dt = fx(dx/dt) + fy(dy/dt) = ∇f · velocity ▲ that ‘+’ means routes ADD — beginners drop it on the path  x = t,  y = t²  ·  check df/dt at  t = 1 ROAD A · chain rule at the point (1, 1) ∇f = (2, 3) velocity = (1, 2) df/dt = 2·1 + 3·2 8 ROAD B · substitute first f(t, t²) = 30+3t+3t²+t³−t⁴ g′(t) = 3+6t+3t²−4t³ at t=1:  3 + 6 + 3 − 4 8 IDENTICAL ✓ both roads print 8 exactly — nothing was assumed x = 3,  y = 5  ·  forward: ŷ = w·x + b,  L = (ŷ − y)² x=3 y=5 dL/dŷ = 2(ŷ−y) = −4 dL/dw = −4·3 = −12 dL/db = −4·1 = −4 dL/dŷ cached, reused ×2 w = 1 b = 0 ŷ = 3 L = 4 ⏴ tap, from the loss
Watch the walker. Divide Δf ≈ fxΔx + fyΔy by Δt and let it shrink — the chain rule falls out.
df/dt = ∇f · velocity
What you're looking at — the chain rule derived, checked twice, then run backwards as backprop
the hill f(x,y) as contours from above; fx, fy are Ch3’s slope taken one axis at a time
∇f = (fx, fy), the gradient — and every number to watch (both roads print 8)
velocity = (dx/dt, dy/dt); forward flow. Backprop sends the slopes back the other way
IDENTICAL — nothing assumed; one descent step drops the loss 4 → 2.56
aha — divide the linear approximation by Δt and shrink: df/dt = ∇f·velocity. Backprop is that rule swept from the loss with the shared dL/dŷ cached. The ‘+’ means contributions ADD — the term beginners drop.
Fig. 14. Two roads to 8. Divide the linear approximation Δf ≈ fxΔx + fyΔy by Δt and the multivariable chain rule falls out: df/dt = ∇f·velocity. On the path (t, t²) the chain rule and substitute-first both print 8 exactly — nothing was assumed. Sweep that rule backwards from the loss through a tiny graph, caching the shared dL/dŷ, and you have backpropagation with the mystique removed; one descent step drops the loss 4 → 2.56.

Step the derivation in that panel and notice that no new machinery arrives. Suppose you walk a path across the hill, so your position (x(t), y(t)) depends on time. Take the linearization we already proved, Δf ≈ ∇f·Δposition, divide both sides by Δt, and let Δt shrink. What comes out is the multivariable chain rule:

df/dt = ∇f · velocity = f_x·(dx/dt) + f_y·(dy/dt)

Read that in words and it is completely reasonable. The rate the height changes is how fast you are moving east times the east slope, plus how fast you are moving north times the north slope. Every route into f contributes, and the contributions add. That addition is the single thing beginners drop, and it is the whole difference from the one-variable chain rule.

The second beat of that panel checks it rather than asserting it. Walk the curve x = t, y = t², and look at time t = 1, which puts you at the point (1, 1). There the gradient is (2, 3) and the velocity is (1, 2), so the chain rule says 2(1) + 3(2) = 8. Now take the other road. Substitute the path into f first, which gives 30 + 3t + 3t² + t³ − t⁴, differentiate that with Chapter 3's power rule to get 3 + 6t + 3t² − 4t³, and evaluate at t = 1. That reads 3 + 6 + 3 − 4 = 8. Identical.

Now flip the panel's last beat and run the same rule backwards through two layers. A loss depends on a prediction. The prediction depends on a weight. So the loss's sensitivity to that weight is the product of the local slopes along the chain. When a weight feeds several places, you sum over the paths, exactly as the two terms above are summed. Sweep that computation from the loss end back toward the inputs and you have backpropagation.

Which is worth saying flatly, because the word carries far more mystique than the mechanism deserves. Backprop is the chain rule, run backwards, with the shared work cached instead of recomputed. That is the entire idea. Chapter 23 will use the same move to explain gradient boosting, where each new tree is fitted to the residual because the residual is the negative gradient of squared error.

07★ When a fence is in the way

Everything so far assumed you may step wherever the arrow points. Real problems almost never allow that. Portfolio weights must sum to one because you invest all your capital and no more, a risk budget caps your exposure, and a position limit is a hard wall. The optimum you actually want is the best point on the allowed set, which is usually nowhere near the free one.

Before we can handle a fence, we need one geometric fact about the gradient, and it is one line of work. Take a step along a contour line. By the definition of a contour, the height does not change, so the directional derivative in that direction is zero.

∇f · (tangent to the contour) = 0 ⟹ the gradient is perpendicular to the contour

Walk along a ring — your altitude never moves, and the gradient stays square to the curve.
basin — lowest point drag me ⟲ ∇f walk 90.0° ∇f = (3, 4) · tangent = (−0.8, 0.6) dot = −2.4 + 2.4 = 0.0000
drag the dot around a ring · then make it rain
altitude on this ring1.9600
∇f · tangent0.0000
angle(∇f, ring)90.0°
∇f=(1.44, 2.15) · t=(−0.83, 0.56)
walk anywhere — still square, 90.0°
Why the gradient meets every contour at a right angle — it costs one line
walk a ring and your altitude never changes — so the change along it is 0
that change is the dot product ∇f·(tangent): the rate of climb in the walking direction, and it reads 0.0000
a zero dot product is perpendicular → the gradient ⊥ contour. Rain has drawn this on hillsides forever.
Fig. 15. Grab the marker and slide it around a ring: the altitude stays frozen to four decimals and ∇f·tangent holds at 0.0000 — you are moving, gaining no height, so your rate of climb in the walking direction is zero. A zero dot product means perpendicular, so the gradient reads 90.0° to the ring everywhere you drag. Hit make it rain: droplets run downhill along −∇f and paint tracks that cross every ring dead square — exactly how water finds a hillside. That single line — height constant → change along it 0 → dot 0 → perpendicular — is the rung Lagrange stands on.

Turn on the arrow field in that panel and look at how the arrows meet the rings. Every one crosses at a right angle. Everywhere on the map, no exceptions. You have seen this in the physical world without naming it. Rain landing on a hillside runs straight downhill, and its track crosses each contour line square on, because water takes the steepest way down.

Check the arithmetic at the trailhead once so it is not just a picture. The gradient is (3, 4), and the contour through that point runs along (−0.8, 0.6). Their dot product is −2.4 + 2.4 = 0. Perpendicular, exactly.

This rung is the one most treatments skip, and skipping it is precisely why the next result feels like a magic trick when you meet it elsewhere. We are not going to skip it.

Slide to tangency — walk the budget line and watch where ∇f = λ∇g is born
0 5 10 x 10 y x + y = 10 (budget) ② the two gradients at the marker press “show ∇f, ∇g” ▸
① Lock where you think the best split sits, then slide the marker along the line. You keep improving while you keep crossing contours.
STILL IMPROVING — keep sliding
x = 3.0 y = 7.0 · xy = 21.0
lock a guess, then slide →
What you're looking at — where ∇f = λ∇g is born, by walking
blue = contours of f = xy (levels of the thing you maximise)
cyan = the budget line x+y=10, i.e. the constraint g; ∇g=(1,1)
gold = ∇f and the contour that just grazes the line at (5,5)
red = you can still improve — one way along the line beats here
Solve it: ∇f=λ∇g means (y,x)=λ(1,1), so y=λ and x=λ → x=y. With x+y=10 → x=y=5, f=25, λ=5. λ is just the price of the constraint: parallel arrows need not be equal length, and λ=5 is how much longer ∇f is.
Same shape in finance → Markowitz minimises wᵀΣw subject to Σw=1: bigger objective, identical calculus (why Σ is the covariance is earned in Ch18).
Fig. 16. Pinned to the budget line x + y = 10, you can still do better as long as sliding keeps crossing contours of f = xy — so lock a guess, then slide and watch the crossing-counter tick. You halt exactly where the line grazes a contour: at (5, 5) the level xy = 25 touches the line at a single point and the two crossing dots merge. Turn on the gradients: ∇f = (y, x) and ∇g = (1, 1) line up only there — off the optimum they point different ways, so parallelism is something you watch arrive. And since parallel arrows needn't be equal length, ∇f = λ∇g with λ = 5 — the price of the constraint, the very same machine that finds a minimum-variance portfolio.

Now the constrained problem, with numbers small enough to check by hand. You have 10 units of capital to split between two desks, so x + y = 10. Their combined output is f = xy, because each desk amplifies the other. The contours of xy are hyperbolas, one for each output level, and the constraint is a straight line across them.

Drag the marker along that line and watch the output readout. As long as you are crossing contours, the output is still changing, so one of the two directions along the line makes it better and you are not done. Keep sliding. The moment you stop improving is the moment the line stops crossing contours and merely grazes one.

That word is the whole method. At the constrained optimum, the constraint is tangent to a contour of the objective. And two curves that are tangent at a point share the same perpendicular direction there. We just proved that each curve's perpendicular is its own gradient. So at the optimum the two gradients must lie along the same line:

∇f = λ ∇g — the Lagrange condition, with λ the multiplier

The λ is there for a plain reason rather than a mysterious one. Two gradients point the same way. They have no reason at all to be the same length, so a scaling number is needed to turn a parallelism into an equation.

Solve our case and see how little work it is. Here ∇f = (y, x) and ∇g = (1, 1), so the condition gives y = λ and x = λ, which forces x = y. Feed that into the constraint and you get x = y = 5, with output 25 and λ = 5. The tangent hyperbola is xy = 25, touching the budget line at exactly one point.

best value V(c) = c²/4 — maximise xy s.t. x+y = c 0 48 c → V slope = λ = 5.00 budget c = 10.0 optimum (5.0, 5.0) best value c²/4 25.00 λ = c/2 (shadow price) 5.00 = slope of curve above = gain per +1 of budget
the audit: loosen c → is the gain really λ·Δc?
new best value V(c+Δc)25.5025
actual gain0.5025
predicted λ·Δc0.5000
mismatch = (Δc)²/40.0025
the price is right — off only by second order
What you're looking at — λ is the slope of the value curve, i.e. the price of one more unit of budget
gold curve = the best value V(c)=c²/4 you can reach with budget c
violet tangent = λ=c/2, its slope; loosen c by 1, value rises by λ
red mismatch = (Δc)²/4 — the same 2nd-order term we drop in linearization

Two voices for the same number: a risk manager reads λ as the value of one more unit of risk budget; a desk reads it as what an extra unit of capital is worth — before anyone quotes a rate. Shrink Δc and the mismatch vanishes, so the price becomes exact in the limit.

Fig. 17. The multiplier is not scaffolding — it is a price. Dial the budget c and three readouts move as one: the optimum (c/2, c/2), the best value c²/4, and λ = c/2, which is exactly the slope of the value curve — so λ is the derivative of the optimum with respect to the budget. Then run the audit at c = 10: loosen by Δc = 0.1 and the value climbs 25 → 25.5025, an actual gain of 0.5025 against a predicted λ·Δc = 0.5000. The 0.0025 mismatch is (Δc)²/4 — the same second-order term we drop in linearization. Shrink Δc to 0.01 and it collapses to 0.000025: the price becomes exact in the limit.

And λ is not scaffolding to be thrown away after solving. It has a meaning, and in finance it is the most useful number in the whole calculation. Drag the budget in that panel and watch both the answer and λ move together.

Loosen the budget from 10 to 10.1. The new optimum is (5.05, 5.05) and the best output rises from 25 to 25.5025. So one tenth of extra capital bought you 0.5025 of extra output, against a prediction of λ × 0.1 = 0.5. The 0.0025 mismatch is second order in the loosening, which is the same we met in the missing-0.01 audit.

So λ is a price. It is how much the best achievable value improves per unit of constraint relaxed, which is why it is called the shadow price of the constraint. A risk manager reads it as the value of one more unit of risk budget. A desk reads it as what an extra unit of capital is worth before anyone quotes a rate.

The wall you may or may not be leaning on — drag the limit, watch the multiplier switch on
the plane — minimise (x−3)² + (y−1)² x y allowed x + y ≤ 6.0 ∇f ∇g ∇f ∥ ∇g free min (3,1) optimum the price — V vs the limit c 2 0 c=2 4 6 cyan slope of V = −μ flat ⇒ μ=0 (slack)· tilt ⇒ μ>0 (binding)
optimum  (3.00, 1.00)
objective  f = 0.00
multiplier  μ = 0.00
— run the check on card B —
slack — delete the constraint
What you're looking at — a ≤ constraint is either slack (delete it) or binding (an equality Lagrange solves)
blue = the objective f: contour rings, free min, ∇f
green = the allowed region x+y ≤ c
gold = the wall, the optimum on it, the value curve V
violet = ∇g, the wall's normal — parallel to ∇f when it binds
Fig. 18. The same problem, minimise (x−3)²+(y−1)², whose free minimum sits at (3,1). Drag the limit c. High up (card A, ≤6) the free min is already legal — the whole neighbourhood shades green, the wall greys out, the multiplier reads 0.00: delete the constraint and solve free. Slide c below 4 and the free min leaves the allowed region; the answer is pushed onto the wall, the two gradients line up (∇f ∥ ∇g), and the multiplier switches on. On the value curve the flat stretch (slack, μ=0) kinks into a slope (binding, μ>0) at exactly c=4 — that is complementary slackness in one motion. The shadow-price check loosens the wall 2→2.1 and watches the objective fall 2.000→1.805, a gain of 0.195 against the price μ×0.1 = 0.200: μ is what one more unit of room is worth. That is the whole of KKT: either the multiplier is zero, or the wall is tight, never both. Chapter 30 carries one of these per asset in a long-only portfolio.

One last case and then we are done with constraints. Most real limits are inequalities rather than equalities, since a position limit says at most this much, not exactly this much. The single question is when such a constraint counts.

Picture a wall you might or might not be leaning on. Toggle the two cards in that panel and both cases are visible at once. Minimise (x−3)² + (y−1)², whose free minimum sits at (3, 1). Under the limit x + y ≤ 6 that free minimum is already legal, so the wall does nothing and you may delete it from the problem. Its multiplier is 0.

Now tighten the limit to x + y ≤ 2. The free minimum is outside the allowed region, so you are pressed flat against the wall and the best legal point is (2, 0). There the constraint is tight, it behaves exactly like the equality we already solved, and its multiplier is nonzero. Loosen it to 2.1 and the minimum falls from 2 to 1.805, which is the same shadow-price reading as before.

That two-case split is the whole content of the KKT conditions, named for Karush, Kuhn and Tucker. The bookkeeping line that encodes it is complementary slackness, and it says only this: either the multiplier is zero, or the constraint is tight. Never both nonzero at once. We meet the full machinery in Chapter 30, where a long-only portfolio has one of these per asset.

08The accumulation twin

The derivative side is finished. One loose end left. Chapter 3 taught us that the derivative has a twin: the integral accumulates area under a curve rather than measuring slope. Two inputs must have a twin too.

And it does. Better still, we already own the move that builds it. Under a curve you accumulate area. Under a surface you accumulate volume, the amount of space between the flat input plane and the sheet floating above it.

Sweep the slice — freeze a variable, integrate, and watch slice areas stack into a volume
slice: freeze x = 1.00 f=2 1 0 0 1 integrate over y → A = 1.500 slice area volume 1.000 of 1.000 input square 0 x=1 0 y=1 sweep x = 1.00 Same freeze, two directions — the move behind the partial derivative: freeze y → ∂f/∂x = slope (Ch 3) same freeze slope ↔ area freeze x → ∫f dy = area (Ch 3)
volume 1.000 — same either order
every slice is one Ch-3 area; stacking them is the second integral
What you're looking at — a volume built one slice at a time
the slice — freeze one variable and f(x,y) collapses to a plain Ch-3 curve; its shaded area is one number (0.5→1.5)
the running volume — stack those slice areas as you sweep; it lands on exactly 1.000 (or 0.250 for x·y)
the sweeping plane — the frozen value marching 0→1 across the input square

That double stack IS the double integral — and it is partial differentiation run backwards: freeze a variable to take a slope, or freeze it to take an area, one move pointed two ways. Geometry only here — volume, not chance. Ch 11 turns this sheet into a probability density.

Fig. 19. Drag sweep and a vertical plane crosses the unit square under f = x + y. At each frozen position the surface becomes a plain Chapter-3 curve; its shaded area runs 0.5→1.5, and the running-volume bar stacks those areas, landing on exactly 1.000 at the far edge. Flip freeze xfreeze y: you stack the slices the other way, same total. That is the double integral — and, as the mirror shows, partial differentiation run backwards. (Switch to x·y to check the machine on a case that lands on 0.250, not 1.)

Here is the machine, and it is partial differentiation run backwards. Freeze x at some value. What sits above that frozen line is an ordinary one-variable curve, exactly as it was when we sliced for a partial derivative, and Chapter 3 already tells you the area under it. That area is one thin slice of the volume. Now sweep the frozen line across and add up every slice.

Sweep the slice in that panel and watch the running volume fill in beneath it. Take f(x, y) = x + y over the unit square, meaning both inputs run from 0 to 1. Freeze x and integrate over y, giving a slice area of x + ½. The readout runs from 0.5 at the left edge to 1.5 at the right.

Then integrate those slice areas across x, which gives ½ + ½ = 1. The volume under that sheet is exactly one cubic unit. On paper, the two integral signs are just the two passes:

∬ f dA = ∫ ( ∫ f dy ) dx — the double integral, computed by iteration

Flip the panel's order switch and slice the other way, freezing y first. Every slice is a different shape and the running total climbs differently, but the final volume is 1 again. Which makes sense, since the volume under a fixed sheet cannot depend on which way you chose to chop it.

An honest flag on where this leads, and on what it is not yet. In Chapter 11 the surface becomes a joint probability density, and the volume over a region becomes the probability of landing in that region. This exact machine is how E[X] gets defined for continuous variables. That sentence needs probability, which we have not built. So here it is pure geometry, and the geometry is already complete.

The whole chapter as one instrument — tap a move to unfold its derivation; flip FORWARD to see the model it becomes.
the keystone ∇f·û = |∇f|cosθ DESCENT step −∇f OPTIMUM set ∇f=0 CURVATURE eigen-signs CONVEX local=global CONSTRAINED ∇f = λ∇g ⊥ CONTOUR ∇f ⊥ set sideways: ∫∫ → E[X] · Ch11 PART 0 ✓ 1 2 3 4 5 6 7 8 9 Chapter 9 ▸
tap a spoke ▸ greyed-out = what its derivation never touches
∇f·û = |∇f|cosθ — the one arrow
tap a spoke to unfold its derivation
The aha — OLS, boosting & Markowitz are ONE template in three costumes
Keystone ∇f·û=|∇f|cosθ: every move just reads this one arrow.
Six moves: tap one — the greying shows how short its derivation really is.
FORWARD: the same move as a real model — OLS, boosting, Markowitz, Greeks, E[X].
Part 0 done: eight foundations lit, one arrow into Ch 9.
Fig. 20. Point it at an optimisation you have never seen: name the surface, find where the gradient dies, check the curvature so the flat spot is really the bottom, and attach a multiplier (λ = the price) for every budget that binds.

So here is the chapter as one image you can carry out the door. A function of many inputs is a landscape. Its partial derivatives are ordinary slopes taken one axis at a time, and packing them into the gradient turns the local height change into a single dot product. That dot product forces the keystone, since |∇f|cosθ is largest when you walk along the arrow. Read it forward and you get descent. Read it as an equation and you get ∇f = 0. Read it sideways and you get perpendicularity to the contours, which is Lagrange. The Hessian's eigenvalue signs tell you whether the flat spot you found is a bowl, and if the surface is convex then that one flat spot is the whole answer.

You now hold a template that fits an optimisation you have never seen. Name the surface, find where the gradient dies or walk downhill until it does, check the curvature so the flat spot is really the bottom, and attach a multiplier for every budget that binds. Chapter 20 runs it on squared error and out come the normal equations. Chapter 23 runs it on residuals and out comes boosting. Chapter 30 runs it on portfolio variance with weights summing to one and out comes the efficient frontier.

Part 0 ends here. We have spent eight chapters building the algebra, the series, the calculus and the linear algebra that the field quietly assumes, and the toolbox is now genuinely complete. Chapter 9 points all of it at the target it was built for. We start putting numbers on uncertain events, from three axioms up, and the first thing probability will ask of you is a function defined on sets.

iolinked.com
Written by Ajai Raj