Lasso regression: a quadratic programming example, solved and checked
The lasso regression is a classic quadratic programming problem. Here it is solved by a real solver (CLARABEL) and checked: every constraint, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
The problem
Minimize ||Ax - b||^2 + 0.5 ||x||_1 with A = [[1, 0.5], [0.2, 1], [1, 1], [0.5, -0.3]] and b = (1, 0.8, 1.6, 0.3).
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- Each squared term is one row of A x − b. The row (0.2, 1) of A with b = 0.8 gives (0.2x_1 + x2 − 0.8)2.
- 0.5|x1| + 0.5|x2| is the penalty 0.5 ||x||1, the sum of the absolute values. Unlike a squared penalty, it can push a coefficient all the way to 0.
- The bounds of −1000 are loose limits in the model; they do not bind at the answer.
The model
Each line has a plain-language note on the right.
| make this as small as possible | |
| first coefficient of x (unitless, free sign) is at least -1000 | |
| second coefficient of x (unitless, free sign) is at least -1000 |
What is being decided
There are 2 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x1 | real number | first coefficient of x (unitless, free sign) |
| x2 | real number | second coefficient of x (unitless, free sign) |
Goal and limits
Minimize the objective subject to 2 constraints, of which 2 are simple bounds on a single variable.
Problem type
Convex QP, complexity level 1: a small convex quadratic. Solved with CLARABEL 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
No constraint is binding. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | x1 >= -1000 | 0.763303 | -1000 | 1000.76 | |
| 2 | x2 >= -1000 | 0.58841 | -1000 | 1000.59 |
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
x1 = cp.Variable(name='x1') # first coefficient of x (unitless, free sign)
x2 = cp.Variable(name='x2') # second coefficient of x (unitless, free sign)
objective = cp.Minimize((0.2*x1 + x2 - 0.8)**2 + (0.5*x1 - 0.3*x2 - 0.3)**2 + (x1 + 0.5*x2 - 1)**2 + (x1 + x2 - 1.6)**2 + 0.5*cp.abs(x1) + 0.5*cp.abs(x2))
constraints = [
x1 >= -1000,
x2 >= -1000,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='CLARABEL')
print(problem.status, problem.value)
print('x1 =', x1.value)
print('x2 =', x2.value)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 (x1 + 0.5*x2 - 1)^2 + (0.2*x1 + x2 - 0.8)^2 + (x1 + x2 - 1.6)^2 + (0.5*x1 - 0.3*x2 - 0.3)^2 + 0.5*(abs(x1) + abs(x2))
subject to
x1 >= -1000
x2 >= -1000
# x1 = first coefficient of x (unitless, free sign)
# x2 = second coefficient of x (unitless, free sign)Checked: the app's answer matches the reference answer in our test set, which two solvers (CLARABEL, OSQP) 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.