We all have to deal with 401K allocation some point in our life right? Often times, we are very indecisive in choosing the best subset from a basket of securities that will give us the highest return in the long run, yet minimizing the risk. The efficient frontier by Markowitz can help us decide by showing us the minimum risk for each level of expected return and vice versa.

Let’s say you have three funds to choose from. Security $A$ tracks the S&P500, Security $B$ tracks the NASDAQ and prices of the technology sector, and Security $C$ is an indicator of the energy sector. Given we have historical records (daily stock price, daily % return) of these securities, we could find the variance and the covariance for these stocks. The variance of one stock would be the fluctuating price of that particular stock, where a higher variance would indicate the stock has a greater volatility, thus a higher risk because the %return is not guaranteed or fixed. Covariances, on the other hand, indicates how much two stocks follow similar movements. For instance, two stocks both from U.S. market would go down if the broader U.S. market is on a downward trend.

In our example, let’s say the four securities takes on four random variables. Then the expected % return for the securities are

\[\bar{Z} = \begin{pmatrix} E(A) \\ E(B) \\ E(C) \\ E(D) \end{pmatrix} = \begin{pmatrix} 14 \\ 12 \\ 15 \\ 7 \end{pmatrix}\]where 14 stands for 14% average expected return for stock $A$, 12% for stock $B$, and so on.

Then, we can also define the sets of weights for the portfolio

\[W = \begin{pmatrix} w_A \\ w_B \\ w_C \\ w_D \end{pmatrix}\]such that $w_A+w_B+w_C+w_D=$ 1

The total average expected return of a portolio $P$ would be the weighted sum

\[E(P) = W^{T}\bar{Z}\]The variance of the portfolio $P$ will be

\[Var(P) = Var(W^{T}\bar{Z}) = W^{T}Var(\bar{Z})W = W^{T}\Sigma W\]where $\Sigma$ is the variance-covariance matrix of vector $\bar{Z}$.

The objective here is to minimize the porfolio variance given a porfolio mean $\bar{Z}$, and subject to the weights add up to 1. Our optimization problem could be defined as follows:

\[\min_{W} W^{T}\Sigma W\] \[s.t. 1^{T}W = 1, E(P) = W^{T}\bar{Z}\]From here, we would use the Lagrangian method to solve the min (I can derive this in another post). Also, from the lagrangian equations we can derive the relationship between $\sigma_p$ and $\mu_p$, which turns out to be a hyperbola of the form

\[\sigma_p^2 = \frac{B\mu_p^2 - 2C\mu_p + A}{D}\]where

\[A = \mu^T\Sigma^{-1}\mu; B=1^T\Sigma^{-1}1, C=1^T\Sigma^{-1}\mu, D= AB - C^2\]Let’s run a simple R program to generate the efficient frontier for our particular choices of securities.

Let’s calculate the coefficient, and find the min-variance