Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created June 19, 2012 17:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save timelyportfolio/2955520 to your computer and use it in GitHub Desktop.
Where are RUT fat tails
require(lattice)
require(latticeExtra)
require(directlabels)
require(reshape2)
require(quantmod)
require(PerformanceAnalytics)
#I will use a csv file of weekly returns to get more history
#but if you do not have access to that then use getSymbols for data to 1987
#getSymbols("^RUT",from="1900-01-01")
#then get weekly ROC
#rut.return <- ROC(to.weekly(RUT)[,4],type="discrete",n=1)
RUT <- read.csv("rut.csv",stringsAsFactors=FALSE)
RUT <- as.xts(RUT[,2],order.by=as.Date(RUT[,1]))
rut.return <- ROC(RUT,type="discrete",n=1)
#get skewness for short and long rolling periods
skew.short <- apply.rolling(rut.return,FUN=skewness,width=20,trim=FALSE)
colnames(skew.short) <- "roll20w"
skew.long <- apply.rolling(rut.return,FUN=skewness,width=250,trim=FALSE)
colnames(skew.long) <- "roll250w"
#do the same for kurtosis
kurtosis.short <- apply.rolling(rut.return,FUN=kurtosis,width=20,trim=FALSE)
colnames(kurtosis.short) <- "roll20w"
kurtosis.long <- apply.rolling(rut.return,FUN=kurtosis,width=250,trim=FALSE)
colnames(kurtosis.long) <- "roll250w"
#combine into data frame so we can melt as plot with lattice
skew <- as.data.frame(cbind(index(skew.short),coredata(merge(skew.short,skew.long))))
#melt to please lattice
skew.melt <- melt(skew,id.vars=1)
#clean up with good column names as properly formatted dates
colnames(skew.melt) <- c("date","measure","skew")
skew.melt[,"date"] <- as.Date(skew.melt[,"date"])
direct.label(asTheEconomist(xyplot(skew~date,groups=measure,data=skew.melt,type="l",lwd=c(1,3),
main="Russell 2000 Rolling Skewness")),"last.qp")
#combine into data frame so we can melt as plot with lattice
kurtosis <- as.data.frame(cbind(index(kurtosis.short),coredata(merge(kurtosis.short,kurtosis.long))))
#melt to please lattice
kurtosis.melt <- melt(kurtosis,id.vars=1)
#clean up with good column names as properly formatted dates
colnames(kurtosis.melt) <- c("date","measure","kurtosis")
kurtosis.melt[,"date"] <- as.Date(kurtosis.melt[,"date"])
direct.label(asTheEconomist(xyplot(kurtosis~date,groups=measure,data=kurtosis.melt,type="l",lwd=c(1,3),
main="Russell 2000 Rolling Kurtosis")),"last.qp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment