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 cvxopt

from cvxopt import matrix
from cvxopt.solvers import  qp

P = matrix([[2., 0.], [0., 2.]], (2, 2))
q = matrix([-2., -1.], (2, 1))
G = matrix([[-1., 0.], [0., -1.]], (2, 2))
h = matrix([0., 0.], (2, 1))
A = matrix([1., 1.], (1, 2))
b = matrix([1.], (1, 1))
sol = qp(P=P, q=q, G=G, h=h, A=A, b=b)
print(sol["x"])
     pcost       dcost       gap    pres   dres
 0: -1.1111e+00 -2.2222e+00  1e+00  1e-16  1e+00
 1: -1.1231e+00 -1.1680e+00  4e-02  1e-16  4e-02
 2: -1.1250e+00 -1.1261e+00  1e-03  2e-16  3e-04
 3: -1.1250e+00 -1.1250e+00  1e-05  6e-17  3e-06
 4: -1.1250e+00 -1.1250e+00  1e-07  3e-16  3e-08
Optimal solution found.
[ 7.50e-01]
[ 2.50e-01]

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\)