# Modern Portfolio Theory and Efficient Frontier

## Constructing a portfolio

Fortunately, we aren’t limited by the selection of available stocks – we may build our own portfolio from stocks. We create a portfolio of assets by giving each a weight: We take $\mathbf{w} = (w_1, \ldots, w_n) \in {\mathbb{R}}^n$ where $w_i$ corresponds to the weight of asset $i$. Note that we require $\sum_{i=1}^n w_i = 1$ for a 100% investment. Then the return of our portfolio is the transformed random variable

\begin{equation*} P = w_1 X_1 + w_2 X_2 + \cdots + w_n X_n = \mathbf{w} \cdot (X_1, \ldots, X_n). \end{equation*}

Its expectation is simply

\begin{equation*} {\mathrm{E}}[P] = w_1 {\mathrm{E}}[X_1] + \cdots + w_n {\mathrm{E}}[X_n] = \mathbf{w} \cdot (\mu_1, \ldots, \mu_n), \end{equation*}

and its variance

\begin{equation*} {\mathrm{Var}}(P) = {\mathrm{Var}}(w_1 X_1 + \cdots + w_n X_n). \end{equation*}

We know from statistics that for random variables $A,B$ and scalars $\mu, {\lambda}$

\begin{equation*} {\mathrm{Var}}({\lambda}A + \mu B) = {\lambda}^2 {\mathrm{Var}}(A) + \mu^2 {\mathrm{Var}}(B) + {\lambda}\mu {\mathrm{Cov}}(A,B) \end{equation*}

where ${\mathrm{Cov}}(A,B)$ is the covariance between two random variables. If we write this out for our equation, we obtain

\begin{equation*} {\mathrm{Var}}(P) = \mathbf{w} \cdot \Sigma \mathbf{w} \end{equation*}

where $\Sigma$ is the covariance matrix for $X_1, \ldots, X_n$. The covariance matrix contains the covariances between each pair of stocks:

\begin{equation*} \Sigma = \begin{pmatrix} \sigma_{11} & \cdots & \sigma_{1n} \\ \vdots & \ddots & \vdots \\ \sigma_{n1} & \cdots & \sigma_{nn} \end{pmatrix} \end{equation*}

in which each $\sigma_{ij}$ denotes the covariance between stock $X_i$ and $X_j$. Like the mean and variance, we don’t know the exact covariances and can only estimate them based on estimators.

## Optimization

The question then becomes, what should each individual weight be so that our expected return is maximized and the variance minimized? This figure from Markowitz’ original paper from 1952 illustrates this well (note that the axes are swapped here): The efficient combination of portfolios we call the efficient frontier. If your portfolio is on this frontier, then it is optimal in the sense that there is no portfolio with the same risk and higher return or with the same return and lower risk. To compute, we first find a set of expected returns $S \subset {\mathbb{R}}$ that are attainable. Then we pick $\mu_0 \in S$ as our desired return baseline and find weights that minimize the variance for this return level. Let $\mathbf{r} = (\mu_1, \ldots, \mu_n)$ be the vector of expected returns, we solve:

\begin{equation*} \begin{aligned} & \underset{\mathbf{w}}{\text{minimize}} & & \sqrt{\mathbf{w} \cdot \Sigma \mathbf{w}} \\ & \text{subject to} & & \mathbf{r} \cdot \mathbf{w} = \mu_0 \\ &&& \sum_{i=1}^n w_i = 1 \\ \end{aligned} \end{equation*}

In Python, we have scipy.optimize.minimize which handles this problem nicely.

def opt_ret(retvct, covmat, tgt):
"""
Find the optimal weights for tgt expected return.
"""
cons = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1},
{'type': 'ineq', 'fun': lambda x: x}, # no short selling
{'type': 'eq', 'fun': lambda x: get_stats(x, retvct, covmat) - tgt})

func = lambda x: get_stats(x, retvct, covmat)

x0 = [1./len(retvct) for x in retvct]
return minimize(func, x0, constraints=cons, method='SLSQP').x


Finally, with some additional code, we can generate the efficient frontier for our stocks:

The color marks the return/risk ratio, or the return per unit of risk. This is known as the Sharpe Ratio; however, in our case we didn't account for an interest free rate.

As a final remark, there are several practical details that are worth noting:

• The standard deviation is used to express the risk, as this is in line with the unit dimension.

• It is conventional to use annualized returns. If the returns are computed using daily percentage changes, then the expectation ${\mathrm{E}}[P]$ is annualized by multiplying with $N = 252$ (amount of trading days in a year).

• Similarly, the standard deviation is annualized by multiplying with $\sqrt{N}$.

• In our code, the set of attainable returns $S$ ranges from the portfolio with the least variance to the individual asset that has the highest overall return.

• The portfolio with least variance lies the most to the left; it is found by solving the above optimization problem without the first constraint ($\mathbf{r} \cdot \mathbf{w} = \mu_0$).

• Often an additional constraint is added: each $w_i \geq 0$, i.e., short selling is prohibited. This can make the output weights more sensible, as there won’t be extreme cases where the optimal portfolio is supposedly to have weights $\mathbf{w} = (34.31, -17.84, -15.47)$ for stocks $A,B$ and $C$.

• Log returns can also be used instead of simple returns:

\begin{equation*} r_t = \log(x_t) - \log(x_{t-1}). \end{equation*}

Its rationale is well explained in this Quantivity article.

• As mentioned earlier, the true expectation and covariances are unknown; we use historical data and simple statistical estimators to estimate this. However, this approach has its shortcomings, which is further discussed in this paper.

Code for the graphs and computation can be found on Github!