Evolving Domestic Frontier

November 1, 2011
By

(This article was first published on Timely Portfolio, and kindly contributed to R-bloggers)

When we learn the efficient frontier, most are misled to believe that the frontier is static and unchanging.  However, we should have all learned by recent experience that the frontier is as volatile as the assets that construct it.  If we look at just US Stocks (SP 500) and US Bonds (Barclays Agg), we can see how this shifting frontier can dramatically affect your returns.

Using R and fPortfolio, let’s construct the frontier every rolling 5-year period to see what has happened since 1975.

From TimelyPortfolio

R code (click to download from Google Docs):

require(quantmod)
require(fPortfolio)
require(reshape)   #############################################################
#get data; unfortunately cannot share since I would violate
#copyright
sp_agg <- read.csv("C:\\Users\\Kent.TLEAVELL_NT\\Documents\\old\\R\\sp_agg.csv",stringsAsFactors=FALSE)
sp_agg <- sp_agg[2:NROW(sp_agg),3:NCOL(sp_agg)]
sp_agg <- sp_agg[,c(1,3,5)]   len <- nchar(sp_agg[,1])
xtsdate <- paste(substr(sp_agg[,1],len-3,len),"-",
ifelse(len==9,"0",""),substr(sp_agg[,1],1,len-8),"-01",sep="")   sp_agg.xts <- xts(data.matrix(sp_agg[,2:NCOL(sp_agg)]),order.by=as.Date(xtsdate))
sp_agg.xts <- sp_agg.xts/100
#############################################################     #for svg
#require(Cairo)
#CairoSVG("frontier.svg", width=8 ,height=8 )
#jpeg(filename="evolving frontier.jpg",
# quality=100,width=6, height = 7.5, units="in",res=96)#############################################################
## spec -
Spec = portfolioSpec()
setTargetReturn(Spec) = mean(colMeans(as.timeSeries(sp_agg.xts)))   ## constraints -
Constraints = "LongOnly"
#get frontiers by 5-year range
from = time(as.timeSeries(sp_agg.xts))[c(1,1,49,109,169,229,289,349,385)]
to = time(as.timeSeries(sp_agg.xts))[c(NROW(sp_agg.xts),48,108,168,228,288,348,NROW(sp_agg.xts)-8,NROW(sp_agg.xts)-8)]   rollFron <- rollingPortfolioFrontier(as.timeSeries(sp_agg.xts),Spec,Constraints,
from=from,to=to)   #chartcol <- topo.colors(length(rollFron))
chartcol <- 1:length(rollFron)
#hindsight bias; yellow is too hard to read so change
chartcol[length(rollFron)-2] <- "goldenrod"     i=1
frontierPlot(rollFron[[1]],col=rep(chartcol[1],2),xlim=c(0,0.08),ylim=c(-0.01,0.025))
frontierlabels <- frontierPoints(rollFron[[i]])
text(x=frontierlabels[NROW(frontierlabels),1],y=frontierlabels[NROW(frontierlabels),2],
labels=paste(from[i]," to ",to[i],sep=""),
pos=4,offset=0.5,cex=0.5,col = chartcol[i])   for (i in 2:(length(rollFron)-3) ) {
frontierPlot(rollFron[[i]],add=TRUE,col = rep(chartcol[i],2),pch=19,auto=FALSE,
title=FALSE)
frontierlabels <- frontierPoints(rollFron[[i]])
text(x=frontierlabels[NROW(frontierlabels),1],y=frontierlabels[NROW(frontierlabels),2],
labels=paste(from[i]," to ",to[i],sep=""),
pos=4,offset=0.5,cex=0.5,col = chartcol[i])
}   i=7
lowerFrontier = frontierPoints(rollFron[[i]], frontier = "both")
points(lowerFrontier,col = rep(chartcol[i],2),pch=19)
frontierlabels <- frontierPoints(rollFron[[i]])
text(x=frontierlabels[1,1],y=frontierlabels[1,2],
labels=paste(from[i]," to ",to[i],sep=""),
pos=4,offset=0.5,cex=0.5,col = chartcol[i])     #legend("topright",legend=paste(from,to,sep=" "),pch=19,
# col=chartcol,cex=0.7)
for (i in 8:length(rollFron)) {
lowerFrontier = frontierPoints(rollFron[[i]], frontier = "lower")
points(lowerFrontier,col = rep(chartcol[i],2),pch=19)
frontierlabels <- frontierPoints(rollFron[[i]],frontier="lower")
text(x=frontierlabels[1,1],y=frontierlabels[1,2],
labels=paste(from[i]," to ",to[i],sep=""),
pos=4,offset=0.5,cex=0.5,col = chartcol[i])
}
#
#frontier <- portfolioFrontier(as.timeSeries(sp_agg.xts))
#frontierPlot(frontier,col=rep(chartcol[length(rollFron)+1],2),add=TRUE,pch=19,auto=FALSE,
# title=FALSE)   #dev.off()

Created by Pretty R at inside-R.org

To leave a comment for the author, please follow the link and comment on his blog: Timely Portfolio.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.