Portfolio Analysis in R
Econ 424/CFRM 462
Eric Zivot
Summer 2014
Updated: August 12, 2014

© Eric Zivot 2006

R Functions for Portfolio Analysis• My R functions (on class webpage inportfolio.r andportfolio_noshorts.r)• R package PortfolioAnalytics (on RR-forge)
forge)– Extensive collection of functions• Rmetrics
R t i packagekfP tf li
fPortfolio– Extensive collection of functions• R package quadprog– solve.QP() for quadratic programming.
© Eric Zivot 2006

R functions in portfolio.r
Function

Description

getPortfolio
f liefficient.portfolio

CreateCt portfolio
tf li object
bj tCompute minimum
variance portfolio subject totarget return
Compute global minimum
variance portfolio
Compute tangency portfolioC
Computet efficientffi i t frontier
f ti offrisky asset only portfolios

globalMin.portfolio
tangency.portfolio
efficient.frontier
ffi i t fti

Note:
Nt thesethf
functionsti
are based
bd on matrix
t i algebral bsolutions to portfolio calculations that allow short sales© Eric Zivot 2006

3 Firm Example Data> er
MSFT
NORD
SBUX
0.0427 0.0015 0.0285> covmatco matMSFTNORDSBUXMSFT 0.0100 0.0018 0.0011
NORD 0.0018 0.0109 0.0026
SBUX 0.0011 0.0026 0.0199> r.free[1] 0.005© Eric Zivot 2006

Create Arbitrary Portfolio# compute equally weighted portfolio> ew = rep(1,3)/3> equalWeight.portfolio =+ getPortfolio(er=er,cov.mat=covmat,weights=ew)> equalWeight.portfolio
Call:
getPortfolio(er = er, cov.mat = covmat,weights = ew)
Portfolio
Ptf li expectedt d return:tPortfolio standard deviation:
Portfolio weights:
MSFTNORD
SBUX
0.3333 0.3333 0.3333© Eric Zivot 2006

00.02423
02423
0.07587

0.15
0.00

0.05

0.10

Weigght

0.20

0.25

0.30

Portfolio Weights

MSFT

NORD

SBUX

Assets> plot(equalWeight.portfolio, col=“blue”)© Eric Zivot 2006

Compute Global Minimum Variance Portfolio> gmin.port <- globalMin.portfolio(er, covmat)> attributes(gmin.port)$names[1] "call""er""sd""weights“$class[1]
1 "portfolio“> print(gmin.port)Call:globalMin.portfolio(er = er, cov.mat = covmat)
Portfolio expected return:Portfolio standard deviation:
Portfolio weights:
gMSFT
NORD
SBUX
0.4411 0.3656 0.1933© Eric Zivot 2006

0.024890.07268

0.2
2
0.1
0.0

Weight0.3

0.4

Portfolio Weights

MSFT

NORD

SBUX

Assets> plot(gmin.port, col=“blue”)© Eric Zivot 2006

Compute Efficient Portfolio# compute efficient portfolio subject to# target return equal to E[Rmsft] = 0.0427> target.returntarget return = er[er["MSFT"]MSFT ]> e.port.msft = efficient.portfolio(er, covmat,target.return)> e.port.msft
Call:efficient.portfolio(er = er, cov.mat = covmat,target.return = target.return)PortfolioP
tf li expectedt d return:tPortfolio standard deviation:
Portfolio weights:
MSFT
NORD
SBUX
0.8275 -0.0907 0.2633© Eric Zivot 2006

00.04270427
0.09166

0..4
0.2
0.0

Weighht

0.6

0.8

Portfolio Weights

MSFT

NORD
Assets© Eric Zivot 2006

SBUX

Compute Tangency Portfolio> tan.port <- tangency.portfolio(er, covmat,+r.free)> print(tan.port)Call:tangency.portfolio(er = er, cov.mat = covmat,risk.free = r.free)
Portfolio expected return:Portfolio standard deviation:
Portfolio Sharpe Ratio:
Portfolio weights:
MSFT
NORD
SBUX
1.0268 -0.3263
0.3263 0.2994© Eric Zivot 2006

00.05189
05189
0.11160.4202

0.4
0.2
00.0
-0.2

Weight

0.6

0.8

1.0

Portfolio Weights

MSFTNORD
Assets© Eric Zivot 2006

SBUX

Compute Efficient Frontier> ef = efficient.frontier(er, covmat,+alpha.min=-2,alpha.min 2, alpha.max
alpha.max=1.5,1.5,+
nport=20)> attributes(ef)$names[1] "call"
"er"
"sd"
"weights"$class[1] "Markowitz"© Eric Zivot 2006

Frontier portfoliosp> summary(ef)Frontier portfolios' expected returns and standard
deviations
port 1 port 2 port 3 port 4 port 5 port 6
ER 0.0783 0.0750 0.0718 0.0685 0.0652 0.0619
SD 0.1826 0.1732 0.1640 0.1548 0.1458 0.1370
port 7 port 8 port 9 port 10 port 11 port 12
ER 0.0586 0.0554 0.0521 0.0488 0.0455 0.0422
SD 0.1284 0.1200 0.1120 0.1044 0.0973 0.0908
port 13 port 114 port 1
15 port 16 port 117ER0.039 0.0357 0.0324 0.0291 0.0258
SD0.085 0.0802 0.0764 0.0739 0.0727
port 18 port 19 port 20
ER 0.0225 0.0193 0.0160
SD 0.0730 0.0748 0.0779© Eric Zivot 2006

Frontier portfolios
Portfolio weights:
MSFTNORD
SBUX
port 1 1.6001 -1.0035 0.4033
port 2 1.5290 -0.9194 0.3904
port 3 1.4578 -0.8354 0.3775
p
port 4 1.3866 -0.7513 0.3646
port 5 1.3155 -0.6672 0.3517…
…
port 18 0.3903 0.4257 0.1841
port 19 0.3191 0.5097 0.1712
port 20 0.2479 0.5938 0.1583© Eric Zivot 2006

Plot efficient frontier> pplot(ef, pplot.assets=T, col="blue", ppch=16)> points(gmin.port$sd, gmin.port$er,+
col="green", pch=16, cex=2)> points(tanpoints(tan.port$sd,port$sd tan.port$er,tan port$er col=col="red"
red ,+
pch=16, cex=2)> text(gmin.port$sd, gmin.port$er,+l b l "GLOBAL MIN"
labels="GLOBALMIN", pos=2)2)> text(tan.port$sd, tan.port$er,+labels="TANGENCY", pos=2)> sr.tan = (tan.port$er - r.free)/tan.port$sd> abline(a=r.free, b=sr.tan, col="green",
lwd=2)© Eric Zivot 2006

0.06

0.008

Efficient Frontier

0.04

MSFT

SBUX
0.02

GLOBAL MIN

0.00

Portfolio ERRTANGENCY

NORD

0.00

0.05

0.10
Portfolio SD© Eric Zivot 2006

0.15