Semidefinite programming

Optimising over matrices that must stay positive semidefinite — with a 2×2 case you can see completely.

What you'll learn
  • Read the constraint X ⪰ 0 and why it defines a convex set
  • Solve the script's SDP by hand with an eigenvalue
  • See why SDP optima like to be low rank

A semidefinite program has a matrix as its variable, and requires that matrix to be positive semidefinite — every eigenvalue non-negative, written \(X \succeq 0\). The script solves the simplest interesting one:

X = cp.Variable((n, n), symmetric=True)
objective   = cp.Minimize(cp.trace(C @ X))
constraints = [X >> 0,              # positive semidefinite
               cp.trace(X) == 1]    # normalisation
problem = cp.Problem(objective, constraints)
problem.solve(solver=cp.SCS)

The objective \(\operatorname{tr}(CX) = \sum_{ij} C_{ij}X_{ij}\) is linear in the entries of \(X\). All the curvature is in the constraint: the PSD matrices form a convex cone, and \(\operatorname{tr} X = 1\) slices it.

The 2×2 case, completely

For 2×2 matrices everything can be drawn. A symmetric matrix with trace 1 has the form

\[X = \begin{bmatrix} a & b \\ b & 1 - a \end{bmatrix}\]

and it is positive semidefinite exactly when its determinant is non-negative, \(a(1-a) - b^2 \ge 0\), which rearranges to

\[\left(a - \tfrac12\right)^2 + b^2 \;\le\; \tfrac14\]

— a disc in the \((a, b)\) plane. The feasible set of this SDP is a disc, and the objective is linear, so its level lines are straight. The optimum is where the lowest level line just touches the disc: on its edge.

The disc is every 2×2 PSD matrix with trace 1. Straight lines are level sets of tr(CX), and the arrow points downhill. As C turns, the optimum X* slides round the edge — where det X = 0 — and the read-out shows tr(CX*) always equals the smallest eigenvalue of C. On the right, X* drawn as an ellipse is flattened to a line.

On the edge the determinant is zero, so one eigenvalue is zero: the optimal \(X\) is rank one. Seen as an ellipse — the unit circle stretched by \(X^{1/2}\), with semi-axes \(\sqrt{\lambda_i}\) — it is squashed flat into a line segment.

Solving the script’s SDP by hand

That picture holds in any size. The answer to

\[\min_X \; \operatorname{tr}(CX) \quad \text{s.t.} \quad X \succeq 0,\; \operatorname{tr} X = 1\]

is \(X^\star = \mathbf{v}\mathbf{v}^{\mathsf{T}}\), where \(\mathbf{v}\) is the unit eigenvector for the smallest eigenvalue of \(C\), and the optimal value is that eigenvalue. For the script’s seed, the eigenvalues of \(C\) are \(-0.966\), \(0.819\) and \(2.956\), so SCS should report an optimal value of about \(-0.966\) and an \(X\) with eigenvalues \((1, 0, 0)\) up to solver tolerance.

It is the Rayleigh quotient from the quadratic-forms lesson in disguise: \(\operatorname{tr}(C\mathbf{v}\mathbf{v}^{\mathsf{T}}) = \mathbf{v}^{\mathsf{T}}C\mathbf{v}\), smallest along the bottom eigenvector.

Why SDPs matter

This one has a closed form, but most do not, and SDPs are remarkably expressive: stability certificates for control systems (Lyapunov functions), relaxations of hard combinatorial problems like max-cut, and covariance estimation all become SDPs. The tendency towards low-rank optima, visible on the edge of the disc, is what makes many of those relaxations come out tight.

Try it yourself. Run sdp.py and compare problem.value with np.linalg.eigvalsh(C)[0]. Then add the constraint X[0, 0] == 0.5 and predict, using the disc picture, whether the optimum can still be rank one.

View source on GitHub →