{"word_count": 554, "character_count": 5876}
Last week, we got our first class on portfolio optimization. We've seen Markowitz's theory where expected returns and the covariance matrix are given,&#62; download.file(url="http://freakonometrics.free.fr/portfolio.r",destfile = "portfolio.r")&#62; source("portfolio.r")&#62; library(zoo)&#62; library(FRAPO)&#62; library(IntroCompFinR)&#62; library(rrcov)&#62; data( StockIndex )&#62; pzoo = zoo ( StockIndex , order.by = rownames ( StockIndex ) )&#62; rzoo = ( pzoo / lag ( pzoo , k = -1) - 1 ) * 100&#62; Moments &#60;- function ( x , method = c ( "CovClassic" , "CovMcd" , "CovMest" , "CovMMest" , "CovMve" , "CovOgk" , "CovSde" , "CovSest" ) , ... ) {method &#60;- match.arg ( method )ans &#60;- do.call ( method , list ( x = x , ... ) ) + return ( getCov ( ans ) )} &#62; covmat=Moments(as.matrix(rzoo),"CovClassic")&#62; (covmat=round(covmat,1))SP500 N225 FTSE100 CAC40 GDAX  HSISP500      17.8 12.7    13.8  17.8 19.5 18.9N225       12.7 36.6    10.8  15.0 16.2 16.7FTSE100  13.8 10.8    17.3  18.8 19.4 19.1CAC40      17.8 15.0    18.8  30.9 29.9 22.8GDAX        19.5 16.2    19.4  29.9 38.0 26.1HSI          18.9 16.7    19.1  22.8 26.1 58.1&#62; er=apply(as.matrix(rzoo),2,mean)&#62; (er=round(er,1))SP500    N225 FTSE100   CAC40    GDAX     HSI0.6    -0.2     0.4     0.5     0.8     1.0&#62; ef &#60;- efficient.frontier(er, covmat, alpha.min=-2.5, alpha.max=2.5, nport=50)We can now visualize the efficient frontier (and admissible portfolios) below&#62; u=c(12,ef$sd,12,12)&#62; v=c(5,ef$er,-1,5)&#62; plot(ef$sd,ef$er,type="l",xlab="Standard Deviation",ylab="Expected Return", xlim=c(3.5,11),ylim=c(0,2.5),col="red",lwd=1.5)&#62; points(sqrt(diag(covmat)),er,pch=19,col="blue")&#62; text(sqrt(diag(covmat)),er,names(er),pos=4, col="blue",cex=.6)&#62; polygon(u,v,border=NA,col=rgb(0,0,1,.3))That was the starting point of our class. We did also mention that something important was actually hard to visualize on that graph : the correlation between returns. It is not in the points (which are univariate, with expected return and standard deviation), but in the efficient frontier. For instance, here is our correlation matrix&#62; (cormat=covmat/(sqrt(diag(covmat) %*% t(diag(covmat)))))SP500 N225 FTSE100 CAC40 GDAX HSISP500   1.00 0.50 0.79 0.76 0.75 0.59N225    0.50 1.00 0.43 0.45 0.43 0.36FTSE100 0.79 0.43 1.00 0.81 0.76 0.60CAC40   0.76 0.45 0.81 1.00 0.87 0.54GDAX    0.75 0.43 0.76 0.87 1.00 0.56HSI     0.59 0.36 0.60 0.54 0.56 1.00We can actually change the correlation between FT500 and FTSE100 (which is here .786)courbe=function(r=.786){R=cormatR[1,3]=R[3,1]=rcovmat2=(sqrt(diag(covmat) %*% t(diag(covmat))))*Ref &#60;- efficient.frontier(er, covmat2, alpha.min=-2.5, alpha.max=2.5, nport=50)plot(ef$sd,ef$er,type="l",xlab="Standard Deviation",ylab="Expected Return",xlim=c(3.5,11),ylim=c(0,2.5),col="red",lwd=1.5)points(sqrt(diag(covmat)),er,pch=19,col=c("blue","red")[c(2,1,2,1,1,1)])text(sqrt(diag(covmat)),er,names(er),pos=4,col=c("blue","red")[c(2,1,2,1,1,1)],cex=.6)polygon(u,v,border=NA,col=rgb(0,0,1,.3))}for instance, with a correlation of 0.6, we get the following efficient frontier&#62; courbe(.6)and with a stronger correlation&#62; courbe(.9)So clearly, correlation does matter. A lot. But more important, one should keep in mind that expected returns and covariances are not given, but estimated. Previously, we did use the standard estimator for the variance matrix. But another (more robust) estimator can be consideredcovmat=Moments(as.matrix(rzoo),"CovSde")er=apply(as.matrix(rzoo),2,mean)ef &#60;- efficient.frontier(er, covmat, alpha.min=-2.5, alpha.max=2.5, nport=50)plot(ef$sd,ef$er,type="l",xlab="Standard Deviation",ylab="Expected Return",xlim=c(3.5,11),ylim=c(0,2.5),col="red",lwd=1.5)points(sqrt(diag(covmat)),er,pch=19,col="blue")text(sqrt(diag(covmat)),er,names(er),pos=4,col="blue",cex=.6)polygon(u,v,border=NA,col=rgb(0,0,1,.3))It did influence (horizontal) position of points, since variances are now different, as well as the efficient frontier, with clearly much lower variances that can be reached.And to illustrate a last point, to illustrate the fact that we do have estimators based on observed returns, what if we had observed different ones? A way to get an idea of what might happened is to use bootstrap, e.g. of daily returns.&#62; covmat=Moments(as.matrix(rzoo),"CovClassic")&#62; er=apply(as.matrix(rzoo),2,mean)&#62; ef &#60;- efficient.frontier(er, covmat, alpha.min=-2.5, alpha.max=2.5, nport=50) &#62; a=sqrt(diag(covmat))&#62; b=er&#62; k=1&#62; plot(ef$sd,ef$er,type="l",xlab="Standard Deviation",ylab="Expected Return", xlim=c(3.5,11),ylim=c(0,2.5),col="white",lwd=1.5)&#62; polygon(u,v,border=NA,col=rgb(0,0,1,.3))&#62; for(i in 1:100){+     id=sample(nrow(rzoo),replace=TRUE)+     covmat=Moments(as.matrix(rzoo)[id,],"CovClassic")+     er=apply(as.matrix(rzoo)[id,],2,mean)+     points(sqrt(diag(covmat))[k],er[k],cex=.5)+ }or for another assetHere is what we got on the (estimated) efficient frontier&#62; covmat=Moments(as.matrix(rzoo),"CovClassic")&#62; er=apply(as.matrix(rzoo),2,mean)&#62; ef &#60;- efficient.frontier(er, covmat, alpha.min=-2.5, alpha.max=2.5, nport=50) &#62; plot(ef$sd,ef$er,type="l",xlab="Standard Deviation",ylab="Expected Return", xlim=c(3.5,11),ylim=c(0,2.5),col="white",lwd=1.5)&#62; points(sqrt(diag(covmat)),er,pch=19,col="blue")&#62; text(sqrt(diag(covmat)),er,names(er),pos=4, col="blue",cex=.6)&#62; polygon(u,v,border=NA,col=rgb(0,0,1,.3))&#62; for(i in 1:100){+     id=sample(nrow(rzoo),replace=TRUE)+     covmat=Moments(as.matrix(rzoo)[id,],"CovClassic")+     er=apply(as.matrix(rzoo)[id,],2,mean)+     ef &#60;- efficient.frontier(er, covmat, alpha.min=-2.5, alpha.max=2.5, nport=50)+     lines(ef$sd,ef$er,col="red")+ }Thus, it is somehow rather difficult to assess wheter a portfolio is optimal, or not... At least from a statistical perspective....