Computing Eﬃcient Portfolios in R
Eric Zivot
November 11, 2008
Abstract
This note describes the computation of mean-variance eﬃcient portfolios
using R.

1Portfolio Analysis Functions

I have written a few R functions for computing Markowitz mean-variance eﬃcient
portfolios allowing for short sales. These functions are meant to be used for learning
the basics of portfolio theory. They are summarized in Table 1.

1.1

ExamplesThe following examples illustrate the use of the functions in Table 1. These examples
are also available in the R script file testport.r on the class R hints web page. To
begin, open the script file portfolio.r and select Edit/Run all to source the portfolio
functions into memory.
Next, consider the input data (μ, Σ and rf ) used for the three firm example usedin the class lectures (see 3firmExample.xls on the class syllabus web page).
> asset.names <- c("MSFT", "NORD", "SBUX")> er <- c(0.0427, 0.0015, 0.0285)> names(er) <- asset.names
Function
getPortfolio
globalMin.portfolio
efficient.portfolio
tangency.portfolio
efficient.frontier

Description
create portfolio object
compute global minimum variance portfolio
compute minimum variance portfolio subject to target return
compute tangency portfolio
compute eﬃcient frontier of risky assets

Table 1: R functions for computing mean-variance eﬃcient portfolios
1

> covmat <- matrix(c(0.0100, 0.0018, 0.0011,+0.0018, 0.0109, 0.0026,+0.0011, 0.0026, 0.0199),+ nrow=3, ncol=3)> rk.free <- 0.005> dimnames(covmat) <- list(asset.names, asset.names)
To specify a portfolio, you need an expected return vector and covariance matrix forthe assets under consideration as well as a vector of portfolio weights. To create anequally weighted portfolio use> ew = rep(1,3)/3> equalWeight.portfolio = getPortfolio(er=er,cov.mat=covmat,weights=ew)> class(equalWeight.portfolio)[1] "portfolio"
Portfolio objects have the following components> names(equalWeight.portfolio)[1] "call"
"er"
"sd"

"weights"

There are print(), summary() and plot() methods for portfolio objects. Theprint() method gives> equalWeight.portfolio
Call:
getPortfolio(er = er, cov.mat = covmat, weights = ew)
Portfolio expected return:Portfolio standard deviation:
Portfolio weights:
MSFT
NORD
SBUX
0.3333 0.3333 0.3333

0.02423333
0.07586538

The plot() method shows a bar chart of the portfolio weights> plot(equalWeight.portfolio)
The global minimum variance portfolio (allowing for short sales) m solves theoptimization problem
min m0 Σm s.t. m0 1 = 1.
m

To compute this portfolio use the function globalMin.portfolio()20.20
0.15
0.00

0.05

0.10

Weight0.25

0.30

Portfolio Weights

MSFT

NORD

SBUX

AssetsFigure 1:> gmin.port <- globalMin.portfolio(er, covmat)> attributes(gmin.port)$names[1] "call"
"er"
"sd"
"weights"$class[1] "portfolio"> gmin.port
Call:globalMin.portfolio(er = er, cov.mat = covmat)
Portfolio expected return:Portfolio standard deviation:
Portfolio weights:
MSFT
NORD
SBUX
0.4411 0.3656 0.1933

0.02489184
0.07267607

A mean-variance eﬃcient portfolio x that achieves the target expected return μ0solves the optimization problem
min x0 Σx s.t. x0 1 = 1 and x0 μ = μ0 .
x

To compute this portfolio for the target expected return μ0 = E[Rmsf t ] = 0.04275
use the efficient.portfolio() function
3

> target.return <- er[1]> e.port.msft <- efficient.portfolio(er, covmat, target.return)> e.port.msft
Call:efficient.portfolio(er = er, cov.mat = covmat, target.return = target.return)Portfolio expected return:Portfolio standard deviation:
Portfolio weights:
MSFT
NORD
SBUX
0.8275 -0.0907 0.2633

0.0427
0.091656

The tangency portfolio t is the portfolio of risky assets with the highest Sharpe’sslope and solves the optimization problem
t0 μ − rf
max 0s.t. t0 1 = 1,
1/2t(t Σt)where rf denotes the risk-free rate. To compute this portfolio with rf = 0.005 use
the tangency.portfolio() function> tan.port <- tangency.portfolio(er, covmat, rk.free)> tan.port
Call:tangency.portfolio(er = er, cov.mat = covmat, risk.free = rk.free)
Portfolio expected return:Portfolio standard deviation:
Portfolio weights:
MSFT
NORD
SBUX
1.0268 -0.3263 0.2994

0.05188967
0.1115816

The the set of eﬃcient portfolios of risky assets can be computed as a convex
combination of any two eﬃcient portfolios. It is convenient to use the global minimum
variance portfolio as one portfolio and an eﬃcient portfolio with target expected
return equal to the maximum expected return of the assets under consideration as
the other portfolio. Call these portfolios m and x, respectively. For any number α,another eﬃcient portfolio can be computed asz = αm + (1 − α)x
The function efficient.frontier() constructs the set of eﬃcient portfolios using
this method for a collection of α values. For example, to compute 20 eﬃcient portfolios
for values of α between −2 and 1.5 use
4

> ef <- efficient.frontier(er, covmat, alpha.min=-2,+alpha.max=1.5, nport=20)> attributes(ef)$names[1] "call"
"er"
"sd"
"weights"$class[1] "Markowitz"
> ef
Call:
efficient.frontier(er = er, cov.mat = covmat, nport = 20, alpha.min = -2,alpha.max = 1.5)
Frontier portfolios’ expected returns and standard deviations
port 1 port 2 port 3 port 4 port 5 port 6 port 7
ER 0.0783 0.0750 0.0718 0.0685 0.0652 0.0619 0.0586
SD 0.1826 0.1732 0.1640 0.1548 0.1458 0.1370 0.1284
port 8 port 9 port 10 port 11 port 12 port 13 port 14
ER 0.0554 0.0521 0.0488 0.0455 0.0422
0.039 0.0357
SD 0.1200 0.1120 0.1044 0.0973 0.0908
0.085 0.0802
port 15 port 16 port 17 port 18 port 19 port 20
ER 0.0324 0.0291 0.0258 0.0225 0.0193 0.0160
SD 0.0764 0.0739 0.0727 0.0730 0.0748 0.0779
Use the summary() method to show the weights of these portfolios. Use theplot() method to plot the eﬃcient frontier> plot(ef)The resulting plot is shown in Figure 2.
To create a plot of the eﬃcient frontier showing the original assets and the tangency portfolio use> plot(ef, plot.assets=T)> points(gmin.port$sd, gmin.port$er, col="blue")> points(tan.port$sd, tan.port$er, col="red")> sr.tan = (tan.port$er - rk.free)/tan.port$sd> abline(a=rk.free, b=sr.tan)
The resulting plot is shown in Figure 3.

5

0.04
0.00

0.02

Portfolio ER

0.06

0.08

Efficient Frontier

0.00

0.05

0.10

0.15

Portfolio SD

Figure 2: Plot method for Markowitz object.

MSFT

0.04

Portfolio ER

0.06

0.08

Efficient Frontier

0.000.02

SBUX

NORD
0.00

0.05

0.10

0.15

Portfolio SD

Figure 3: Eﬃcient frontier for three firm example.

6