############################################################### ### Example 4.8: Efficient frontier ############################################################### ### # Define parameters ### muvec<-matrix(c(1.05,1.15,1.1,1.1),4,1) cormat<-matrix(c(1.0,0.6,0.5,0.4,0.6,1.0,-0.1,0.0,0.5,-0.1,1.0,0.6,0.4,0.0,0.6,1.0),4,4,byrow=T) sdmat<-diag(c(0.3,0.25,0.2,0.15)) covmat<-sdmat%*%cormat%*%sdmat # [,1] [,2] [,3] [,4] #[1,] 0.090 0.0450 0.030 0.0180 #[2,] 0.045 0.0625 -0.005 0.0000 #[3,] 0.030 -0.0050 0.040 0.0180 #[4,] 0.018 0.0000 0.018 0.0225 # Function to compute optimal portfolios minvarweights<-function(mu,sigma,r0,c) { dimen<-length(mu) res<-matrix(0,dimen,3) res[,1]<-solve(sigma)%*%(c*mu-max(c*t(rep(1,dimen))%*%solve(sigma)%*%mu-1,0)/(t(rep(1,dimen))%*%solve(sigma)%*%rep(1,dimen))*rep(1,dimen)) res[,2]<-c*solve(sigma)%*%(mu-r0*rep(1,dimen)) res } ### # Solving the trade-off problem ### musigmapairs<-matrix(0,800,4) for(i in (0:799)) { wvec<-minvarweights(muvec,covmat,1.05,i/1600) # Computing the efficient frontier without risk free asset musigmapairs[i+1,1]<-t(wvec[,1])%*%muvec musigmapairs[i+1,2]<-sqrt(t(wvec[,1])%*%covmat%*%wvec[,1]) # Computing the efficient frontier with risk free asset musigmapairs[i+1,3]<-t(wvec[,2])%*%muvec+(1-sum(wvec[,2]))*1.05 musigmapairs[i+1,4]<-sqrt(t(wvec[,2])%*%covmat%*%wvec[,2]) } # Plot efficient frontier withour risk free asset plot(musigmapairs[,2],musigmapairs[,1],type="l",ylim=c(1,1.25),xlim=c(0,0.24),xlab="",ylab="",lty=1) par(new=T) # Plot efficient frontier with risk free asset plot(musigmapairs[,4],musigmapairs[,3],type="l",ylim=c(1,1.25),xlim=c(0,0.24),xlab="",ylab="",lty=2) par(new=T) # Plot minimum variance portfolio plot((t(rep(1,4))%*%solve(covmat)%*%rep(1,4))^(-1/2),t(muvec)%*%solve(covmat)%*%rep(1,4)/t(rep(1,4))%*%solve(covmat)%*%rep(1,4),type="p",ylim=c(1,1.25),xlim=c(0,0.24),xlab="",ylab="") par(new=T) # Compute tangent portfolio c1<-(t(rep(1,4))%*%solve(covmat)%*%(muvec-1.05*rep(1,4)))^(-1) tmpwvec<-solve(covmat)%*%(muvec-1.05*rep(1,4)) tmpwvec<-as.double(c1)*tmpwvec # Plot tangent portfolio plot(sqrt(t(tmpwvec)%*%covmat%*%tmpwvec),t(muvec)%*%tmpwvec,type="p",ylim=c(1,1.25),xlim=c(0,0.24),xlab="",ylab="",pch=19) # Repeat the plots but zoom in plot(musigmapairs[,2],musigmapairs[,1],type="l",ylim=c(1.135,1.165),xlim=c(0.109,0.13),xlab="",ylab="",lty=1) par(new=T) plot(musigmapairs[,4],musigmapairs[,3],type="l",ylim=c(1.135,1.165),xlim=c(0.109,0.13),xlab="",ylab="",lty=2) par(new=T) plot((t(rep(1,4))%*%solve(covmat)%*%rep(1,4))^(-1/2),t(muvec)%*%solve(covmat)%*%rep(1,4)/t(rep(1,4))%*%solve(covmat)%*%rep(1,4),type="p",ylim=c(1.135,1.165),xlim=c(0.109,0.13),xlab="",ylab="") par(new=T) plot(sqrt(t(tmpwvec)%*%covmat%*%tmpwvec),t(muvec)%*%tmpwvec,type="p",ylim=c(1.135,1.165),xlim=c(0.109,0.13),xlab="",ylab="",pch=19)