In classical Markowitz Portfolio Theory, we have a one-period portfolio choice problem where by we,
The general setup is that we have N assets with stochastic one period returns and we denote the vector of expected returns by a $n \times 1$ vector $\mathbf{R}$. The returns covariance matrix between assets is specified by the $N \times N$ matrix $\mathbf{\Sigma}$, and the portfolio weights by a $N \times 1$ vector $\lambda$.
The expected return of our portfolio corresponding to the weights $\lambda$ is, $$ \overline{\mathbf{R}}_{\mathsf{\Pi}} = \lambda^T \mathbf{R} $$ The variance of portfolio returns is given by, $$ \sigma_{\mathsf{\Pi}}^2 = \lambda^T \mathbf{\Sigma}\, \lambda $$ ( Note : the above form for variance is from standard results on covariances of linear combinations )
Our optimization problem is then, $$ \begin{aligned} \min_{\lambda}\sigma_{\mathsf{\Pi}}^2 &= \min_{\lambda} \lambda^T \mathbf{\Sigma}\, \lambda\\ \text{subject to }\quad & \lambda^T \mathbf{R} = \mu, \quad \lambda^T \mathbb{1} = 1 \end{aligned} $$
In the above, the first condition is the constraint on the specified level of return $\mu$, while the second is the condition that the portfolio weights sum to 1.
The solution of the system is given by the method of Lagrangian Multipliers. The Lagrange function for our problem is,
$$ \mathsf{L} = \lambda^T \mathbf{\Sigma}\, \lambda + \alpha( \mu - \lambda^T \mathbf{R} ) + \beta(1-\lambda^T \mathbb{1}) $$Using matrix calculus, we differentiate $\mathsf{L}$ with respect to the vector $ \lambda^T$,
$$ \frac{\partial \mathsf{L}}{\partial \lambda^T} = 2\mathbf{\Sigma}\, \lambda -\alpha\,\mathbf{R} -\beta\,\mathbb{1} $$where in the above we have used the result $\frac{\partial}{\partial \mathbf{x}^T} \mathbf{x}^T \mathbf{A} \mathbf{x}=( \mathbf{A}^T + \mathbf{A} )\mathbf{x}= 2\mathbf{A} \mathbf{x}$ if $\mathbf{A}$ is symmetric. Setting $\frac{\partial \mathsf{L}}{\partial \lambda^T} = \mathbf{0}$ gives,
$$ 2\mathbf{\Sigma} \lambda = \alpha\,\mathbf{R} + \beta\,\mathbb{1} $$which gives us the value of $\lambda$ in terms of the Langrangian multipliers $\alpha$ and $\beta$. With $\mathbf{\Sigma}$ strictly positive-definite, we can invert it to get,
$$ \lambda = \frac{1}{2} \mathbf{\Sigma}^{-1}(\alpha\,\mathbf{R} + \beta\,\mathbb{1}) $$Recall that our portfolio variance is given by $\sigma_{\mathsf{\Pi}}^2 = \lambda^T \mathbf{\Sigma}\, \lambda$. Substituting in the value of $\lambda$ gives,
$$ \begin{aligned} \sigma_{\mathsf{\Pi}}^2 &= \lambda^T \mathbf{\Sigma}\, \lambda\\ &= \left(\frac{1}{2} \mathbf{\Sigma}^{-1}(\alpha\,\mathbf{R} + \beta\,\mathbb{1})\right)^T \mathbf{\Sigma}\left(\frac{1}{2} \mathbf{\Sigma}^{-1}(\alpha\,\mathbf{R} + \beta\,\mathbb{1})\right)\\ &= \frac{1}{4}\left( \mathbf{\Sigma}^{-1}(\alpha\,\mathbf{R} + \beta\,\mathbb{1})\right)^T (\alpha\,\mathbf{R} + \beta\,\mathbb{1})\\ &= \frac{1}{4}\left( \mathbf{R}^T \mathbf{\Sigma}^{-1}\mathbf{R} \alpha^2\,+ \alpha\beta\,\mathbf{R}^T \mathbf{\Sigma}^{-1}\mathbb{1} + \alpha\beta \mathbf{R}^T \mathbf{\Sigma}^{-1} \mathbb{1} +\mathbb{1}^T \mathbf{\Sigma}^{-1}\mathbb{1}\beta^2 \right) \\ &= \frac{1}{4}\left( \underbrace{\mathbf{R}^T \mathbf{\Sigma}^{-1}\mathbf{R} }_{a}\alpha^2\, + 2\alpha\beta\,\underbrace{\mathbf{R}^T \mathbf{\Sigma}^{-1}\mathbb{1}}_{b} +\underbrace{\mathbb{1}^T \mathbf{\Sigma}^{-1}\mathbb{1}}_{c}\beta^2 \right) \\ &=\frac{1}{4}\left( a \alpha^2\, + 2b \alpha\beta\ + c \beta^2 \right) \end{aligned} $$Next we differentiate the Lagrange function with respect to the multipliers $\alpha$ and $\beta$ and set them to zero.
$\frac{\partial \mathsf{L}}{\partial \alpha} = 0$, and $\frac{\partial \mathsf{L}}{\partial \beta}=0$ recovers the two constraints, $$ \lambda^T\mathbf{R} = \mu, \quad \lambda^T \mathbb{1} = 1 $$
Substituting $\lambda$ back into these two equations gives,
$ \frac{1}{2} \left(\mathbf{\Sigma}^{-1}(\alpha\,\mathbf{R} + \beta\,\mathbb{1})\right)^T \mathbf{R} = \mu \Rightarrow \frac{1}{2} ( a \alpha + b \beta ) = \mu $
$ \frac{1}{2} \left(\mathbf{\Sigma}^{-1}(\alpha\,\mathbf{R} + \beta\,\mathbb{1})\right)^T \mathbb{1} = 1 \Rightarrow \frac{1}{2}(\alpha b + c \beta) = 1$
This gives the linear system,
$$ \frac{1}{2}\begin{pmatrix} a & b \\ b & c \\ \end{pmatrix} \begin{pmatrix} \alpha \\ \beta \\ \end{pmatrix} = \begin{pmatrix} \mu \\ 1 \\ \end{pmatrix} $$The $2\times 2$ matrix here is invertible provided $\mathbf{\Sigma}$ is strictly positive definite. Given this, we can solve the system to get,
$$ \begin{pmatrix} \alpha \\ \beta \\ \end{pmatrix}=\frac{2}{ac-b^2}\begin{pmatrix} c\mu-b \\ a-b\mu \\ \end{pmatrix} $$Substituting back the values for $\alpha$ and $\beta$ gives the optimal allocation vector $\lambda$ and the variance of the minimum risk portfolio.
In the following, we show sample code in Python for constructing the efficient frontier of optimal (expected-return, portfolio-variance) pairs.
import numpy as np
from numpy.linalg import inv
from scipy.linalg import lu_solve, lu_factor
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from IPython.display import display, HTML
%matplotlib inline
We
d = {'E[R]' : [2., 3., 4.],
'$\sigma$' : [2., 3., 4.]}
df = pd.DataFrame(d, index = pd.Series(['S1', 'S2', 'S3']))
display ( HTML('We use the following three securities ( with given mean and variances )\
below for our example, <br>' + df.to_html()))
def plot_frontier():
R = np.array([2.,3.,4.]) # vector of expected returns
cov_mat = np.array([[2.,1.,0.], # the covariance matrix
[1.,3.,2.],
[0.,2.,4.] ])
inv_cov_mat = inv( cov_mat )
ones = np.ones_like( R )
a = R.T.dot( inv_cov_mat.dot( R ) )
b = R.T.dot( inv_cov_mat.dot( ones ) )
c = ones.T.dot( inv_cov_mat.dot( ones ) )
# solve for the lagrangian multipliers
A = 0.5 * np.array( [[a, b],
[b, c]] )
def solve( mu ):
k = np.array( [mu, 1.] )
alpha, beta = lu_solve( lu_factor( A ), k )
# now do backwards substitution to solve for lambda, the portfolio weights
Lambda = ( 0.5 * alpha * inv_cov_mat.dot( R )) + ( 0.5 * beta * inv_cov_mat.dot( ones ))
StdDev = np.sqrt( Lambda.T.dot( cov_mat.dot( Lambda ) ) )
return StdDev
mus = np.linspace( 1., 4.5, 5000 )
stddevs = list( solve( mu ) for mu in mus )
min_stddev = np.argmin( stddevs )
plt.figure( figsize=(8,6))
plt.plot( stddevs, mus, color='gray')
lower_boundary_x, lower_boundary_y = stddevs[:min_stddev], mus[:min_stddev]
upper_boundary_x, upper_boundary_y = stddevs[min_stddev:], mus[min_stddev:]
new_lower = np.pad( lower_boundary_y[::-1],
(0, len( upper_boundary_x ) - len( lower_boundary_y ) ), 'edge' )
plt.fill_between( upper_boundary_x, upper_boundary_y, new_lower, facecolor='pink',
alpha=0.3)
plt.plot( np.sqrt([2.,3.,4.]) , [2.,3.,4.], 'ro') # individual securities's risk-return
plt.ylabel( 'Expected Return R' )
plt.xlabel( 'Std Dev of Expected Returns $\sigma$' )
plt.title( 'The opportunity set and its boundary' )
plt.annotate('S1', ([np.sqrt(2.) + 0.05, 2.]) )
plt.annotate('S2', ([np.sqrt(3.) + 0.02, 3.]) )
plt.annotate('S3', ([np.sqrt(4.) + 0.02, 4. + 0.05]) )
plot_frontier()
The above figure shows the efficient frontier, on which we should chose our portfolios. The opportunity set is the shaded region, which represents all possible combinations of weights we can use to form our portfolio, although not always efficient.