Portfolio Optimization

In this problem, we will find the portfolio allocation that minimizes risk while achieving a given expected return $R_\mbox{target}$.

Suppose that we know the mean returns $R \in \mathbf{R}^n$ and the covariance $Q \in \mathbf{R}^{n \times n}$ of the $n$ assets. We would like to find a portfolio allocation $x \in \mathbf{R}^n$, $\sum_i x_i = 1$, minimizing the risk of the portfolio, which we measure as the variance $x^T Q x$ of the portfolio. The requirement that the portfolio allocation achieve the target expected return can be expressed as $x^T R >= R_\mbox{target}$. We suppose further that our portfolio allocation must comply with some lower and upper bounds on the allocation, $x_\mbox{lower} \leq x \leq x_\mbox{upper}$.

This problem can be written as

\begin{array}{ll} \mbox{minimize} & x^T Q x \\ \mbox{subject to} & x^T R >= R_\mbox{target} \\ & \sum_i x_i = 1 \\ & x_\mbox{lower} \leq x \leq x_\mbox{upper} \end{array}

where $x \in \mathbf{R}^n$ is our optimization variable.

We can solve this problem as follows.

In [9]:
using Convex, ECOS

# generate problem data
srand(0)
n = 50
R = 5*randn(n)
A = randn(n, 5)
Q = A * A' + diagm(rand(n))
R_target = 5
x_lower = 0
x_upper = 1

x = Variable(length(R))
p = minimize(quadform(x, Q), 
             x' * R >= R_target, 
             sum(x) == 1, 
             x_lower <= x, 
             x <= x_upper )

solve!(p, ECOSSolver(verbose = false)) 

# the minimal risk
p.optval
Out[9]:
0.025263946889697284

We see that we can achieve an extremely low risk portfolio (with variance .025) with the desired expected return.

The optimal portfolio invests in only about half of the assets.

In [17]:
sum(x.value.>1e-4)
Out[17]:
27

Let's take a look at the optimal portfolio we chose:

In [18]:
plot(x=1:n,y=x.value,Geom.bar,Guide.xlabel("Asset Index"),Guide.ylabel("Fraction of Portfolio"))
Out[18]:
Asset Index -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 -50 0 50 100 -50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 Fraction of Portfolio -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 -0.155 -0.150 -0.145 -0.140 -0.135 -0.130 -0.125 -0.120 -0.115 -0.110 -0.105 -0.100 -0.095 -0.090 -0.085 -0.080 -0.075 -0.070 -0.065 -0.060 -0.055 -0.050 -0.045 -0.040 -0.035 -0.030 -0.025 -0.020 -0.015 -0.010 -0.005 0.000 0.005 0.010 0.015 0.020 0.025 0.030 0.035 0.040 0.045 0.050 0.055 0.060 0.065 0.070 0.075 0.080 0.085 0.090 0.095 0.100 0.105 0.110 0.115 0.120 0.125 0.130 0.135 0.140 0.145 0.150 0.155 0.160 0.165 0.170 0.175 0.180 0.185 0.190 0.195 0.200 0.205 0.210 0.215 0.220 0.225 0.230 0.235 0.240 0.245 0.250 0.255 0.260 0.265 0.270 0.275 0.280 0.285 0.290 0.295 0.300 0.305 -0.2 0.0 0.2 0.4 -0.16 -0.15 -0.14 -0.13 -0.12 -0.11 -0.10 -0.09 -0.08 -0.07 -0.06 -0.05 -0.04 -0.03 -0.02 -0.01 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.30 0.31