∇ProvenDecisions
Ctrl K

Least absolute deviations regression: a linear programming example, solved and checked

The least absolute deviations regression 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 1, at b = 1, a = 1. Optimal, numerically checked.

Solve your own version free, no account needed

The problem

Fit y = a + b t to the points (0, 1), (1, 2), (2, 2), (3, 4), (4, 5) minimizing the sum of absolute residuals.

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

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 |a - 1|; t2 stands for |a + 4*b - 5|; t3 stands for |a + 3*b - 4|; t4 stands for |a + b - 2|; t5 stands for |a + 2*b - 2|. 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
t1 is at least a - 1
and at least 1 - a, so t1 is at least |a - 1|
t2 is at least a + 4*b - 5
and at least -a - 4*b + 5, so t2 is at least |a + 4*b - 5|
t3 is at least a + 3*b - 4
and at least -a - 3*b + 4, so t3 is at least |a + 3*b - 4|
t4 is at least a + b - 2
and at least -a - b + 2, so t4 is at least |a + b - 2|
t5 is at least a + 2*b - 2
and at least -a - 2*b + 2, so t5 is at least |a + 2*b - 2|

What is being decided

There are 2 decision variables.

VariableTypeMeaning
breal numberslope of fitted line (y per unit t)
areal numberintercept of fitted line (y units)

Goal and limits

Minimize the objective subject to 0 constraints.

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.

(1, 1)-0.4-0.40.050.050.50.50.950.951.41.4ba

The answer at a glance

The optimal value of each decision, as the solver returned it.

b1a1

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

b = cp.Variable(name='b')  # slope of fitted line (y per unit t)
a = cp.Variable(name='a')  # intercept of fitted line (y units)

objective = cp.Minimize(cp.abs(a - 1) + cp.abs(a + b - 2) + cp.abs(a + 2*b - 2) + cp.abs(a + 3*b - 4) + cp.abs(a + 4*b - 5))
constraints = [
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('b =', b.value)
print('a =', a.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, t5: 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 (1).

Dual variablePricesValue
y1price of constraint 't1 >= a - 1' (objective change per extra unit of its right-hand side)0.5
y2price of constraint 't1 >= 1 - a' (objective change per extra unit of its right-hand side)0.5
y3price of constraint 't2 >= a + 4*b - 5' (objective change per extra unit of its right-hand side)0
y4price of constraint 't2 >= -a - 4*b + 5' (objective change per extra unit of its right-hand side)1
y5price of constraint 't3 >= a + 3*b - 4' (objective change per extra unit of its right-hand side)1
y6price of constraint 't3 >= -a - 3*b + 4' (objective change per extra unit of its right-hand side)0
y7price of constraint 't4 >= a + b - 2' (objective change per extra unit of its right-hand side)0
y8price of constraint 't4 >= -a - b + 2' (objective change per extra unit of its right-hand side)1
y9price of constraint 't5 >= a + 2*b - 2' (objective change per extra unit of its right-hand side)1
y10price of constraint 't5 >= -a - 2*b + 2' (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 abs(1 - a - b*0) + abs(2 - a - b*1) + abs(2 - a - b*2) + abs(4 - a - b*3) + abs(5 - a - b*4)
subject to
a, b free
# a = intercept of fitted line (y units)
# b = slope of fitted line (y per unit t)

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: