|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.13" |
| 3 | +# dependencies = [ |
| 4 | +# "cvxpy==1.6.0", |
| 5 | +# "marimo", |
| 6 | +# "numpy==2.2.2", |
| 7 | +# "wigglystuff==0.1.9", |
| 8 | +# ] |
| 9 | +# /// |
| 10 | + |
| 11 | +import marimo |
| 12 | + |
| 13 | +__generated_with = "0.11.2" |
| 14 | +app = marimo.App() |
| 15 | + |
| 16 | + |
| 17 | +@app.cell(hide_code=True) |
| 18 | +def _(mo): |
| 19 | + mo.md(r"""# Semidefinite program""") |
| 20 | + return |
| 21 | + |
| 22 | + |
| 23 | +@app.cell(hide_code=True) |
| 24 | +def _(mo): |
| 25 | + mo.md( |
| 26 | + r""" |
| 27 | + _This notebook introduces an advanced topic._ A semidefinite program (SDP) is an optimization problem of the form |
| 28 | +
|
| 29 | + \[ |
| 30 | + \begin{array}{ll} |
| 31 | + \text{minimize} & \mathbf{tr}(CX) \\ |
| 32 | + \text{subject to} & \mathbf{tr}(A_iX) = b_i, \quad i=1,\ldots,p \\ |
| 33 | + & X \succeq 0, |
| 34 | + \end{array} |
| 35 | + \] |
| 36 | +
|
| 37 | + where $\mathbf{tr}$ is the trace function, $X \in \mathcal{S}^{n}$ is the optimization variable and $C, A_1, \ldots, A_p \in \mathcal{S}^{n}$, and $b_1, \ldots, b_p \in \mathcal{R}$ are problem data, and $X \succeq 0$ is a matrix inequality. Here $\mathcal{S}^{n}$ denotes the set of $n$-by-$n$ symmetric matrices. |
| 38 | +
|
| 39 | + **Example.** An example of an SDP is to complete a covariance matrix $\tilde \Sigma \in \mathcal{S}^{n}_+$ with missing entries $M \subset \{1,\ldots,n\} \times \{1,\ldots,n\}$: |
| 40 | +
|
| 41 | + \[ |
| 42 | + \begin{array}{ll} |
| 43 | + \text{minimize} & 0 \\ |
| 44 | + \text{subject to} & \Sigma_{ij} = \tilde \Sigma_{ij}, \quad (i,j) \notin M \\ |
| 45 | + & \Sigma \succeq 0, |
| 46 | + \end{array} |
| 47 | + \] |
| 48 | + """ |
| 49 | + ) |
| 50 | + return |
| 51 | + |
| 52 | + |
| 53 | +@app.cell(hide_code=True) |
| 54 | +def _(mo): |
| 55 | + mo.md( |
| 56 | + r""" |
| 57 | + ## Example |
| 58 | +
|
| 59 | + In the following code, we show how to specify and solve an SDP with CVXPY. |
| 60 | + """ |
| 61 | + ) |
| 62 | + return |
| 63 | + |
| 64 | + |
| 65 | +@app.cell |
| 66 | +def _(): |
| 67 | + import cvxpy as cp |
| 68 | + import numpy as np |
| 69 | + return cp, np |
| 70 | + |
| 71 | + |
| 72 | +@app.cell |
| 73 | +def _(np): |
| 74 | + # Generate a random SDP. |
| 75 | + n = 3 |
| 76 | + p = 3 |
| 77 | + np.random.seed(1) |
| 78 | + C = np.random.randn(n, n) |
| 79 | + A = [] |
| 80 | + b = [] |
| 81 | + for i in range(p): |
| 82 | + A.append(np.random.randn(n, n)) |
| 83 | + b.append(np.random.randn()) |
| 84 | + return A, C, b, i, n, p |
| 85 | + |
| 86 | + |
| 87 | +@app.cell |
| 88 | +def _(A, C, b, cp, n, p): |
| 89 | + # Create a symmetric matrix variable. |
| 90 | + X = cp.Variable((n, n), symmetric=True) |
| 91 | + |
| 92 | + # The operator >> denotes matrix inequality, with X >> 0 constraining X |
| 93 | + # to be positive semidefinite |
| 94 | + constraints = [X >> 0] |
| 95 | + constraints += [cp.trace(A[i] @ X) == b[i] for i in range(p)] |
| 96 | + prob = cp.Problem(cp.Minimize(cp.trace(C @ X)), constraints) |
| 97 | + _ = prob.solve() |
| 98 | + return X, constraints, prob |
| 99 | + |
| 100 | + |
| 101 | +@app.cell |
| 102 | +def _(X, mo, prob, wigglystuff): |
| 103 | + mo.md( |
| 104 | + f""" |
| 105 | + The optimal value is {prob.value:0.4f}. |
| 106 | +
|
| 107 | + A solution for $X$ is (rounded to the nearest decimal) is: |
| 108 | + |
| 109 | + {mo.ui.anywidget(wigglystuff.Matrix(X.value)).center()} |
| 110 | + """ |
| 111 | + ) |
| 112 | + return |
| 113 | + |
| 114 | + |
| 115 | +@app.cell |
| 116 | +def _(): |
| 117 | + import wigglystuff |
| 118 | + return (wigglystuff,) |
| 119 | + |
| 120 | + |
| 121 | +@app.cell |
| 122 | +def _(): |
| 123 | + import marimo as mo |
| 124 | + return (mo,) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + app.run() |
0 commit comments