Written and maintained by CASRAI Editorial Board
Last updated
Short answer
CVXPY is an open-source, Python-embedded modeling language for convex optimization problems. Developed by Steven Diamond and Stephen Boyd, CVXPY lets researchers specify optimization models using intuitive mathematical syntax rather than low-level solver matrices. It validates Disciplined Convex Programming (DCP) rules, converts expressions into canonical cone forms, calls numerical solvers, and returns primal and dual solutions.
Definition and scope
In computational mathematics and operations research, convex optimization minimizes a convex objective function subject to convex inequality constraints and affine equality constraints. Formally, a convex optimization problem takes the form:
minimize f0(x)
subject to fi(x) ≤ 0, i = 1, …, m
Ax = b
where x ∈ ℝn is the optimization variable, f0, …, fm are convex functions, and Ax = b represents affine equalities. As established by Stephen Boyd and Lieven Vandenberghe in Convex Optimization (2004), any local minimum of a convex problem is a global minimum, and duality theory provides exact certificates of optimality and infeasibility.
CVXPY is an embedded domain-specific language (DSL) in Python. Rather than requiring researchers to manually assemble numerical coefficient matrices for low-level solvers, CVXPY abstracts solver interfaces behind declarative Python syntax. Researchers define decision variables with cp.Variable(), constants or tunable inputs with cp.Parameter(), objectives with cp.Minimize() or cp.Maximize(), and constraints as Python lists of relations. This connects mathematical problem formulation with execution in operations research, signal processing, and laboratory engineering.
How it works or how it is used in practice
CVXPY uses a compilation pipeline described by Diamond and Boyd (2016) and Agrawal et al. (2018). When formulating a model, CVXPY builds an Abstract Syntax Tree (AST) representing the mathematical operations.
The compilation pipeline executes five phases:
- AST Construction: Mathematical atoms (such as
cp.normorcp.sum_squares) form nodes in an expression tree whose leaves are variables, parameters, or constants. - DCP Verification: CVXPY verifies convexity using Disciplined Convex Programming rules, checking curvature (convex, concave, affine, constant) and monotonicity. Expressions violating these rules trigger a
DCPErrorbefore solver invocation. - Canonicalization: The problem undergoes reductions, transforming high-level expressions via epigraph formulations into standard cone programs (linear, second-order, exponential, or semidefinite cones).
- Solver Dispatch: CVXPY formats canonical cone constraints into sparse numerical matrices for the backend solver (e.g., Clarabel, OSQP, SCS, or HiGHS) and runs the solver.
- Result Retrieval: CVXPY unpacks optimal values into
variable.value, records the objective value inproblem.value, and stores Lagrange multipliers inconstraint.dual_value.
The script below demonstrates a constrained estimation model, commonly applied in experimental calibration alongside techniques like elastic net regression and SciPy least squares:
import cvxpy as cp
import numpy as np
# Calibration data
np.random.seed(42)
m, n = 40, 8
A = np.random.randn(m, n)
y = A @ np.maximum(0, np.random.randn(n)) + 0.05 * np.random.randn(m)
# 1. Variables and regularizer parameter
x = cp.Variable(n, nonneg=True)
gamma = cp.Parameter(nonneg=True, value=0.1)
# 2. Objective and constraints
objective = cp.Minimize(0.5 * cp.sum_squares(A @ x - y) + gamma * cp.norm1(x))
constraints = [cp.sum(x) <= 3.0]
# 3. Solve problem
problem = cp.Problem(objective, constraints)
problem.solve(solver=cp.CLARABEL)
print(f"Status: {problem.status}")
print(f"Optimal value: {problem.value:.4f}")
print(f"Shadow price: {constraints[0].dual_value:.4f}")
This formulation illustrates how CVXPY handles weighted formulations comparable to weighted least squares while guaranteeing global optimality.
Types or related concepts
CVXPY interfaces with multiple open-source and commercial solvers. Because solvers support distinct cone geometries, selecting the appropriate backend is essential for performance:
| Solver | Type & License | Cones Supported | Mixed-Integer (MIP) | Primary Research & Lab Use Case |
|---|---|---|---|---|
| Clarabel | Default Open Source (Apache-2.0) | LP, QP, SOCP, SDP, EXP | No | High-precision conic optimization; default general-purpose solver in CVXPY since version 1.4. |
| OSQP | Default Open Source (Apache-2.0) | LP, QP | No | Operator-splitting QP solver; rapid convergence for large-scale quadratic programs and control loops. |
| SCS | Default Open Source (MIT) | LP, QP, SOCP, SDP, EXP | No | First-order splitting conic solver; scales to very large semidefinite problems where moderate precision suffices. |
| HiGHS | Default Open Source (MIT) | LP, QP | Yes (MILP, MIQP) | Simplex and interior-point solver; standard backend for discrete lab scheduling and combinatorial models. |
| MOSEK | Commercial (Free Academic) | LP, QP, SOCP, SDP, EXP | Yes (MISOCP) | Interior-point solver; benchmark accuracy for large-scale second-order cone and semidefinite programs. |
| Gurobi | Commercial (Free Academic) | LP, QP, SOCP | Yes (MILP, MIQP) | Branch-and-cut solver for large-scale mixed-integer quadratic and second-order cone optimization. |
CVXPY also supports specialized disciplines:
- Disciplined Parametrized Programming (DPP): Structures parameter usage so problems can be canonicalized once and repeatedly solved with updated
cp.Parametervalues without AST recompilation. - Disciplined Geometric Programming (DGP): Formulates log-log convex models for physical systems like electronic circuits and chemical equilibrium ratios.
- Disciplined Quasiconvex Programming (DQCP): Solves quasiconvex models, such as generalized fractional programs, using bisection methods.
- Mixed-Integer Convex Programming (MICP): Integrates integer or boolean variables (e.g.,
cp.Variable(integer=True)) over convex cones via branch-and-bound backends.
Where it matters for labs, clinics or supply decisions
Convex optimization provides analytical rigor across quantitative laboratories and research facilities:
- Instrument Calibration: Chromatography and spectrometry yield overlapping peaks subject to physical bounds. Formulating baseline subtraction and peak deconvolution as non-negative quadratic programs in CVXPY ensures valid, non-negative estimates.
- High-Throughput Assays: In screening pipelines, curve fitting across microplate wells requires fast solving. Using DPP with
cp.Parameterallows analysis scripts to re-solve models across thousands of wells without re-compiling the problem graph. - Equipment Scheduling: Core facilities managing shared sequencers or spectrometers formulate run schedules as mixed-integer programs solved via HiGHS, balancing queue times against maintenance windows.
- Sensitivity Analysis in Supply Decisions: Examining dual variables (
constraint.dual_value) reveals the shadow price of capacity or budget limits, quantifying the throughput gain of expanding equipment capacity.
Common misunderstandings
Researchers commonly encounter several misconceptions when working with CVXPY:
- Convexity guarantees DCP acceptance: A function can be convex yet violate DCP grammar. For example,
cp.sqrt(x**2 + 1)is mathematically convex, but CVXPY raises aDCPErrorbecause the composition of a concave atom (cp.sqrt) with a convex argument is non-compliant. The disciplined alternative is the second-order cone expressioncp.norm(cp.vstack([x, 1])). - CVXPY performs numerical solving: CVXPY is a compiler and domain-specific modeling layer, not a numerical solver. Numerical iterations, factorizations, and tolerances reside entirely within backend solvers like Clarabel or OSQP.
- Re-creating Problem instances in loops: Instantiating
cp.Probleminside iterative loops introduces significant canonicalization overhead. Reusable models should declarecp.Parameterobjects, update their values, and callproblem.solve()directly. - Solvers provide identical precision: First-order solvers (OSQP, SCS) prioritize speed and low memory usage, terminating at modest tolerances (10-3 to 10-5). Interior-point solvers (Clarabel, MOSEK) provide high precision (10-8) required for exact dual values and tight constraints.
Frequently asked questions
What causes a DCPError in CVXPY and how is it resolved?
A DCPError occurs when an expression violates Disciplined Convex Programming rules, such as multiplying decision variables or composing functions where monotonicity does not preserve convexity. To resolve it, inspect curvatures using expr.curvature and reformulate expressions using standard convex atoms or epigraph variables.
How does CVXPY handle non-convex optimization problems?
CVXPY rejects general non-convex problems because its transformations require convexity. It accommodates specific non-convex extensions, including Mixed-Integer Convex Programming (MICP) via branch-and-bound and Disciplined Quasiconvex Programming (DQCP) via bisection. For general non-convex optimization, SciPy is recommended.
Which solver should be used for mixed-integer problems?
For models containing integer or boolean variables, HiGHS is the default bundled open-source solver in CVXPY, invoked via solver=cp.HIGHS. Commercial solvers like Gurobi or MOSEK can also be specified if available.
How are dual variables extracted and interpreted in CVXPY?
After calling problem.solve(), each constraint stores its optimal Lagrange multiplier in constraint.dual_value. For an inequality constraint f(x) ≤ b, the dual value represents the marginal change in the objective value with respect to perturbations in bound b, providing shadow prices for resource limits.
Sources
- Diamond, S., & Boyd, S. (2016). “CVXPY: A Python-Embedded Modeling Language for Convex Optimization.” Journal of Machine Learning Research, 17(83):1–5. http://jmlr.org/papers/v17/15-408.html (read 2026-11-09).
- CVXPY Development Team. (2026). “CVXPY Documentation and API Reference.” https://www.cvxpy.org/ (read 2026-11-09).
- Agrawal, A., Verschueren, R., Diamond, S., & Boyd, S. (2018). “A Rewriting System for Convex Optimization Problems.” Journal of Control and Decision, 5(1):42–60. https://web.stanford.edu/~boyd/papers/cvxpy_rewriting.html (read 2026-11-09).
- Boyd, S., & Vandenberghe, L. (2004). Convex Optimization. Cambridge University Press. https://web.stanford.edu/~boyd/cvxbook/ (read 2026-11-09).







