Coursera Computational Finance and Financial Econometrics Week 8 Lab 8

# lab8.r script file lab 8
# 
# author: Eric Zivot created: November 9, 2009 revised: July 31, 2012
# fixed problem with yearmon date object

options(digits = 4, width = 70)

# load packages
library("PerformanceAnalytics")
## Loading required package: zoo
## Attaching package: 'zoo'
## The following object(s) are masked from 'package:base':
## 
## as.Date, as.Date.numeric
## Loading required package: xts
## Attaching package: 'PerformanceAnalytics'
## The following object(s) are masked from 'package:graphics':
## 
## legend
library("zoo")

#
# |------------------------------------------------------------------------------------------|
# | M A I N P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# 
# introduction to portfolio theory

# source portfolio functions
source(file = "http://spark-public.s3.amazonaws.com/compfinance/R%20code/portfolio.r")

# load data from class website
lab8returns.df = read.csv(file = "http://spark-public.s3.amazonaws.com/compfinance/R%20code/lab8returns.csv", 
    stringsAsFactors = FALSE)
# 7/31/12: fix to problem with the yearmon class
dates = seq(as.Date("1992-07-01"), as.Date("2000-10-01"), by = "months")
lab8returns.df$Date = dates
# create zoo object
lab8returns.z = zoo(lab8returns.df[, -1], lab8returns.df$Date)
plot(lab8returns.z, lwd = 2, col = "blue")

plot of chunk unnamed-chunk-1


# compute estimates of CER model and annualize
muhat.annual = apply(lab8returns.z, 2, mean) * 12
sigma2.annual = apply(lab8returns.z, 2, var) * 12
sigma.annual = sqrt(sigma2.annual)
covmat.annual = cov(lab8returns.z) * 12
covhat.annual = cov(lab8returns.z)[1, 2] * 12
rhohat.annual = cor(lab8returns.z)[1, 2]

mu.b = muhat.annual["rboeing"]
mu.m = muhat.annual["rmsft"]
sig2.b = sigma2.annual["rboeing"]
sig2.m = sigma2.annual["rmsft"]
sig.b = sigma.annual["rboeing"]
sig.m = sigma.annual["rmsft"]
sig.bm = covhat.annual
rho.bm = rhohat.annual


# 1. create portfolios and plot
x.b = seq(from = -1, to = 2, by = 0.1)
x.m = 1 - x.b
mu.p = x.b * mu.b + x.m * mu.m
sig2.p = x.b^2 * sig2.b + x.m^2 * sig2.m + 2 * x.b * x.m * sig.bm
sig.p = sqrt(sig2.p)

plot(sig.p, mu.p, type = "b", pch = 16, ylim = c(0, max(mu.p)), xlim = c(0, 
    max(sig.p)), xlab = expression(sigma[p]), ylab = expression(mu[p]), col = c(rep("green", 
    18), rep("red", 13)))
text(x = sig.b, y = mu.b, labels = "Boeing", pos = 4)
text(x = sig.m, y = mu.m, labels = "MSFT", pos = 4)

plot of chunk unnamed-chunk-1


# now compute portfolios with assets and T-bills as well as Sharpe slopes

r.f = 0.03
# T-bills + Boeing
x.b = seq(from = 0, to = 2, by = 0.1)
mu.p.b = r.f + x.b * (mu.b - r.f)
sig.p.b = x.b * sig.b


# T-bills + MSFT
x.m = seq(from = 0, to = 2, by = 0.1)
mu.p.m = r.f + x.m * (mu.m - r.f)
sig.p.m = x.m * sig.m


plot(sig.p, mu.p, type = "b", pch = 16, ylim = c(0, max(mu.p)), xlim = c(0, 
    max(sig.p)), xlab = expression(sigma[p]), ylab = expression(mu[p]), col = c(rep("green", 
    18), rep("red", 13)))
text(x = sig.b, y = mu.b, labels = "Boeing", pos = 4)
text(x = sig.m, y = mu.m, labels = "MSFT", pos = 4)
points(sig.p.b, mu.p.b, type = "b", col = "blue")
points(sig.p.m, mu.p.m, type = "b", col = "orange")

plot of chunk unnamed-chunk-1



# 2. compute global minimum variance portfolio

gmin.port = globalMin.portfolio(muhat.annual, covmat.annual)
gmin.port
## Call:
## globalMin.portfolio(er = muhat.annual, cov.mat = covmat.annual)
## 
## Portfolio expected return:     0.2106 
## Portfolio standard deviation:  0.2139 
## Portfolio weights:
## rboeing   rmsft 
##   0.662   0.338
summary(gmin.port, risk.free = 0.03)
## Call:
## globalMin.portfolio(er = muhat.annual, cov.mat = covmat.annual)
## 
## Portfolio expected return:     0.2106 
## Portfolio standard deviation:  0.2139 
## Portfolio Sharpe Ratio:        0.8443 
## Portfolio weights:
## rboeing   rmsft 
##   0.662   0.338
plot(gmin.port)

plot of chunk unnamed-chunk-1


pie(gmin.port$weights)

plot of chunk unnamed-chunk-1



plot(sig.p, mu.p, type = "b", pch = 16, ylim = c(0, max(mu.p)), xlim = c(0, 
    max(sig.p)), xlab = expression(sigma[p]), ylab = expression(mu[p]), col = c(rep("green", 
    18), rep("red", 13)))
text(x = sig.b, y = mu.b, labels = "Boeing", pos = 4)
text(x = sig.m, y = mu.m, labels = "MSFT", pos = 4)
text(x = gmin.port$sd, y = gmin.port$er, labels = "Global min", pos = 2)

plot of chunk unnamed-chunk-1


# 3. compute tangency portfolio

tan.port = tangency.portfolio(muhat.annual, covmat.annual, risk.free = 0.03)
tan.port
## Call:
## tangency.portfolio(er = muhat.annual, cov.mat = covmat.annual, 
##     risk.free = 0.03)
## 
## Portfolio expected return:     0.2507 
## Portfolio standard deviation:  0.2365 
## Portfolio weights:
## rboeing   rmsft 
##  0.4409  0.5591
summary(tan.port, risk.free = 0.03)
## Call:
## tangency.portfolio(er = muhat.annual, cov.mat = covmat.annual, 
##     risk.free = 0.03)
## 
## Portfolio expected return:     0.2507 
## Portfolio standard deviation:  0.2365 
## Portfolio Sharpe Ratio:        0.9334 
## Portfolio weights:
## rboeing   rmsft 
##  0.4409  0.5591
plot(tan.port)

plot of chunk unnamed-chunk-1

pie(tan.port$weights)

plot of chunk unnamed-chunk-1


# T-bills + tangency
x.t = seq(from = 0, to = 2, by = 0.1)
mu.p.t = r.f + x.t * (tan.port$er - r.f)
sig.p.t = x.t * tan.port$sd



plot(sig.p, mu.p, type = "b", pch = 16, ylim = c(0, max(mu.p)), xlim = c(0, 
    max(sig.p)), xlab = expression(sigma[p]), ylab = expression(mu[p]), col = c(rep("green", 
    18), rep("red", 13)))
text(x = sig.b, y = mu.b, labels = "Boeing", pos = 4)
text(x = sig.m, y = mu.m, labels = "MSFT", pos = 4)
text(x = tan.port$sd, y = tan.port$er, labels = "Tangency", pos = 2)
points(sig.p.t, mu.p.t, type = "b", col = "blue", pch = 16)

plot of chunk unnamed-chunk-1


# 4 and 5

x.t = 0.1
x.f = 1 - x.t

mu.e = r.f + x.t * (tan.port$er - r.f)
sd.e = x.t * tan.port$sd


plot(sig.p, mu.p, type = "b", pch = 16, ylim = c(0, max(mu.p)), xlim = c(0, 
    max(sig.p)), xlab = expression(sigma[p]), ylab = expression(mu[p]), col = c(rep("green", 
    18), rep("red", 13)))
text(x = sig.b, y = mu.b, labels = "Boeing", pos = 4)
text(x = sig.m, y = mu.m, labels = "MSFT", pos = 4)
text(x = tan.port$sd, y = tan.port$er, labels = "Tangency", pos = 2)
points(sig.p.t, mu.p.t, type = "b", col = "blue", pch = 16)
points(sd.e, mu.e, type = "p", col = "orange", pch = 16, cex = 2)
text(x = sd.e, y = mu.e, labels = "Efficient Portfolio with 10% Tangency", pos = 4, 
    cex = 0.75)

plot of chunk unnamed-chunk-1


# efficient port with same risk as msft
x.t = sig.m/tan.port$sd
mu.e = r.f + x.t * (tan.port$er - r.f)
sd.e = x.t * tan.port$sd


plot(sig.p, mu.p, type = "b", pch = 16, ylim = c(0, max(mu.p)), xlim = c(0, 
    max(sig.p)), xlab = expression(sigma[p]), ylab = expression(mu[p]), col = c(rep("green", 
    18), rep("red", 13)))
text(x = sig.b, y = mu.b, labels = "Boeing", pos = 4)
text(x = sig.m, y = mu.m, labels = "MSFT", pos = 4)
text(x = tan.port$sd, y = tan.port$er, labels = "Tangency", pos = 2)
points(sig.p.t, mu.p.t, type = "b", col = "blue", pch = 16)
points(sd.e, mu.e, type = "p", col = "orange", pch = 16, cex = 2)
text(x = sd.e, y = mu.e, labels = "Efficient Portfolio with Same SD as MSFT", 
    pos = 2, cex = 0.75)

plot of chunk unnamed-chunk-1

#
# |------------------------------------------------------------------------------------------|
# | E N D O F S C R I P T |
# |------------------------------------------------------------------------------------------|