Quadratic Programming





Kerry Back

Quadratic Program

  • Choose \(x\) to minimize a quadratic objective

\[\frac{1}{2}x^\top P x + q^\top x\]

  • subject to equality constraints \(Ax=b\)

  • and inequality constraints \(Gx \le h\)

  • for given \(P, q, A, b, G\), and \(h\).

Example

minimize \(x_1^2 + x_2^2 - 2x_1 - x_2\) subject to \(x_1 \ge 0\), \(x_2 \ge 0\) and \(x_1+x_2=1\).

\[P = \begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix}\] \[G =\begin{pmatrix} -1 & 0 \\ 0 & -1 \end{pmatrix}\] \[A = \begin{pmatrix} 1 & 1 \end{pmatrix}\]

\[ q = \begin{pmatrix} - 2 \\ - 1 \end{pmatrix}\] \[h= \begin{pmatrix} 0 \\ 0 \end{pmatrix}\] \[b = \begin{pmatrix} 1 \end{pmatrix}\]

Solution with cvxpy

import cvxpy as cp
import numpy as np

x = cp.Variable(2)
P = np.array([[2., 0.], [0., 2.]])
q = np.array([-2., -1.])
prob = cp.Problem(
    cp.Minimize(0.5 * cp.quad_form(x, P) + q @ x),
    [x >= 0, cp.sum(x) == 1]
)
prob.solve()
print(x.value)
[0.75 0.25]

Mean-Variance Frontier

minimize \((1/2)w^\top C w\) subject to

\[r_f + (\bar{r}-r_f1_n)^\top w = \bar{r}_{\text{targ}}\]

\(P=C\)
\(q=0\)
\(G=0\)
\(h=0\)
\(A = (\bar{r}-r_f1_n)^\top\)
\(b=(\bar{r}_{\text{targ}} - r_f)\)

Optimal portfolio

  • maximize expected return minus (1/2)raver times variance
  • equivalent to: minimize (1/2)raver times variance minus expected return

\(P=\text{raver} \times C\)
\(q=-(\bar{r}-r_f1_n)\)
\(G=0\)
\(h=0\)
\(A = 0\)
\(b=0\)