Cash-flow matching with bonds: a linear programming example, solved and checked
The cash-flow matching with bonds is a classic linear programming problem. Here it is solved by a real solver (HIGHS) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
The problem
Liabilities of $100k, $120k and $150k are due at the ends of years 1, 2 and 3. Bond A (price $95) pays $100 at year 1. Bond B (price $90) pays $5 at years 1 and 2 and $105 at year 3. Bond C (price $92) pays $6 at year 1 and $106 at year 2. Surplus cash is carried forward at 0% interest. Buy fractional units (in thousands) to meet every liability at minimum cost.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- xA, xB and xC are the bonds bought, in thousands, so a bond paying $100 adds 100 xA in $ thousands.
- s1 and s2 are the cash left over after years 1 and 2, carried to the next year at 0% interest.
- Year 1: cash in 100x_A + 5x_B + 6x_C, minus what is carried, s1, pays the $100k due. Year 2: 5x_B + 106x_C + s1 − s2 = 120. Year 3: 105x_B + s2 = 150.
- Cost = $95 xA + $90 xB + $92 xC, in $ thousands.
The model
Each line has a plain-language note on the right.
| make this as small as possible | |
| limit 1: left side must equal 100 | |
| limit 2: left side must equal 120 | |
| limit 3: left side must equal 150 | |
| units of Bond A bought (thousands of bonds) cannot be negative | |
| units of Bond B bought (thousands of bonds) cannot be negative | |
| units of Bond C bought (thousands of bonds) cannot be negative | |
| surplus cash carried from year 1 to year 2 ($k) cannot be negative | |
| surplus cash carried from year 2 to year 3 ($k) cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 5 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| xA | real number | units of Bond A bought (thousands of bonds) |
| xB | real number | units of Bond B bought (thousands of bonds) |
| xC | real number | units of Bond C bought (thousands of bonds) |
| s1 | real number | surplus cash carried from year 1 to year 2 ($k) |
| s2 | real number | surplus cash carried from year 2 to year 3 ($k) |
Goal and limits
Minimize the objective subject to 8 constraints, of which 5 are simple bounds on a single variable.
Problem type
LP, complexity level 2: a linear program. Solved with HIGHS at the Medium solver tier.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Which constraints hold the answer in place
Binding (no slack left): (1) -s1 + 100*xA + 5*xB + 6*xC = 100; (2) s1 - s2 + 5*xB + 106*xC = 120; (3) s2 + 105*xB = 150; (7) s1 >= 0; (8) s2 >= 0. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | -s1 + 100*xA + 5*xB + 6*xC = 100 | 100 | 100 | 0 | yes |
| 2 | s1 - s2 + 5*xB + 106*xC = 120 | 120 | 120 | 0 | yes |
| 3 | s2 + 105*xB = 150 | 150 | 150 | 0 | yes |
| 4 | xA >= 0 | 0.86469 | 0 | 0.86469 | |
| 5 | xB >= 0 | 1.42857 | 0 | 1.42857 | |
| 6 | xC >= 0 | 1.06469 | 0 | 1.06469 | |
| 7 | s1 >= 0 | 0 | 0 | 0 | yes |
| 8 | s2 >= 0 | 0 | 0 | 0 | yes |
Why this is optimal
A proof you can check by hand. Multiply each constraint in the table by its weight and add them up: the result is a lower limit on the objective that holds for every plan meeting the constraints. Our answer reaches that limit exactly, so no plan does better. Every step was checked in exact fractions, not rounded decimals.
| # | Constraint | Weight |
|---|---|---|
| 1 | -s1 + 100·xA + 5·xB + 6·xC = 100 | 19/20 |
| 2 | s1 - s2 + 5·xB + 106·xC = 120 | 863/1060 |
| 3 | s2 + 105·xB = 150 | 1721/2226 |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Standard form
As a linear program: minimize cᵀx subject to A x ≤ b (the inequality rows) and A_eq x = b_eq (the equations); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
xA = cp.Variable(name='xA') # units of Bond A bought (thousands of bonds)
xB = cp.Variable(name='xB') # units of Bond B bought (thousands of bonds)
xC = cp.Variable(name='xC') # units of Bond C bought (thousands of bonds)
s1 = cp.Variable(name='s1') # surplus cash carried from year 1 to year 2 ($k)
s2 = cp.Variable(name='s2') # surplus cash carried from year 2 to year 3 ($k)
objective = cp.Minimize(95*xA + 90*xB + 92*xC)
constraints = [
-s1 + 100*xA + 5*xB + 6*xC == 100,
s1 - s2 + 5*xB + 106*xC == 120,
s2 + 105*xB == 150,
xA >= 0,
xB >= 0,
xC >= 0,
s1 >= 0,
s2 >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('xA =', xA.value)
print('xB =', xB.value)
print('xC =', xC.value)
print('s1 =', s1.value)
print('s2 =', s2.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Strong duality: primal and dual optima are equal (308.668).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint '-s1 + 100*xA + 5*xB + 6*xC = 100' (objective change per extra unit of its right-hand side) | 0.95 |
| y2 | price of constraint 's1 - s2 + 5*xB + 106*xC = 120' (objective change per extra unit of its right-hand side) | 0.814151 |
| y3 | price of constraint 's2 + 105*xB = 150' (objective change per extra unit of its right-hand side) | 0.773136 |
Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
minimize 95 xA + 90 xB + 92 xC
subject to
100 xA + 5 xB + 6 xC - s1 = 100
s1 + 5 xB + 106 xC - s2 = 120
s2 + 105 xB = 150
xA, xB, xC, s1, s2 >= 0
# xA = units of Bond A bought (thousands of bonds)
# xB = units of Bond B bought (thousands of bonds)
# xC = units of Bond C bought (thousands of bonds)
# s1 = surplus cash carried from year 1 to year 2 ($k)
# s2 = surplus cash carried from year 2 to year 3 ($k)Checked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, CLARABEL) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.