public

  • Download Gist
french global factors.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#get very helpful Ken French data
#for this project we will look at Global Factors
#http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Global_Factors.zip
 
require(PerformanceAnalytics)
require(quantmod)
require(RColorBrewer)
 
 
#my.url will be the location of the zip file with the data
my.url="http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/Global_Factors.zip"
#this will be the temp file set up for the zip file
my.tempfile<-paste(tempdir(),"\\frenchfactors.zip",sep="")
#my.usefile is the name of the txt file with the data
my.usefile<-paste(tempdir(),"\\Global_Factors.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_factor <- read.table(file=my.usefile,
header = TRUE, sep = "",
as.is = TRUE,
skip = 6, nrows=261)
 
#get dates ready for xts index
datestoformat <- rownames(french_factor)
datestoformat <- paste(substr(datestoformat,1,4),
substr(datestoformat,5,7),"01",sep="-")
 
#get xts for analysis
french_factor_xts <- as.xts(french_factor[,1:NCOL(french_factor)],
order.by=as.Date(datestoformat))
 
french_factor_xts <- french_factor_xts/100
#replace -0.9999 which means data does not exist
#know there is a better method to index, but can't find my easy approach
french_factor_xts[which(french_factor_xts < -0.99,arr.ind=TRUE)[,1],
unique(which(french_factor_xts < -0.99,arr.ind=TRUE)[,2])] <- 0
 
colnames(french_factor_xts) <- c("market","size","value","momentum","tbill")
 
chart.Correlation(french_factor_xts, main="Correlation of Ken French Global Market Factors")
mtext("Source: Kenneth French Data Library http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library",
side=1,line=4,cex=0.5, col="blue",adj=0)
 
chart.RollingCorrelation(french_factor_xts[,2:5],french_factor_xts[,1],
legend.loc="topleft",width=36,lwd=3,
main="Global Factor Rolling Correlation (3 Years)",
colorset=c("darkseagreen4","slateblue3","deepskyblue3","tan4"))
mtext("Source: Kenneth French Data Library http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library",
side=1,line=2.25,cex=0.5, col="blue",adj=0)
 
require(FactorAnalytics)
chart.RollingStyle(french_factor_xts[,1],french_factor_xts[,2:NCOL(french_factor_xts)],
width=12,
colorset=c("darkseagreen3","slateblue2","deepskyblue2","tan1"),
main="Global Market Rolling 1y French Factor Weights")
mtext("Source: Kenneth French Data Library http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library",
side=1,line=1,cex=0.5, col="blue",adj=0)
 
chart.Boxplot(french_factor_xts,main="Global Factors Return Distribution",
sort.by="",mean.symbol=19,symbol.color=c("gray60","darkseagreen4","slateblue3","deepskyblue3","tan3"),
colorset=c("gray60","darkseagreen4","slateblue3","deepskyblue3","tan3"))
mtext("Source: Kenneth French Data Library http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library",
side=1,cex=0.5, col="blue")
 
require(fPortfolio)
mycolors = c("gray60","darkseagreen4","slateblue3","deepskyblue3","tan4")
frontier <- portfolioFrontier(as.timeSeries(french_factor_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))
 
 
#plot frontier points
#this method gives us much better control than frontierPlot
plot(ans,xlim=c(min(ans[,1]),max(ans[,1])+.01),ylim=c(0,0.0075),type="l",lwd=2, xlab=NA,ylab=NA)
minvariancePoints(frontier,pch=19,col="red")
tangencyPoints(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")
#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 Small and Mid Since 1984")
 
#set up function from equalWeightsPoints to also label the point
equalLabel <- function (object, return = c("mean", "mu"), risk = c("Cov", "Sigma",
"CVaR", "VaR"), auto = TRUE, ...)
{
return = match.arg(return)
risk = match.arg(risk)
data = getSeries(object)
spec = getSpec(object)
constraints = getConstraints(object)
numberOfAssets = getNAssets(object)
setWeights(spec) = rep(1/numberOfAssets, times = numberOfAssets)
ewPortfolio = feasiblePortfolio(data, spec, constraints)
assets = frontierPoints(ewPortfolio, return = return, risk = risk,
auto = auto)
text(assets, labels = "Equal-Weight", pos=4,...)
invisible(assets)
}
equalLabel(frontier,cex=0.7,col="grey")
#title(main="Efficient Frontier 2000-October 2011",xlab="Risk(cov)",ylab="Monthly Return")
 
title(main="Efficient Frontier of French Global Factors",xlab="Risk(cov)",ylab="Monthly Return")
mtext("Source: Kenneth French Data Library http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library",
side=1,cex=0.5, col="blue",adj=0)
 
weightsPlot(frontier,col=mycolors)
mtext("Source: Kenneth French Data Library http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library",
side=1,cex=0.5, col="blue",adj=0)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.