CVaR (worst-case loss) portfolio: a linear programming example, solved and checked
The CVaR (worst-case loss) portfolio 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
Two funds had monthly returns (%) of A: 2, -3, 1, 4, -1 and B: -1, 2, 0, -2, 3 over five months. Invest fully, no shorts. Minimize the average loss over the worst two months of the resulting portfolio (loss = minus return).
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- a and b are the shares in funds A and B. A month's loss is minus its return; month 1: −(2a − b) = −2a + b.
- t is a threshold, and ui is how far month i's loss is above it (0 if below): ui ≥ loss_i − t and ui ≥ 0.
- t + (u1 + … + u5) ÷ 2, at its best t, is the average of the 2 worst losses (2 is the number of worst months). Example: losses of 3, 1, 0, −1 and −2 with t = 1 give 1 + 2 ÷ 2 = 2, the average of 3 and 1.
- Losses are in %. A negative value means the two worst months still average a gain.
The model
Each line has a plain-language note on the right.
| make this as small as possible | |
| limit 1: left side must equal 1 | |
| limit 2: left side must reach at least the right side | |
| limit 3: left side must reach at least the right side | |
| limit 4: left side must reach at least the right side | |
| limit 5: left side must reach at least the right side | |
| limit 6: left side must reach at least the right side | |
| fraction of the portfolio invested in fund A cannot be negative | |
| fraction of the portfolio invested in fund B cannot be negative | |
| excess of month 1 loss over t (percent) cannot be negative | |
| excess of month 2 loss over t (percent) cannot be negative | |
| excess of month 3 loss over t (percent) cannot be negative | |
| excess of month 4 loss over t (percent) cannot be negative | |
| excess of month 5 loss over t (percent) cannot be negative |
In matrix form
With its numbers:
What is being decided
There are 8 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| a | real number | fraction of the portfolio invested in fund A |
| b | real number | fraction of the portfolio invested in fund B |
| u1 | real number | excess of month 1 loss over t (percent) |
| t | real number | value-at-risk threshold, the loss level in the tail formula (percent, free sign) |
| u2 | real number | excess of month 2 loss over t (percent) |
| u3 | real number | excess of month 3 loss over t (percent) |
| u4 | real number | excess of month 4 loss over t (percent) |
| u5 | real number | excess of month 5 loss over t (percent) |
Goal and limits
Minimize the objective subject to 13 constraints, of which 7 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): constraints 1, 2, 3, 5, 10, 11, 12, 13 (see the table). Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | a + b = 1 | 1 | 1 | 0 | yes |
| 2 | u1 >= -2*a + b - t | 0.0909091 | 0.0909091 | 0 | yes |
| 3 | u2 >= 3*a - 2*b - t | 0 | 0 | 0 | yes |
| 4 | u3 >= -a - t | 0 | -0.181818 | 0.181818 | |
| 5 | u4 >= -4*a + 2*b - t | 0 | 0 | 0 | yes |
| 6 | u5 >= a - 3*b - t | 0 | -1.36364 | 1.36364 | |
| 7 | a >= 0 | 0.363636 | 0 | 0.363636 | |
| 8 | b >= 0 | 0.636364 | 0 | 0.636364 | |
| 9 | u1 >= 0 | 0.0909091 | 0 | 0.0909091 | |
| 10 | u2 >= 0 | 0 | 0 | 0 | yes |
| 11 | u3 >= 0 | 0 | 0 | 0 | yes |
| 12 | u4 >= 0 | 0 | 0 | 0 | yes |
| 13 | u5 >= 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. A negative weight turns a limit around (for example ≥ into ≤) before adding. Every step was checked in exact fractions, not rounded decimals.
| # | Constraint | Weight |
|---|---|---|
| 1 | a + b = 1 | -3/22 |
| 2 | u1 ≥ -2·a + b - t | 1/2 |
| 3 | u2 ≥ 3·a - 2·b - t | 9/22 |
| 5 | u4 ≥ -4·a + 2·b - t | 1/11 |
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
a = cp.Variable(name='a') # fraction of the portfolio invested in fund A
b = cp.Variable(name='b') # fraction of the portfolio invested in fund B
u1 = cp.Variable(name='u1') # excess of month 1 loss over t (percent)
t = cp.Variable(name='t') # value-at-risk threshold, the loss level in the tail formula (percent, free sign)
u2 = cp.Variable(name='u2') # excess of month 2 loss over t (percent)
u3 = cp.Variable(name='u3') # excess of month 3 loss over t (percent)
u4 = cp.Variable(name='u4') # excess of month 4 loss over t (percent)
u5 = cp.Variable(name='u5') # excess of month 5 loss over t (percent)
objective = cp.Minimize(t + 0.5*u1 + 0.5*u2 + 0.5*u3 + 0.5*u4 + 0.5*u5)
constraints = [
a + b == 1,
u1 >= -2*a + b - t,
u2 >= 3*a - 2*b - t,
u3 >= -a - t,
u4 >= -4*a + 2*b - t,
u5 >= a - 3*b - t,
a >= 0,
b >= 0,
u1 >= 0,
u2 >= 0,
u3 >= 0,
u4 >= 0,
u5 >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('a =', a.value)
print('b =', b.value)
print('u1 =', u1.value)
print('t =', t.value)
print('u2 =', u2.value)
print('u3 =', u3.value)
print('u4 =', u4.value)
print('u5 =', u5.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 (-0.136364).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'a + b = 1' (objective change per extra unit of its right-hand side) | -0.136364 |
| y2 | price of constraint 'u1 >= -2*a + b - t' (objective change per extra unit of its right-hand side) | 0.5 |
| y3 | price of constraint 'u2 >= 3*a - 2*b - t' (objective change per extra unit of its right-hand side) | 0.409091 |
| y4 | price of constraint 'u3 >= -a - t' (objective change per extra unit of its right-hand side) | 0 |
| y5 | price of constraint 'u4 >= -4*a + 2*b - t' (objective change per extra unit of its right-hand side) | 0.0909091 |
| y6 | price of constraint 'u5 >= a - 3*b - t' (objective change per extra unit of its right-hand side) | 0 |
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 t + 0.5 u1 + 0.5 u2 + 0.5 u3 + 0.5 u4 + 0.5 u5
subject to
a + b = 1
u1 >= -2 a + b - t
u2 >= 3 a - 2 b - t
u3 >= -a - t
u4 >= -4 a + 2 b - t
u5 >= a - 3 b - t
a, b, u1, u2, u3, u4, u5 >= 0
# a = fraction of the portfolio invested in fund A
# b = fraction of the portfolio invested in fund B
# t = value-at-risk threshold, the loss level in the tail formula (percent, free sign)
# u1 = excess of month 1 loss over t (percent)
# u2 = excess of month 2 loss over t (percent)
# u3 = excess of month 3 loss over t (percent)
# u4 = excess of month 4 loss over t (percent)
# u5 = excess of month 5 loss over t (percent)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.