When Yields Do Not Move Together
The DV01 hedge protects against one scenario; if the 10-year and the 2-year yield move by different amounts, the book gains or loses about its DV01 times the difference.
The question
Lesson 4 tested the hedge with one shift for both yields. A 10-year yield and a 2-year yield have no reason to move by the same amount. What does the hedged book earn or lose when they move differently? In this lesson each bond has its own yield and its own move, and the profit of the book is again the change in the value of A minus the change in the value of the face of B that was sold. All the moves are made-up scenarios, not forecasts.
Work it by hand
To first order the book is hedged against a move that is the same for both, so what remains is the DV01 times the difference of the two moves in basis points. For bond A of the fixed example the DV01 is 772.17349 (772.17 to the cent). Let the yield of A rise by 20 basis points and that of B by 30. The difference is 10, so the first-order estimate of the profit is 772.17349 times 10, which is 7,721.73.
The engine reprices at 7,771.44. Turn it round: A moves 30 and B moves 20, and the first-order estimate is -7,721.73, while the engine gives -7,432.07. The first-order rule gets the sign and the size roughly right and misses the curvature of lesson 5. A bond hedged against equal moves is exposed to the difference of the moves.
Run it
The program holds bond A of 1,000,000 face (5% coupon paid once a year, 10 years, yield 5% a year) against the sold hedge face from the DV01 ratio of bond B (4% coupon, 2 years, yield 4%). Lists give four scenarios: long_bp is the move of the yield of A and short_bp that of B, in basis points.

Watch the steps (recorded video)
market.set_date("2025-01-02");
let a = instrument.bond(1000000.0, coupon_a, float(years_a), 1);
let b = instrument.bond(1000000.0, coupon_b, float(years_b), 1);
let hedge = risk.bond_dv01_yield(a, yield_a) / risk.bond_dv01_yield(b, yield_b);
hedge = hedge * 1000000.0;
let a0 = pricing.bond_price_from_yield(a, yield_a) * 10000.0;
let b0 = pricing.bond_price_from_yield(b, yield_b) * hedge / 100.0;
let rows = str_list(); let worst = 0.0;
for (i in range(0, long_bp.len())) {
let pa = pricing.bond_price_from_yield(a, yield_a + long_bp.at(i) / 10000.0);
let pb = pricing.bond_price_from_yield(b, yield_b + short_bp.at(i) / 10000.0);
let da = pa * 10000.0 - a0;
let db = pb * hedge / 100.0 - b0;
let label = str(long_bp.at(i)) + " and " + str(short_bp.at(i)) + " bp";
rows.add(label + "|" + lmf.money(da) + "|" + lmf.money(0.0 - db));
rows.set(i, rows.at(i) + "|" + lmf.money(da - db));
if (i == 0 || da - db < worst) { worst = da - db; }
}
lmcode_return_put("rows", rows);
let answer = int(math_round(lmf.round(worst, 2) * 100.0));
{
"answer": -2997744,
"rows": [
"50 and 50 bp|-37,688.13|38,333.82|645.69",
"10 and 50 bp|-7,684.37|38,333.82|30,649.45",
"50 and 10 bp|-37,688.13|7,710.68|-29,977.44",
"-25 and 25 bp|19,540.87|19,235.41|38,776.27"
]
}Lines 10 and 11 reprice each bond at its own moved yield, lines 12 and 13 take the changes, and line 17 keeps the worst profit of the book. The picture lists, for each scenario, the profit of A, of the sold hedge and of the book. With 50 and 50 the book earns 645.69, with 10 and 50 it earns 30,649.45, with 50 and 10 it loses 29,977.44 and with -25 and 25 it earns 38,776.27. The answer, on line 20, is the worst profit in cents: -2997744.
You can change every input. The lists must have the same length: a shorter short_bp stops line 11 with "FloatList.at: index out of range: 2" for two entries.
A yield of bond A that is moved to -100% or below stops line 10, and one of bond B stops line 11, with "pricing.bond_price_from_yield: yield must be above -100% per coupon period (above -100% a year for 1 period a year), got -1". A yield of -1 stops line 4 with the same text for risk.bond_dv01_yield, and years_b of 0 stops line 4 with "bond maturity must be between 0 and 100 years".
The picture shows 16 scenarios at most, and extra entries of short_bp beyond the length of long_bp are ignored.
A yield of -0.9 for bond A is still above -100%, but its profits are too large to return and the run stops, without a line, with "lmcode return: invalid JSON value at /answer: unsafe_integer"; -0.8 runs.
Your turn
The program below should count the scenarios in which the hedged book loses more than a limit. Several candidate programs are offered. Read them, run them on scenarios of your own, and decide which of them is right for every set. Bond B has 2 years in every case. Check runs each candidate on the scenarios shown and on five other sets.

market.set_date("2025-01-02");
let a = instrument.bond(1000000.0, coupon_a, float(years_a), 1);
let b = instrument.bond(1000000.0, coupon_b, 2.0, 1);
let hedge = risk.bond_dv01_yield(a, yield_a) / risk.bond_dv01_yield(b, yield_b);
hedge = hedge * 1000000.0;
let a0 = pricing.bond_price_from_yield(a, yield_a) * 10000.0;
let b0 = pricing.bond_price_from_yield(b, yield_b) * hedge / 100.0;
let answer = 0;
for (i in range(0, long_bp.len())) {
let pa = pricing.bond_price_from_yield(a, yield_a + long_bp.at(i) / 10000.0);
let pb = pricing.bond_price_from_yield(b, yield_b + long_bp.at(i) / 10000.0);
let net = (pa * 10000.0 - a0) - (pb * hedge / 100.0 - b0);
if (net < 0.0 - limit) { answer = answer + 1; }
}
lmcode_return_put("hedge_face", lmf.money(hedge));
{
"answer": 0,
"hedge_face": "4,504,911.05"
}Bond A has a face of 1,000,000, the coupon coupon_a once a year, years_a whole years and the yield yield_a. Bond B has a face unit of 1,000,000, the coupon coupon_b, 2 years and the yield yield_b. The face of B that makes the DV01 of the book zero (the exact ratio, not rounded) is sold. In scenario i the yield of A moves by long_bp[i] basis points and the yield of B by short_bp[i] basis points. The profit of the book is the change in the value of A minus the change in the value of the sold face of B, repriced at the moved yields. Return how many scenarios have a profit below minus limit (limit is positive). Check runs your program on these scenarios and on five other sets.
Read the result carefully
The four scenarios of the demo and the lists of the cases are made up; nothing says which is likely. Each bond's own yield is moved, which is not the same as moving a curve: a coupon bond's yield is an average over its cash flows, and a real curve changes shape in ways two numbers cannot describe. Hedging against more than one kind of move needs more than one hedge instrument, which this book does not cover.
The exact profits come from repricing and are rounded to cents. The first-order rule of the hand example is only an approximation, and the model has no cost of trading or of borrowing the sold bond. The bonds pay for certain, and every yield and move is an assumption.
Recap
- A DV01 hedge is exact to first order only if the two yields move by the same amount.
- For different moves the book earns or loses about its DV01 times the difference of the moves: 7,721.73 estimated against 7,771.44 for 20 and 30 basis points.
- With 50 and 10 basis points the demo's book loses 29,977.44.
- Each bond has its own yield here; a real curve moves in more ways than two numbers describe.