Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

CVXPY: Python Convex Optimization Modeling Guide

Technical guide to CVXPY for Python convex optimization: problem specification, disciplined convex programming rules, solver matching, and lab workflows.

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:

  1. AST Construction: Mathematical atoms (such as cp.norm or cp.sum_squares) form nodes in an expression tree whose leaves are variables, parameters, or constants.
  2. DCP Verification: CVXPY verifies convexity using Disciplined Convex Programming rules, checking curvature (convex, concave, affine, constant) and monotonicity. Expressions violating these rules trigger a DCPError before solver invocation.
  3. Canonicalization: The problem undergoes reductions, transforming high-level expressions via epigraph formulations into standard cone programs (linear, second-order, exponential, or semidefinite cones).
  4. 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.
  5. Result Retrieval: CVXPY unpacks optimal values into variable.value, records the objective value in problem.value, and stores Lagrange multipliers in constraint.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.Parameter values 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:

  1. 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.
  2. High-Throughput Assays: In screening pipelines, curve fitting across microplate wells requires fast solving. Using DPP with cp.Parameter allows analysis scripts to re-solve models across thousands of wells without re-compiling the problem graph.
  3. 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.
  4. 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 a DCPError because the composition of a concave atom (cp.sqrt) with a convex argument is non-compliant. The disciplined alternative is the second-order cone expression cp.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.Problem inside iterative loops introduces significant canonicalization overhead. Reusable models should declare cp.Parameter objects, update their values, and call problem.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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about CVXPY: Python Convex Optimization Modeling Guide

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →