Ridge regression: a quadratic programming example, solved and checked
The ridge regression is a classic quadratic programming problem. Here it is solved by a real solver (CLARABEL) 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
Minimize ||Ax - b||^2 + 0.5 ||x||^2 with A = [[1, 0], [1, 1], [1, 2], [1, 3]] and b = (1, 2, 2, 4).
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- x0 and x1 are the two entries of x. Each squared term is one row of A x − b: the row (1, 2) of A with b = 2 gives (x0 + 2x_1 − 2)2.
- 0.5 x02 + 0.5 x12 is the penalty 0.5 ||x||2. It pulls the fit towards smaller coefficients.
- 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 | |
| intercept coefficient (unitless) is at least -1000 | |
| slope coefficient (unitless) is at least -1000 |
In matrix form
With its numbers:
What is being decided
There are 2 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x0 | real number | intercept coefficient (unitless) |
| x1 | real number | slope coefficient (unitless) |
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 | x0 >= -1000 | 0.769231 | -1000 | 1000.77 | |
| 2 | x1 >= -1000 | 0.923077 | -1000 | 1000.92 |
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
x0 = cp.Variable(name='x0') # intercept coefficient (unitless)
x1 = cp.Variable(name='x1') # slope coefficient (unitless)
objective = cp.Minimize(0.5*x0**2 + 0.5*x1**2 + (x0 - 1)**2 + (x0 + x1 - 2)**2 + (x0 + 2*x1 - 2)**2 + (x0 + 3*x1 - 4)**2)
constraints = [
x0 >= -1000,
x1 >= -1000,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='CLARABEL')
print(problem.status, problem.value)
print('x0 =', x0.value)
print('x1 =', x1.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 (1.46154).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | multiplier of an inequality constraint | 0 |
| y2 | multiplier of an inequality constraint | 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 (x0 + 0*x1 - 1)^2 + (x0 + x1 - 2)^2 + (x0 + 2*x1 - 2)^2 + (x0 + 3*x1 - 4)^2 + 0.5*(x0^2 + x1^2)
subject to
x0 >= -1000
x1 >= -1000
# x0 = intercept coefficient (unitless)
# x1 = slope coefficient (unitless)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.