Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 580

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 583

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 586

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 589

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 592
Calculating the Efficient Frontier: Part 1 » The Calculating Investor
A Matrix Based Example of  Mean-Variance Optimization using Octave

The concept of  an “efficient frontier” was developed by Harry Markowitz in the 1950s.  The efficient frontier shows us the minimum risk (i.e. standard deviation) that can be achieved at each level of expected return for a given set of risky securities.

Of course, to calculate the efficient frontier, we need to have an estimate of the expected returns and the covariance matrix for the set of risky securities which will used to build the optimal portfolio.  These parameters are difficult (impossible) to forecast, and the optimal portfolio calculation is extremely sensitive to these parameters.  For this reason, an efficient frontier based portfolio is difficult to successfully implement in practice.  However, a familiarity with the concept is still very useful and will help to develop intuition about diversification and the relationship between risk and return.

Calculating the Efficient Frontier

In this post, I’ll demonstrate how to calculate and plot the efficient frontier using the expected returns and covariance matrix for a set of securities.

In a future post, I’ll demonstrate how to calculate the security weights for various points on this efficient frontier using the two-fund separation theorem.


In order to calculate the efficient frontier using n assets, we need two inputs.  First, we need the expected returns of each asset.  The vector of expected returns will be designated \bar{\mathbf{z}}.  The second input is the variance-covariance matrix for the n assets.  This covariance matrix will be designated as \mathbf{S}.  We also need a unity vector (\mathbf{1}) with the same length as the vector \bar{\mathbf{z}}.

Once we have this information, we can run the following calculations using a matrix based mathematical program such as Octave or Matlab.




\Delta =AC-B^{2}>0

Using these values, the variance (\sigma^2) at each level of expected return (\mu) is given by this equation:


You can see from the equation, that the efficient frontier is a parabola in mean-variance space.

Using the standard deviation (\sigma) rather than the variance, we have:


Example using Octave Script

As an example, lets consider four securities, A,B,C and D, with expected returns of 14%, 12%, 15%, and 7%.  The expected return vector is:

\mathbf{\bar{z}}=\begin{bmatrix} 14\\ 12\\ 15\\ 7 \end{bmatrix}

The covariance matrix for our example is shown below.  In practice, the historical covariance matrix can be calculated by reading the historical returns into Octave or Matlab and using the cov(X) command. Note that the diagonal of the matrix is the variance of our four securities.  So, if we take the square root of the diagonal, we can calculate the standard deviation of each asset (13.6%, 14%, 20.27%, and 5%).

 \mathbf{S}=\begin{bmatrix} 185& 86.5& 80& 20\\ 86.5& 196& 76& 13.5\\ 80& 76& 411& -19\\ 20& 13.5& -19& 25 \end{bmatrix}

The example script for computing the efficient frontier from these inputs is shown at the end of this post.  It can be modified for any number of assets by updating the expected return vector and the covariance matrix.

The plot of the efficient frontier for our four assets is shown here:

Derivation and References

Deriving the approach I have shown is beyond the scope of this post.  However, for those who want to dive into the linear algebra, there are several excellent examples available online.

Derivation of Mean-Variance Frontier Equation using the Lagrangian (The Appendix B result is identical to what I show above, but the notation is a little different)

Old school derivation by a young professor who later went on to win a Nobel prize

Octave Code:

This script will also work in Matlab, but I’ve chosen to use Octave since it is opensource and available for free.

% Mean Variance Optimizer

% S is matrix of security covariances
S = [185 86.5 80 20; 86.5 196 76 13.5; 80 76 411 -19; 20 13.5 -19 25]

% Vector of security expected returns
zbar = [14; 12; 15; 7]

% Unity vector..must have same length as zbar
unity = ones(length(zbar),1)

% Vector of security standard deviations
stdevs = sqrt(diag(S))

% Calculate Efficient Frontier
A = unity'*S^-1*unity
B = unity'*S^-1*zbar
C = zbar'*S^-1*zbar
D = A*C-B^2

% Efficient Frontier
mu = (1:300)/10;

% Plot Efficient Frontier
minvar = ((A*mu.^2)-2*B*mu+C)/D;
minstd = sqrt(minvar);

title('Efficient Frontier with Individual Securities','fontsize',18)
ylabel('Expected Return (%)','fontsize',18)
xlabel('Standard Deviation (%)','fontsize',18)

9 Responses to “Calculating the Efficient Frontier: Part 1”

  1. in injection molding i have 2 defects (warpage&sink marks) they are controlled with variables(pressure&temperature) and i want to study the effect of the combination between these 2 variables on the 2 defects by using the efficient frontier .
    please help me and tell me how can i do it and the equation needed

    thank you

  2. […] I’m trying to convert Matlab code to R. I’m not familiar with Matlab matrix operations, and it appears the results from my R code do not match the results from Matlab, so any help would be greatly appreciated. The Matlab code I’m trying to convert is below (from this website): […]

  3. I really appreciate your articles.

    Question: Is it possible you have the definition of efficient frontier wrong. It looks to me like you are describing how to calculate the minimum variance frontier as opposed to the efficient frontier.

    see defintion: http://www.investopedia.com/terms/e/efficientfrontier.asp
    see discusion: http://www.analystforum.com/forums/cfa-forums/cfa-level-i-forum/91320257

  4. hi.can you tell me how i can calculate entropy of portfolio wieghts with matlab codes???

  5. I’m wondering whether one can calculate the weights of the portfolio corresponding to a point on the frontier?

  6. There is a useful, free site: portfoliovisualizer,com to consider–10.2017

  7. Hello this is incorrect. You need to calculate the global minimum point which is mu=A/C i.e. you defined mu incorrectly. Then you increase mu by some increments and calculate corresponding Variances for given mu.
    Then you plot.

    Also is there a way to plot the Capital Market Line i.e. portfolio of risk free assets and risky assets (tangency portfolio)?


Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>