Akshay Agrawal commited on
Commit
cef6f59
·
unverified ·
2 Parent(s): 94e0f7b 07062d3

Merge pull request #25 from marimo-team/aka/optimization-qp

Browse files
Files changed (1) hide show
  1. optimization/04_quadratic_program.py +265 -0
optimization/04_quadratic_program.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.13"
3
+ # dependencies = [
4
+ # "cvxpy==1.6.0",
5
+ # "marimo",
6
+ # "matplotlib==3.10.0",
7
+ # "numpy==2.2.2",
8
+ # "wigglystuff==0.1.9",
9
+ # ]
10
+ # ///
11
+
12
+ import marimo
13
+
14
+ __generated_with = "0.11.0"
15
+ app = marimo.App()
16
+
17
+
18
+ @app.cell(hide_code=True)
19
+ def _(mo):
20
+ mo.md(
21
+ r"""
22
+ # Quadratic program
23
+
24
+ A quadratic program is an optimization problem with a quadratic objective and
25
+ affine equality and inequality constraints. A common standard form is the
26
+ following:
27
+
28
+ \[
29
+ \begin{array}{ll}
30
+ \text{minimize} & (1/2)x^TPx + q^Tx\\
31
+ \text{subject to} & Gx \leq h \\
32
+ & Ax = b.
33
+ \end{array}
34
+ \]
35
+
36
+ Here $P \in \mathcal{S}^{n}_+$, $q \in \mathcal{R}^n$, $G \in \mathcal{R}^{m \times n}$, $h \in \mathcal{R}^m$, $A \in \mathcal{R}^{p \times n}$, and $b \in \mathcal{R}^p$ are problem data and $x \in \mathcal{R}^{n}$ is the optimization variable. The inequality constraint $Gx \leq h$ is elementwise.
37
+
38
+ **Why quadratic programming?** Quadratic programs are convex optimization problems that generalize both least-squares and linear programming.They can be solved efficiently and reliably, even in real-time.
39
+
40
+ **An example from finance.** A simple example of a quadratic program arises in finance. Suppose we have $n$ different stocks, an estimate $r \in \mathcal{R}^n$ of the expected return on each stock, and an estimate $\Sigma \in \mathcal{S}^{n}_+$ of the covariance of the returns. Then we solve the optimization problem
41
+
42
+ \[
43
+ \begin{array}{ll}
44
+ \text{minimize} & (1/2)x^T\Sigma x - r^Tx\\
45
+ \text{subject to} & x \geq 0 \\
46
+ & \mathbf{1}^Tx = 1,
47
+ \end{array}
48
+ \]
49
+
50
+ to find a nonnegative portfolio allocation $x \in \mathcal{R}^n_+$ that optimally balances expected return and variance of return.
51
+
52
+ When we solve a quadratic program, in addition to a solution $x^\star$, we obtain a dual solution $\lambda^\star$ corresponding to the inequality constraints. A positive entry $\lambda^\star_i$ indicates that the constraint $g_i^Tx \leq h_i$ holds with equality for $x^\star$ and suggests that changing $h_i$ would change the optimal value.
53
+ """
54
+ )
55
+ return
56
+
57
+
58
+ @app.cell(hide_code=True)
59
+ def _(mo):
60
+ mo.md(
61
+ r"""
62
+ ## Example
63
+
64
+ In this example, we use CVXPY to construct and solve a quadratic program.
65
+ """
66
+ )
67
+ return
68
+
69
+
70
+ @app.cell
71
+ def _():
72
+ import cvxpy as cp
73
+ import numpy as np
74
+ return cp, np
75
+
76
+
77
+ @app.cell(hide_code=True)
78
+ def _(mo):
79
+ mo.md("""First we generate synthetic data. In this problem, we don't include equality constraints, only inequality.""")
80
+ return
81
+
82
+
83
+ @app.cell
84
+ def _(np):
85
+ m = 4
86
+ n = 2
87
+
88
+ np.random.seed(1)
89
+ q = np.random.randn(n)
90
+ G = np.random.randn(m, n)
91
+ h = G @ np.random.randn(n)
92
+ return G, h, m, n, q
93
+
94
+
95
+ @app.cell(hide_code=True)
96
+ def _(mo, np):
97
+ import wigglystuff
98
+
99
+ P_widget = mo.ui.anywidget(
100
+ wigglystuff.Matrix(np.array([[4.0, -1.4], [-1.4, 4]]), step=0.1)
101
+ )
102
+
103
+ mo.md(
104
+ f"""
105
+ The quadratic form $P$ is equal to the symmetrized version of this
106
+ matrix:
107
+
108
+ {P_widget.center()}
109
+ """
110
+ )
111
+ return P_widget, wigglystuff
112
+
113
+
114
+ @app.cell
115
+ def _(P_widget, np):
116
+ P = 0.5 * (np.array(P_widget.matrix) + np.array(P_widget.matrix).T)
117
+ return (P,)
118
+
119
+
120
+ @app.cell(hide_code=True)
121
+ def _(mo):
122
+ mo.md(r"""Next, we specify the problem. Notice that we use the `quad_form` function from CVXPY to create the quadratic form $x^TPx$.""")
123
+ return
124
+
125
+
126
+ @app.cell
127
+ def _(G, P, cp, h, n, q):
128
+ x = cp.Variable(n)
129
+
130
+ problem = cp.Problem(
131
+ cp.Minimize((1 / 2) * cp.quad_form(x, P) + q.T @ x),
132
+ [G @ x <= h],
133
+ )
134
+ _ = problem.solve()
135
+ return problem, x
136
+
137
+
138
+ @app.cell(hide_code=True)
139
+ def _(mo, problem, x):
140
+ mo.md(
141
+ f"""
142
+ The optimal value is {problem.value:.04f}.
143
+
144
+ A solution $x$ is {mo.as_html(list(x.value))}
145
+ A dual solution is is {mo.as_html(list(problem.constraints[0].dual_value))}
146
+ """
147
+ )
148
+ return
149
+
150
+
151
+ @app.cell
152
+ def _(G, P, h, plot_contours, q, x):
153
+ plot_contours(P, G, h, q, x.value)
154
+ return
155
+
156
+
157
+ @app.cell(hide_code=True)
158
+ def _(mo):
159
+ mo.md(
160
+ r"""
161
+ In this plot, the gray shaded region is the feasible region (points satisfying the inequality), and the ellipses are level curves of the quadratic form.
162
+
163
+ **🌊 Try it!** Try changing the entries of $P$ above with your mouse. How do the
164
+ level curves and the optimal value of $x$ change? Can you explain what you see?
165
+ """
166
+ )
167
+ return
168
+
169
+
170
+ @app.cell(hide_code=True)
171
+ def _(P, mo):
172
+ mo.md(
173
+ rf"""
174
+ The above contour lines were generated with
175
+
176
+ \[
177
+ P= \begin{{bmatrix}}
178
+ {P[0, 0]:.01f} & {P[0, 1]:.01f} \\
179
+ {P[1, 0]:.01f} & {P[1, 1]:.01f} \\
180
+ \end{{bmatrix}}
181
+ \]
182
+ """
183
+ )
184
+ return
185
+
186
+
187
+ @app.cell(hide_code=True)
188
+ def _(np):
189
+ def plot_contours(P, G, h, q, x_star):
190
+ import matplotlib.pyplot as plt
191
+
192
+ # Create a grid of x and y values.
193
+ x = np.linspace(-5, 5, 400)
194
+ y = np.linspace(-5, 5, 400)
195
+ X, Y = np.meshgrid(x, y)
196
+
197
+ # Compute the quadratic form Q(x, y) = a*x^2 + 2*b*x*y + c*y^2.
198
+ # Here, a = P[0,0], b = P[0,1] (and P[1,0]), c = P[1,1]
199
+ Z = (
200
+ 0.5 * (P[0, 0] * X**2 + 2 * P[0, 1] * X * Y + P[1, 1] * Y**2)
201
+ + q[0] * X
202
+ + q[1] * Y
203
+ )
204
+
205
+ # --- Evaluate the constraints on the grid ---
206
+ # We stack X and Y to get a list of (x,y) points.
207
+ points = np.vstack([X.ravel(), Y.ravel()]).T
208
+
209
+ # Start with all points feasible
210
+ feasible = np.ones(points.shape[0], dtype=bool)
211
+
212
+ # Apply the inequality constraints Gx <= h.
213
+ # Each row of G and corresponding h defines a condition.
214
+ for i in range(G.shape[0]):
215
+ # For a given point x, the condition is: G[i,0]*x + G[i,1]*y <= h[i]
216
+ feasible &= points.dot(G[i]) <= h[i] + 1e-8 # small tolerance
217
+ # Reshape the boolean mask back to grid shape.
218
+ feasible_grid = feasible.reshape(X.shape)
219
+
220
+ # --- Plot the feasible region and contour lines---
221
+ plt.figure(figsize=(8, 6))
222
+
223
+ # Use contourf to fill the region where feasible_grid is True.
224
+ # We define two levels, so that points that are True (feasible) get one
225
+ # color.
226
+ plt.contourf(
227
+ X,
228
+ Y,
229
+ feasible_grid,
230
+ levels=[-0.5, 0.5, 1.5],
231
+ colors=["white", "gray"],
232
+ alpha=0.5,
233
+ )
234
+
235
+ contours = plt.contour(X, Y, Z, levels=10, cmap="viridis")
236
+ plt.clabel(contours, inline=True, fontsize=8)
237
+ plt.title("Feasible region and level curves")
238
+ plt.xlabel("$x_1$")
239
+ plt.ylabel("$y_2$")
240
+ # plt.colorbar(contours, label='Q(x, y)')
241
+
242
+ ax = plt.gca()
243
+ # Optionally, mark and label the point x_star.
244
+ ax.plot(x_star[0], x_star[1], "ko", markersize=5)
245
+ ax.text(
246
+ x_star[0],
247
+ x_star[1],
248
+ r"$\mathbf{x}^\star$",
249
+ color="black",
250
+ fontsize=12,
251
+ verticalalignment="bottom",
252
+ horizontalalignment="right",
253
+ )
254
+ return plt.gca()
255
+ return (plot_contours,)
256
+
257
+
258
+ @app.cell
259
+ def _():
260
+ import marimo as mo
261
+ return (mo,)
262
+
263
+
264
+ if __name__ == "__main__":
265
+ app.run()