Portfolio Analysis in R
Econ 424
Eric Zivot
Fall 2008
Updated: November 12, 2008

© Eric Zivot 2006

R Functions for Portfolio
Analysis• Eric Zivot’s R functions (on class webpage
i portfolio.r
intf li• R package tseries– portfolio.optim• R package fportfolio– Extensive collection of functions• R package quadprog– QP.solve for quadratic programming.
© Eric Zivot 2006

1

My R functions
Function

Description

getPortfolio

Create portfolio object

efficient po tfolio
efficient.portfolio

Comp te minim
Computeminimumm varianceariance
portfolio subject to target
returnglobalMin.portfolio

Compute global minimum
variance portfolio

tangency.portfolio

Compute tangency portfolio

efficient.frontier

Compute efficient frontier ofrisky asset only portfolios

Note: these functions are based on matrix algebra solutions to portfolio
calculations that allow short sales© Eric Zivot 2006

3 Firm Example Data
> erMSFT
NORD
SBUX
0.0427 0.0015 0.0285> covmat
MSFT
NORD
SBUX
MSFT 0.0100 0.0018 0.0011
NORD 0.0018 0.0109 0.0026
SBUX 0.0011
0 0011 00.00260026 00.0199
0199> rk.free[1] 0.005© Eric Zivot 2006

2

Create Arbitrary Portfolio# compute equally weighted portfolio> ew = rep(1,3)/3> equalWeight.portfolio
equalWeight portfolio =+ getPortfolio(er=er,cov.mat=covmat,weights=ew)> 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© Eric Zivot 2006

Plot Portfolio Weights

0.20
0.15
0.10
0.05
0.00

Weight0.25

0.30

Portfolio Weights

MSFTNORD

SBUX

Assets> plot(equalWeight.portfolio)© Eric Zivot 2006

3

Compute Global Minimum Variance Portfolio> gmin.port <- globalMin.portfolio(er, covmat)> attributes(gmin.port)$names[1] "call"
"er"
"sd"
"weights"$class[1] "portfolio"> print(gmin.port)Call:globalMin.portfolio(erl b lMi
tf li (= er, cov.matt = covmat)t)
Portfolio expected return:Portfolio standard deviation:
Portfolio weights:
MSFT
NORD
SBUX
0.4411 0.3656 0.1933

0.02489184
0.07267607© Eric Zivot 2006

Global Minimum Variance Portfolio

0.2
0.1
0.0

Weight0.3

0.4

Portfolio Weights

MSFTNORD

SBUX

Assets> plot(gmin.port)© Eric Zivot 2006

4

Compute Efficient Portfolio# compute efficient portfolio subject to target# return equal to E[Rmsft] = 0.0427> target.return <- er[1]> e.port.msft <- efficient.portfolio(er, covmat,+target.return)> print(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

00.04270427
0.091656© Eric Zivot 2006

Efficient Portfolio Weights

0.4
0.2
0.0

Weight0.6

0.8

Portfolio Weights

MSFT

NORD

SBUXAssets© Eric Zivot 2006

5

Compute Tangency Portfolio> tan.port <- tangency.portfolio(er, covmat,+rk free)rk.free)> print(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© Eric Zivot 2006

Tangency Portfolio Weights

0.4
0.2
0.0
0-0.2

Weight

0.6

0.8

1.0

Portfolio Weights

MSFT

NORD

SBUX

Assets© Eric Zivot 2006

6

Compute Efficient Frontier> ef <- efficient.frontier(er, covmat, alpha.min=-2,+alpha.max=1.5)> efCall:efficient.frontier(er = er, cov.mat = covmat, 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© Eric Zivot 2006

Plot Efficient Frontier

0.04

MSFT

0.02

SBUX

0.00

Portfolio ER

0.06

0.08

Efficient Frontier

NORD
0.00

0.05

0.10

0.15

Portfolio SD> plot(ef, plot.assets=T)© Eric Zivot 2006

7