∇ProvenDecisions
Ctrl K

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.

Answer. The smallest objective value is -0.136364, at a = 0.363636, b = 0.636364, u1 = 0.0909091, t = -0.181818, u2 = 0, u3 = 0, u4 = 0, u5 = 0. All 13 constraints are satisfied exactly. Exactly proven optimal.

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:

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.

VariableTypeMeaning
areal numberfraction of the portfolio invested in fund A
breal numberfraction of the portfolio invested in fund B
u1real numberexcess of month 1 loss over t (percent)
treal numbervalue-at-risk threshold, the loss level in the tail formula (percent, free sign)
u2real numberexcess of month 2 loss over t (percent)
u3real numberexcess of month 3 loss over t (percent)
u4real numberexcess of month 4 loss over t (percent)
u5real numberexcess 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.

a0.363636b0.636364u10.0909091t-0.181818u20u30u40u50

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.

#ConstraintValueLimitSlackBinding
1a + b = 1110yes
2u1 >= -2*a + b - t0.09090910.09090910yes
3u2 >= 3*a - 2*b - t000yes
4u3 >= -a - t0-0.1818180.181818
5u4 >= -4*a + 2*b - t000yes
6u5 >= a - 3*b - t0-1.363641.36364
7a >= 00.36363600.363636
8b >= 00.63636400.636364
9u1 >= 00.090909100.0909091
10u2 >= 0000yes
11u3 >= 0000yes
12u4 >= 0000yes
13u5 >= 0000yes

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.

#ConstraintWeight
1a + b = 1-3/22
2u1 ≥ -2·a + b - t1/2
3u2 ≥ 3·a - 2·b - t9/22
5u4 ≥ -4·a + 2·b - t1/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 variablePricesValue
y1price of constraint 'a + b = 1' (objective change per extra unit of its right-hand side)-0.136364
y2price of constraint 'u1 >= -2*a + b - t' (objective change per extra unit of its right-hand side)0.5
y3price of constraint 'u2 >= 3*a - 2*b - t' (objective change per extra unit of its right-hand side)0.409091
y4price of constraint 'u3 >= -a - t' (objective change per extra unit of its right-hand side)0
y5price of constraint 'u4 >= -4*a + 2*b - t' (objective change per extra unit of its right-hand side)0.0909091
y6price 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)

Change the numbers and solve it

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

All 50 solved examples

Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.

What do you want to optimize?

Type the math, describe it in words, snap a photo or say it. ProvenDecisions builds the model, solves it, checks it and walks you through it.

One constraint per line, or separated by commas. Add integer: x, y or binary: z for whole-number decisions. LaTeX works too.

Try: