Created
December 15, 2011 20:59
-
-
Save timelyportfolio/1482847 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#use Ken French market cap series | |
#http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_ME.zip | |
require(PerformanceAnalytics) | |
require(quantmod) | |
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Portfolios_Formed_on_ME.zip" | |
my.tempfile<-paste(tempdir(),"\\frenchsize.zip",sep="") | |
my.usefile<-paste(tempdir(),"\\Portfolios_Formed_on_ME.txt",sep="") | |
download.file(my.url, my.tempfile, method="auto", | |
quiet = FALSE, mode = "wb",cacheOK = TRUE) | |
unzip(my.tempfile,exdir=tempdir(),junkpath=TRUE) | |
#read space delimited text file extracted from zip | |
french_size <- read.table(file=my.usefile, | |
header = TRUE, sep = "", | |
as.is = TRUE, | |
skip = 13, nrows=1020) | |
#for simplicity use small mid large but series contains more granular also | |
colnames(french_size)[3:5] <- c("Small30","Mid40","Large30") | |
#get dates ready for xts index | |
datestoformat <- french_size[,1] | |
datestoformat <- paste(substr(datestoformat,1,4), | |
substr(datestoformat,5,7),"01",sep="-") | |
#get xts for analysis | |
french_size_xts <- as.xts(french_size[,3:5], | |
order.by=as.Date(datestoformat)) | |
mycolors = c("indianred3","slateblue4","darkolivegreen4") | |
french_size_xts <- french_size_xts/100 | |
#jpeg#"size risk return.jpeg",height=6.5,width=6.5,res=96,units="in") | |
chart.RiskReturnScatter(french_size_xts, main="Risk and Return by Size | |
U. S. Stocks Since 1926",xlim=c(0,0.35),colorset=mycolors) | |
mtext("Source: http://mba.tuck.dartmouth.edu/pages/faculty/ken.french", | |
side=1,line=2,cex=0.8,adj=0) | |
#dev.off() | |
#jpeg#"size correlation.jpeg",height=6.5,width=6.5,res=96,units="in") | |
chart.Correlation(french_size_xts, main="Correlation by Size | |
U. S. Stocks Since 1926") | |
mtext("Source: http://mba.tuck.dartmouth.edu/pages/faculty/ken.french", | |
side=1,line=2,cex=0.8,adj=0) | |
#dev.off() | |
frontier <- portfolioFrontier(as.timeSeries(french_size_xts)) | |
pointsFrontier = frontierPoints(frontier, frontier = "both", auto=TRUE) | |
targetRisk = getTargetRisk(frontier@portfolio)[,1] | |
targetReturn = getTargetReturn(frontier@portfolio)[,1] | |
ans = cbind(Risk = targetRisk, Return = targetReturn) | |
colnames(ans) = c("targetRisk", "targetReturn") | |
rownames(ans) = as.character(1:NROW(ans)) | |
#points(ans) | |
#jpeg#"size frontier.jpeg",height=6.5,width=6.5,res=96,units="in") | |
plot(ans,xlim=c(min(ans[,1]),max(ans[,1])+.025),ylim=c(0,0.016),type="l",lwd=2, xlab=NA,ylab=NA) | |
#frontierPlot(frontier, pch=19,title=FALSE,xlim=c(min(ans[,1]),max(ans[,1])+.025),ylim=c(0,0.016),add=FALSE) | |
#minvariancePoints(frontier,pch=19,col="red") | |
#tangencyPoints(frontier,pch=19,col="blue") | |
#tangencyLines(frontier,pch=19,col="blue") | |
equalWeightsPoints(frontier,pch=15,col="grey") | |
singleAssetPoints(frontier,pch=19,cex=1.5,col=mycolors) | |
#twoAssetsLines(frontier,lty=3,col="grey") | |
#sharpeRatioLines(frontier,col="orange",lwd=2) | |
#legend("topleft",legend=colnames(french_size_xts),pch=19,col=mycolors, | |
# cex=0.65) | |
#label assets | |
stats <- getStatistics(frontier) | |
text(y=stats$mean,x=sqrt(diag(stats$Cov)),labels=names(stats$mean),pos=4,col=mycolors,cex=0.7) | |
title(main="Efficient Frontier by Size since 1926",xlab="Risk(cov)",ylab="Monthly Return") | |
mtext("Source: http://mba.tuck.dartmouth.edu/pages/faculty/ken.french", | |
side=1,line=2,cex=0.8,adj=0) | |
#dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment