hhatefi

Curious about computing, modeling and analysis

Portfolio Optimization

Modern portfolio theory provides a framework to address asset allocation problem and construct portfolios. The idea was first put forward by Harry Markowitz in 1952 and since then has been enhanced both in theory and practice and found its application in financial industry. In this post, I formulate asset allocation problem and explain with a simple example how to solve it using mean-variance optimization.

Problem formulation as mean-variance optimization

Assume an investor has to select a portfolio comprised of \(N\) risky assets. The investor’s selection is represented by vector \(\boldsymbol w=(w_1, w_2,\dotsc,w_N)^T\), where \(w_i\) denotes the fraction of the capital invested in asset \(i\). The fractions should indeed sum up to one:

\[ \sum_{i=1}^{N}w_i = 1 \]

Assume for the moment that short selling is possible, which means weights can be negative. Now if the return of each asset is known, it will be possible to quantify the performance of the portfolio. However, asserts are risky, which means their returns vary and so does the portfolio return. Therefore, portfolio and asset returns are modeled as random variables. Let random variable \(R_i\) denote the return of asset \(i\) and so random vector \(\boldsymbol R=(R_1,R_2,\dotsc,R_N)^T\) be the return of all assets. The overall return of the portfolio is then specified by random variable

\[ R_p=\sum_{i=1}^{N}w_iR_i=\boldsymbol w^T\boldsymbol R \]

Moreover, let \(\boldsymbol\mu=(\mu_1,\mu_2,\dotsc,\mu_N)^T\) be the vector of expected returns, where \(\mu_i\) is the expected return of asset \(i\), i.e. \(\mu_i=E(R_i)\). The performance of the portfolio can then be assessed by its expected return \(\mu_p=\boldsymbol w^T\boldsymbol\mu\). Now the question is how to quantify the risk of the portfolio. Markowitz used the overall variance of the portfolio, which is the variance of random variable \(R_p\), as the risk measure. It is not hard to see that the variance can be computed as follows:

\[ \sigma_p=\boldsymbol w^T\Sigma\boldsymbol w \]

where \(\Sigma=\{\sigma_{ij}\}_{1\le i,j\le N}\) is the covariance matrix of asset returns with \(\sigma_{ij}\) being the covariance between the return of asset \(i\) and \(j\).

Modern portfolio theory tries to select portfolios by offering a trade-off between their risk and return. It aims to compute the least risky portfolio for a given return level or the most profitable portfolio entailing a predefined risk level. It can be formulated as an optimization problem in variant ways. Here, I explain the risk minimization formulation, where the portfolio risk is subject to minimization while its expected return is kept at a predefined level. \[ \min~\boldsymbol w^T \Sigma \boldsymbol w \] subject to

\begin{align*} \boldsymbol w^T \boldsymbol u&=\mu_0\\ \boldsymbol w^T\boldsymbol 1&=1 \end{align*}

where \(\boldsymbol 1=(1,1,\dotso,1)^T\) denotes all-one vector. The last constraint assures that the asset weights sum up to one. Basically, the optimization problem computes asset weights \(\boldsymbol w\) in such a way that the constructed portfolio involves the minimum possible risk and furthermore produces a desired return given by \(\mu_0\). Portfolio managers usually add more complicated constraints and objective terms to change this formulation based on their own requirements. For the sake of simplicity, I consider only the above optimization problem without any extra constraints and objective terms.

Computing the efficient frontier

This section demonstrates the set of all efficient portfolios that are selected by the above optimization problem. The risk minimization formulation chooses for any given level of expected return the portfolio with the minimum risk from the set of all possible portfolios. Such minimum variance portfolios for different expected returns constitute a set, which is called efficient frontier. In order to find this set, the optimization problem should be solved for all given expected returns. The risk minimization formulation, described above, is a quadratic optimization problem with equality constraints and has an analytical solution. The problem can be solved using the method of Lagrange multipliers (see e.g. Fabozzi et. al. 2010, Section 8) with the solution given by:

\begin{equation} \label{eq:ef} \sigma_0^2=\frac{A\mu_0^2-2B\mu_0+C}{AC-B^2} \end{equation}

where \(A=\boldsymbol 1^T\Sigma^{-1}\boldsymbol 1\), \(B=\boldsymbol 1^T\Sigma^{-1}\boldsymbol\mu\) and \(C=\boldsymbol\mu^T \Sigma^{-1}\boldsymbol\mu\). According to Equation \(\eqref{eq:ef}\), the minimum variance \(\sigma_0^2\) of a portfolio form a quadratic function of \(\mu_0\). This quadratic function has a minimum value, which corresponds to the portfolio with the global minimum variance. This can be computed by:

\[ \frac{\mathrm{d}\sigma_0^2}{\mathrm{d}\mu_0}=\frac{2A\mu_0-2B}{AC-B^2}=0\Rightarrow\mu_0=\frac{A}{B}=\frac{\boldsymbol 1^T\Sigma^{-1}}{\boldsymbol 1^T\Sigma^{-1}\boldsymbol 1}\boldsymbol\mu \]

Since we know that \(\mu_0=\boldsymbol w^T\boldsymbol\mu\) then the global minimum variance portfolio becomes:

\[ \boldsymbol w_{\mathrm{min}}=\frac{\Sigma^{-1}\boldsymbol 1}{\boldsymbol 1^T\Sigma^{-1}\boldsymbol 1} \]

This portfolio is not interesting only because it is the least risky one, but also because its construction does not require any estimation of expected returns (vector \(\boldsymbol\mu\)). That is important, as such an estimation is subject to error and uncertainty, particularly if it relies on historical price data.

With all the formulas given above, computing the efficient frontier will be straightforward. At first we pick up our universe, the set of all assets we want to invest in and then download their price data. The follwing python code uses yfinance package for this purpose.

import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

assets = "GOOG MA AMZN GM AAPL"
data = yf.download(assets, start="2019-01-01", end="2019-12-31")['Adj Close']

One of the main challenges of portfolio optimization is the estimation of expected returns \(\boldsymbol\mu\) and the covariance matrix \(\Sigma\). One approach is to estimate them by sample mean and covariance, respectively. However, it is known to provide poor estimation and to show high sensitivity to price fluctuation, specially for expected returns. There are several other estimations that are generally considered more precise and more stable. Package pyopt provides different models for computing expected returns and the covariance matrix. Here we use exponential moving average model for expected returns and exponential covariance for the covariance matrix.

mu=expected_returns.return_model(data, method='ema_historical_return')
sigma=risk_models.risk_matrix(data, method='exp_cov')

Now all the ingredients are ready to derive the efficient frontier.

import matplotlib.pyplot as plt
import math

sigma_inv=np.linalg.inv(sigma)
all_ones=np.ones(sigma.shape[0])
A=all_ones@sigma_inv@all_ones
B=all_ones@sigma_inv@mu
C=mu@sigma_inv@mu
delta=A*C-B*B
compute_risk=lambda mu0: np.sqrt((A*mu0*mu0 - 2*B*mu0 + C) / delta)
mu0=np.linspace(0, 3.5, 40)
sigma0=compute_risk(mu0)
plt.plot(sigma0, mu0)

The result is illustrated in the next diagram.

Figure 1: Efficient frontier without risk free asset

Figure 1: Efficient frontier without risk free asset

As it can be observed, increasing the expected return (\(\mu_0\)) leads to portfolios that take higher risks. Selecting a portfolio with lower risk, on the other hand, decrease its performance as well.

We can also calculate the global minimum portfolio weights, its risk and return characteristics.

gmin_mu0=B/A                       # the expected return at the global minimum risk
gmin_sigma0=compute_risk(gmin_mu0) # the global minimum risk
w_gmin=sigma_inv@all_ones/A        # the global minimum risk portfolio
AAPL AMZN GM GOOG MA
Weights 9.62% 22.08% 23.46% 19.48% 25.37%
Return 1.04 0.19 0.10 0.32 0.53
Risk 0.22 0.20 0.24 0.21 0.21

Capital market line and the tangency portfolio

Investors can add a risk free asset to improve the performance of their portfolio. A risk free asset is an asset with a fixed return \(R_f\) that might be selected by investors together with a combination of \(N\) risky assets as before. On the efficient frontier all portfolios comprises of only risky assets. Adding the risk-free asset implies that the weights of risky assets do not necessarily sum up to one, as the difference is invested in the risk free asset. Therefore, both risky and risk-free assets contributes to the expected return of the portfolio:

\begin{align*} \mu_p=&\boldsymbol w^T\boldsymbol\mu+(1-\boldsymbol w^T\boldsymbol 1)R_f\\\\ \end{align*}

However, the overall portfolio risk depends only on the risky assets, since the risk free asset has zero variance and is independent of the risky assets.

\begin{align*} \sigma_p=&\boldsymbol w^T\Sigma\boldsymbol w \end{align*}

Putting the risk and the return of the portfolio into risk minimization formulation gives:

\[ \min~\boldsymbol w^T\Sigma\boldsymbol w \] subject to \[ \mu_0=\boldsymbol w^T\boldsymbol\mu+(1-\boldsymbol w^T\boldsymbol 1)R_f \]

The optimization problem can again be solved by the method of Lagrange multipliers. The optimal portfolio weights are derived as follows:

\begin{equation} \label{eq:wrf} \boldsymbol w=K\Sigma^{-1}(\boldsymbol\mu-R_f\boldsymbol 1) \end{equation}

where \[ K=\frac{\mu_0-R_f}{(\boldsymbol\mu-R_f\boldsymbol 1)^T\Sigma^{-1}(\boldsymbol\mu-R_f\boldsymbol 1)}=\frac{\mu_0-R_f}{AR_f^2-2R_fB+C} \]

In order to illustrate the relation between the risk and the return, we can derive \(\sigma_0^2\) as a function of \(\mu_0\) using \eqref{eq:wrf}:

\begin{equation*} \sigma_0^2=\boldsymbol w^T\Sigma\boldsymbol w=\frac{(\mu_0-R_f)^2}{(\boldsymbol\mu-R_f\boldsymbol 1)^T\Sigma^{-1}(\boldsymbol\mu-R_f\boldsymbol 1)}=\frac{(\mu_0-R_f)^2}{AR_f^2-2R_fB+C} \end{equation*}

This time the expected return and its corresponding minimal risk lie on a line. For \(\mu_0\ge R_f\):

\begin{equation} \label{eq:cml} \sigma_0=\frac{\mu_0-R_f}{\sqrt{(\boldsymbol\mu-R_f\boldsymbol 1)^T\Sigma^{-1}(\boldsymbol\mu-R_f\boldsymbol 1)}}=\frac{\mu_0-R_f}{\sqrt{AR_f^2-2R_fB+C}} \end{equation}

There are two specially interesting portfolios on line \eqref{eq:cml}. The portfolio with zero risk coincides with \(\mu_0\) being \(R_f\), which contains only the risk free asset. On the other side, there is a portfolio comprising merely the risky assets. Because it only includes the risky assets, it must also lie on the efficient frontier, represented by \eqref{eq:ef}. Therefore, this portfolio is located on the intersection between \eqref{eq:ef} and \eqref{eq:cml}. In addition, it can be shown that \eqref{eq:cml} is a tangent line to \eqref{eq:ef}. The portfolio at the intersection point is thereby called tangancy portfolio. Under certain condition (Fama 1970), it is also named as market portfolio. Moreover, line \eqref{eq:cml} is referred to as the Capital Market Line (CML).

To compute the tangency portfolio, it is enough to find the intersection between \eqref{eq:cml} and \eqref{eq:ef}. As explained before, the intersection embodies only the risky assets. Let \(\boldsymbol w_m\) be the market portfolio, then it holds that \(\boldsymbol w_m^T\boldsymbol 1=1\), as the weight of risk free asset is zero. Therefore:

\[ \boldsymbol w^T\boldsymbol 1=\left(K\Sigma^{-1}(\boldsymbol\mu-R_f\boldsymbol 1)\right)^T\boldsymbol 1=1\Rightarrow K=\frac{1}{\boldsymbol 1^T\Sigma(\boldsymbol\mu-R_f\boldsymbol 1)} \]

And finally substituting \(K\) into \(\eqref{eq:wrf}\) gives the market portfolio: \[ \boldsymbol w_m=\frac{\Sigma^{-1}(\boldsymbol\mu-R_f\boldsymbol 1)}{\boldsymbol 1^T\Sigma(\boldsymbol\mu-R_f\boldsymbol 1)} \]

Furthermore, putting \(\boldsymbol w_m\) into \(\mu_0=\boldsymbol w_m^T\boldsymbol\mu\) and \(\sigma_0=\boldsymbol w_m^T\Sigma\boldsymbol w_m\) gives:

\begin{align*} \mu_0=&\frac{C-BR_f}{B-AR_f}\\ \sigma_0=&\frac{\sqrt{AR_f^2-2R_fB+C}}{B-AR_f} \end{align*}

Now we can put all the formulas together to compute CML and the tangency portfolio:

import math
rf = 0.05
D=A*rf*rf-2*rf*B+C
sigma_rf=(mu0-rf)/math.sqrt(D)
w_m=sigma_inv@(mu - rf)/(all_ones@sigma@(mu - rf))
mu_t=(C-B*rf)/(B-A*rf)
sigma_t=math.sqrt(D)/(B-A*rf)

The result is illustrated in the following figure. It can be observed that a portfolio on the CML outperforms the corresponding portfolio on the efficient frontier with the same risk level. That is to say, introducing the risk free asset improves the performance of the portfolio.

Figure 2: Efficient frontier with capital market line

Figure 2: Efficient frontier with capital market line

Conclusions

Modern portfolio theory utilizes mean-variance optimization to construct portfolios by offering a trade-off between their risk and their return. The optimization problem can be formulated in different ways. Risk minimization formulation computes the minimum risk portfolio for a desired expected return. Including a risk free asset into asset universe can improve portfolio selection.

References

Fabozzi et al. “Quantitative Equity Investing: Techniques and Strategies,” John Wiley & Sons (2010)

Eugene F. Fama, “Efficient Capital Markets: A Review of Theory and Empirical Work,” Journal of Finance, 25 (1970), pp. 383-417