You are on page 1of 8

Illustrating the efrontier Stata command

Alberto Dorantes, Ph.D.

October 11, 2018

Abstract
The efrontier Stata command estimates the efficient frontier given
a set of financial assets. The command receives a list of asset returns
and estimates N portfolios that lie in the efficient frontier. It is possible
to include restrictions to form portfolios such as minimum or maximum
weights. The purpose of this tutorial is to illustrate how to use the
efrontier command

1. General description of the command


According to portfolio theory, it is possible to find a set of portfolios that minimize
expected risk given a range of required return. Just by changing the allocation
weights for the stocks, it is possible to find a minimum variance portfolio for a
specific level of expected return.
The command mvport estimates the minimum variance portfolio given a specific
expected portfolio return. The command efrontier uses the mvport command
in a loop to identify a set of portfolios of minimum variance given a range of
expected portfolio returns.

1.1. The minimum variance portfolio estimation (mvport)


When there is no restrictions related to minimum or maximum weights, it is
possible to find the minimum variance portfolio for a given expected portfolio
return using an analytical formula.
When there are restrictions for minimum weights, there is no analytical solution.
Numerical algorithms can be used to find the minimum variance portfolio. The
most common algorithm to optimize portfolios is the quadratic programming
algorithm. In this case, I designed a different algorithm that starts with an
anlytical solution adn then iterates until it finds the portfolio with the minimum
variance given a specific expected portfolio return.

1
1.2. Brief description of the efrontier command
The efrontier command uses the mvport command in a loop to estimate a set
of minimum variance portfolios given a range of portfolio returns.
The efrontier command estimates two frontiers: one allowing for short sales,
and another without allowing for short sales.

2. Basic use of the efrontier command


Imagine you want to estimate an efficient frontier given a set of N asset historical
returns. Let’s start with a portfolio of US stocks. I will use the command
getsymbols to download data directly from Yahoo Finance.
To install this command, write the following in the command line or in a do-file:

ssc install getsymbols


With the getsymbols command you will be able to download online (and
upto-date) stock prices/returns from Yahoo Finance, Alpha Vantage and from
Quandl.com.
Now I download monthly data of these stocks from Jan 2015 to Dec 2017:
. getsymbols AMZN ADBE MSFT TSLA GE BBT BWA, yahoo ///
> fy(2015) fm(1) ly(2017) lm(12) freq(m) price(adjclose) clear
The data is being pulled from Yahoo.com ; the variables of this dataset are:
adjclose volume
Period returns were calculated using the variable: adjclose and only this varia
> ble was kept; if you want to keep all variables use the option keepall
Symbol AMZN was downloaded
Symbol ADBE was downloaded
Symbol MSFT was downloaded
Symbol TSLA was downloaded
Symbol GE was downloaded
Symbol BBT was downloaded
Symbol BWA was downloaded
36 periods of 7 symbol(s) from Yahoo were downloaded.
Number of observations with valid prices for all symbols: 36 out of 36

I specified the option price, so returns are calculated using prices.


Now I have a dataset of 36 months with adjusted stock prices, trading volume
and returns (simple and continuously compounded). The cc return variables
start with r_, while the simple return variables start with R_.
I install the mvport package that contains the efrontier command:
ssc install mvport
Now I use the cc returns to estimate the efficient frontier:
. efrontier r_*
Number of observations used to calculate expected returns and var-cov matrix :

2
> 36
The weight vector of the Global Minimum Variance Portfolio (NOT Allow Short Sal
> es) is:
Weights
r_AMZN 0
r_ADBE .38931191
r_MSFT 0
r_TSLA .02671568
r_GE .27712718
r_BBT .30684523
r_BWA 0
The return of the Global Minimum Variance Portfolio is: .01274005
The standard deviation (risk) of the Global Minimum Variance Portfolio is: .034
> 73642

Estimating 100 Portfolios without allowing for short sales...


Number of observations used to calculate expected returns and var-cov matrix :
> 36
The weight vector of the Global Minimum Variance Portfolio (Allowing Short Sale
> s) is:
Weights
r_AMZN -.09644054
r_ADBE .45921428
r_MSFT .03963435
r_TSLA .0696722
r_GE .25943479
r_BBT .43509136
r_BWA -.16660644
The return of the Global Minimum Variance Portfolio is: .01448937
The standard deviation (risk) of the Global Minimum Variance Portfolio is: .032
> 0399

Estimating 100 Portfolios allowing for short sales...

Figure 1: Efficient frontier, stocks: AMZN ADBE MSFT TSLA GE BBT BWA

3
To calculate the efficient frontier, the expected returns of the assets are calculated
using geometric average returns using all history of the asset returns. The
variance-covariance matrix is also estimated using continuously compounded
returns.
If you do not specify the number of portfolios to be created the default is 100.
All portfolios in the frontier are part of the estimated results of the command.
To see these results:
. return list
scalars:
r(N) = 7
matrices:
r(exprets) : 7 x 1
r(cov) : 7 x 7
r(sdefws) : 101 x 1
r(sdefwos) : 101 x 1
r(refws) : 101 x 1
r(refwos) : 101 x 1
r(wefws) : 101 x 7
r(wefwos) : 101 x 7

You can see the details of these scalars and matrices using the help of the
command.
To better understand this command, I will use the simport command that
also comes with the mvport package. The simport simulates portfolios using
random weights for the assets. If I generate many random portfolios, it is possible
to appreciate the complete frontier, the efficient and the non-efficient frontier:
. simport r_*, nport(10000)
Random portfolios were generated with no negative weights.
Descriptive statistics of the set of portfolio returns are below:
Variable Obs Mean Std. Dev. Min Max

R 10,002 .0147501 .0048022 -.0012048 .0309658


Random portfolio with the minimum variance had a return of : .01411465 and a st
> andard deviation of .03559232
Weights of this random portfolio:

w1 w2 w3 w4 w5 w6 w7

.0449523 .2739291 .0615581 .0249912 .2062751 .3872133 .0010809

. graph export simport1.png, replace


(file simport1.png written in PNG format)

This commands displays the expected risk, return and weights of the random
portfolio with the least variance.

4
Figure 2: Feasible random portfolios, stocks: AMZN ADBE MSFT TSLA GE
BBT BWA

3. efrontier and its options


The efrontier command has different options. There are options to restrict the
weights of each asset, and also options related to the way the expected asset
returns and the variance-covariance matrix is estimated. I wil illustrate some of
these options.
I can restrict the weights to equal or less than 30% and at least 5% for all assets:
. efrontier r_*, minw(0.05) maxw(0.30)
Number of observations used to calculate expected returns and var-cov matrix :
> 36
The weight vector of the Global Minimum Variance Portfolio (NOT Allow Short Sal
> es) is:
Weights
r_AMZN .05442065
r_ADBE .3
r_MSFT .05530088
r_TSLA .05238339
r_GE .23434721
r_BBT .25354787
r_BWA .05
The return of the Global Minimum Variance Portfolio is: .01347438
The standard deviation (risk) of the Global Minimum Variance Portfolio is: .037
> 02956

Estimating 100 Portfolios without allowing for short sales...


Number of observations used to calculate expected returns and var-cov matrix :
> 36
The weight vector of the Global Minimum Variance Portfolio (Allowing Short Sale
> s) is:
Weights

5
r_AMZN .0108092
r_ADBE .3
r_MSFT .13035701
r_TSLA .08891515
r_GE .28701332
r_BBT .3
r_BWA -.11709467
The return of the Global Minimum Variance Portfolio is: .01453091
The standard deviation (risk) of the Global Minimum Variance Portfolio is: .033
> 82529

Estimating 100 Portfolios allowing for short sales...

Figure 3: For red portfolios, weights are restricted between 5 and 30%

It is also possible to estimate the portfolios using different expected returns and
expected variance-covariance matrix. You can create the vector for expected
returns using any methodology, and the same for the variance-covariance matrix.
Then, you can just input both the vector of expected returns and the variance-
covariance matrix as options.
For example, I can estimate the expected returns assigning more relevance to
the most recent periods. I can use the Exponential Weighted Moving Average
(EWMA) method to assign exponential weights to the periods. Here the example:
. meanrets r_* , lew(0.95)
Expected simple returns (Geometric means):
Exp ret

r_AMZN .0323127
r_ADBE .0291448
r_MSFT .0254862
r_TSLA .0124862

6
r_GE -.0193464
r_BBT .0140553
r_BWA .007895
Observations to calculate expected simple returns: 36
The weights used for the computations is stored in r(W)

Note: the meanrets is a command that comes with the mvport package. I used
a lamda=0.95, so 5% weight will be assigned to the last period, and 95% to the
previous periods using the EWMA method.
I save the vector result in a matrix:
. matrix mrets=r(meanrets)

Now I can use the matrix mrets as option in the efrontier command. Before
this, I will also estimate the variance-covariance matrix using the EWMA method
with lamda=0.95:
. varrets r_* , lew(0.95)
The Variance-Covariance matrix is:
r_AMZN r_ADBE r_MSFT r_TSLA r_GE r_BBT
> r_BWA

r_AMZN .0042676
>
r_ADBE .0024469 .002962
>
r_MSFT .0019524 .0016694 .0028229
>
r_TSLA .0010279 .0012785 .0004686 .0085226
>
r_GE -.0003749 -.0008689 .0002752 .0008513 .0034752
>
r_BBT .0003263 -.0000749 .0002308 -.0002303 .0005797 .002575
>
r_BWA .0013379 .0013393 .0012672 .0022702 .0008258 .0022024
> .0083678
Observations to calculate the var-cov matrix: 36
The weights used for the computations is stored in r(W)

I save the variance-covariance matrix:


. matrix cov=r(cov)

Now I am ready to use these inputs in the efrontier command:


. efrontier r_*, covm(cov) mrets(mrets)
Number of observations used to calculate expected returns and var-cov matrix :
> 36
The weight vector of the Global Minimum Variance Portfolio (NOT Allow Short Sal
> es) is:
Weights
r_AMZN 0
r_ADBE .34756967
r_MSFT .05259762
r_TSLA .02854258
r_GE .28270871
r_BBT .28858142

7
r_BWA 0
The return of the Global Minimum Variance Portfolio is: .01041345
The standard deviation (risk) of the Global Minimum Variance Portfolio is: .029
> 77441

Estimating 100 Portfolios without allowing for short sales...


Number of observations used to calculate expected returns and var-cov matrix :
> 36
The weight vector of the Global Minimum Variance Portfolio (Allowing Short Sale
> s) is:
Weights
r_AMZN -.04890414
r_ADBE .39047758
r_MSFT .0761413
r_TSLA .04948208
r_GE .27521755
r_BBT .36115784
r_BWA -.10357221
The return of the Global Minimum Variance Portfolio is: .01129257
The standard deviation (risk) of the Global Minimum Variance Portfolio is: .028
> 67722

Estimating 100 Portfolios allowing for short sales...

Figure 4: Efficient frontier using EWMA to estimate expected returns and


var-covar matrix

You might also like