Mean-absolute-deviation portfolio: a linear programming example, solved and checked
The mean-absolute-deviation 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 returned (%) A: 4, -2, 3, 1 and B: 1, 2, -1, 2 in four equally likely scenarios. Fully invested, no shorts. Minimize the mean absolute deviation of the portfolio return from its mean.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- wa and wb are the shares in funds A and B. The portfolio returns 4w_a + wb in scenario 1, and so on. Its mean is (6w_a + 4w_b) ÷ 4 = 1.5w_a + wb.
- Each term is one scenario's distance from the mean, times its probability 0.25. Scenario 1: 4w_a + wb − (1.5w_a + wb) = 2.5w_a, so 0.25 × |2.5w_a| = 0.625|wa|. Scenario 2: −2w_a + 2w_b − (1.5w_a + wb) = −3.5w_a + wb, written |3.5w_a − wb|.
- Returns are in %, so the objective is in % too.
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 | |
| fraction of the portfolio invested in fund A cannot be negative | |
| fraction of the portfolio invested in fund B cannot be negative |
In matrix form
With its numbers:
The problem as a linear program
Each absolute value (or max, min) gets a helper variable, tied to the term it stands for by the lines below: t1 stands for |0.5*wa - wb|; t2 stands for |3.5*wa - wb|; t3 stands for |1.5*wa - 2*wb|; t4 stands for |wa|. At the optimum each helper equals its term, so this linear program has the same optimum as your problem; it is what the solver solved, and what the dual is built from.
| the same objective, with each helper in place of the term it stands for | |
| limit 1: left side must equal 1 | |
| fraction of the portfolio invested in fund A cannot be negative | |
| fraction of the portfolio invested in fund B cannot be negative | |
| t1 is at least 0.5*wa - wb | |
| and at least -0.5*wa + wb, so t1 is at least |0.5*wa - wb| | |
| t2 is at least 3.5*wa - wb | |
| and at least -3.5*wa + wb, so t2 is at least |3.5*wa - wb| | |
| t3 is at least 1.5*wa - 2*wb | |
| and at least -1.5*wa + 2*wb, so t3 is at least |1.5*wa - 2*wb| | |
| t4 is at least wa | |
| and at least -wa, so t4 is at least |wa| |
What is being decided
There are 2 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| wa | real number | fraction of the portfolio invested in fund A |
| wb | real number | fraction of the portfolio invested in fund B |
Goal and limits
Minimize the objective subject to 3 constraints, of which 2 are simple bounds on a single variable.
Problem type
LP (after reformulating |.|, max, min), complexity level 1: a small linear program. Solved with HIGHS at the Medium solver tier.
Picture
Shaded: every point that satisfies all the constraints. Dot: the optimum. Dashed: the objective's level line through it.
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) wa + wb = 1. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | wa + wb = 1 | 1 | 1 | 0 | yes |
| 2 | wa >= 0 | 0.222222 | 0 | 0.222222 | |
| 3 | wb >= 0 | 0.777778 | 0 | 0.777778 |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
wa = cp.Variable(name='wa') # fraction of the portfolio invested in fund A
wb = cp.Variable(name='wb') # fraction of the portfolio invested in fund B
objective = cp.Minimize(0.625*cp.abs(wa) + 0.25*cp.abs(0.5*wa - wb) + 0.25*cp.abs(1.5*wa - 2*wb) + 0.25*cp.abs(3.5*wa - wb))
constraints = [
wa + wb == 1,
wa >= 0,
wb >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('wa =', wa.value)
print('wb =', wb.value)The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. First the problem was written as an exact linear program with helper variables t1, t2, t3, t4: each helper is held at or above (or below) the terms it stands for, so at the optimum it equals them and the optimum is unchanged. This is the dual of that linear program. Strong duality: primal and dual optima are equal (0.611111).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint 'wa + wb = 1' (objective change per extra unit of its right-hand side) | 0.611111 |
| y2 | price of constraint 't1 >= 0.5*wa - wb' (objective change per extra unit of its right-hand side) | 0 |
| y3 | price of constraint 't1 >= -0.5*wa + wb' (objective change per extra unit of its right-hand side) | 0.25 |
| y4 | price of constraint 't2 >= 3.5*wa - wb' (objective change per extra unit of its right-hand side) | 0.194444 |
| y5 | price of constraint 't2 >= -3.5*wa + wb' (objective change per extra unit of its right-hand side) | 0.0555556 |
| y6 | price of constraint 't3 >= 1.5*wa - 2*wb' (objective change per extra unit of its right-hand side) | 0 |
| y7 | price of constraint 't3 >= -1.5*wa + 2*wb' (objective change per extra unit of its right-hand side) | 0.25 |
| y8 | price of constraint 't4 >= wa' (objective change per extra unit of its right-hand side) | 0.625 |
| y9 | price of constraint 't4 >= -wa' (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 0.25*(abs(2.5*wa) + abs(-3.5*wa + wb) + abs(1.5*wa - 2*wb) + abs(-0.5*wa + wb))
subject to
wa + wb = 1
wa, wb >= 0
# wa = fraction of the portfolio invested in fund A
# wb = fraction of the portfolio invested in fund BChecked: 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.