HW3
2025-03-03
HW3
data = [Link]("[Link]", header = T)
price = cbind(data$CAT_AC, data$IBM_AC, data$MSFT_AC)
n = dim(price)[1]
return = price[2:n,]/price[1:(n-1),] - 1
mu = colMeans(return)
sigma = cov(return)
correl = cor(return)
m = 500 # no. of points to evaluate
muP = seq(.00001,.0015,length=m) # target portfolio return
sdP = rep(0, length(muP)) # sd of portfolio return
weight = matrix(0,nrow=m,ncol=3) # storage for portfolio weights
for (i in 1:length(muP)) { # find the optimal portfolios
result = [Link](Dmat=2*sigma,dvec=rep(0,3),
Amat = cbind(rep(1,3),mu),bvec=c(1,muP[i]),meq=2)
sdP[i] = sqrt(result$value)
weight[i,] = result$solution
}
GMP = [Link](sdP) # global minimum point
# efficient frontier
plot(sdP[GMP:m],muP[GMP:m],type="l",xlim=c(0,.04),ylim=c(0,.0015),
lwd=3,col="red", xlab = "SD of portfolio return",
ylab = "mean of portfolio return")
points(sdP[1:(GMP-1)],muP[1:(GMP-1)], type="l", lty = 2,lwd=1, col = "red")
points(sqrt(diag(sigma)), mu, pch = 4) # label
text(sqrt(diag(sigma))+0.002, mu, c("CAT","IBM", "MSFT")) # text label
ind2 = (sdP == min(sdP)) # find minimum variance portfolio
points(sdP[ind2], muP[ind2], cex = 2, pch = "+") # min var
1
0.0015
MSFT
mean of portfolio return
0.0010
+ CAT
0.0005
IBM
0.0000
0.00 0.01 0.02 0.03 0.04
SD of portfolio return
rf <- 0.05 / 253
plot(sdP[GMP:m],muP[GMP:m],type="l",xlim=c(0,.04),ylim=c(0,.0015),
lwd=3,col="red", xlab = "SD of portfolio return",
ylab = "mean of portfolio return")
points(sdP[1:(GMP-1)],muP[1:(GMP-1)], type="l", lty = 2,lwd=1, col = "red")
points(sqrt(diag(sigma)), mu, pch = 4) # label
text(sqrt(diag(sigma))+0.002, mu, c("CAT","IBM", "MSFT")) # text label
ind2 = (sdP == min(sdP)) # find minimum variance portfolio
points(sdP[ind2], muP[ind2], cex = 2, pch = "+") # min var
points(0, rf, cex = 4, pch = "*") # show risk-free asset
sharpe = (muP - rf) / sdP # compute Sharpe’s ratios
ind = (sharpe == max(sharpe)) # Find maximum Sharpe’s ratio
weight[ind, ] # print the weights of the tangency portfolio
## [1] 0.4427754 -0.2424755 0.7997001
lines(c(0, 2), rf + c(0, 2) * (muP[ind] - rf) / sdP[ind],
lwd = 4, lty = 1, col = "blue") # show line of optimal
points(sdP[ind], muP[ind], cex = 4, pch = "*") # tangency
2
0.0015
MSFT
*
mean of portfolio return
0.0010
+ CAT
0.0005
IBM
*
0.0000
0.00 0.01 0.02 0.03 0.04
SD of portfolio return
muP_noSS = seq(min(mu),max(mu),length=m) # target portfolio return
sdP_noSS = rep(0, length(muP_noSS))
for (i in 1:length(muP_noSS)) { # find the optimal portfolios
result = [Link](Dmat=2*sigma,dvec=rep(0,3),
Amat=cbind(rep(1,3),mu,diag(1,3)),
bvec=c(1,muP_noSS[i],rep(0,3)),meq=2)
sdP_noSS[i] = sqrt(result$value)
weight[i,] = result$solution
}
GMP_noSS = [Link](sdP_noSS) # global minimum point with no short-selling
plot(sdP[GMP:m],muP[GMP:m],type="l",xlim=c(0,.04),ylim=c(0,.0015),
lwd=3,col="red", xlab = "SD of portfolio return",
ylab = "mean of portfolio return")
points(sdP[1:(GMP-1)],muP[1:(GMP-1)], type="l", lty = 2,lwd=1, col = "red")
text(sqrt(diag(sigma))+0.002, mu, c("CAT","IBM", "MSFT")) # text label
points(sdP_noSS[GMP_noSS:m],muP_noSS[GMP_noSS:m],type="l",
lwd=3,col="blue", xlab = "SD of portfolio return",
ylab = "mean of portfolio return")
points(sdP_noSS[1:(GMP_noSS-1)],muP_noSS[1:(GMP_noSS-1)],
type="l", lty = 2,lwd=1, col = "blue")
3
0.0015
MSFT
mean of portfolio return
0.0010
CAT
0.0005
IBM
0.0000
0.00 0.01 0.02 0.03 0.04
SD of portfolio return