Path: blob/master/sandbox/RFinance2014/data_analysis.R
1433 views
library(PerformanceAnalytics)12source("data_prep.R")34figures.dir <- "figures"56# mix of blue, green, and red hues7my_colors <- c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c")89##### Equity Data for Example 1 and Example 2 #####10colors <- c(rep(my_colors[1], 15), rep(my_colors[2], 15), rep(my_colors[3], 5))11sd.order <- order(StdDev(equity.data))1213# boxplot to compare return distributions14# mar(bottom, left, top, right)15# default: par(mar=c(5, 4, 4, 2) + 0.1)16png(paste(figures.dir, "equity_box.png", sep="/"), height = 500, width = 1000)17boxplot(coredata(equity.data[,sd.order]),18cex.axis=0.8, las=3, ylab="Returns", pch=18,19col=colors[sd.order],20main="Return Distribution\n(sorted by StdDev)")21legend("topleft", legend=c("Large Cap", "Mid Cap", "Small Cap"),22fill=c(my_colors[1], my_colors[2], my_colors[3]), bty="n", cex=0.8)23dev.off()2425##### edhec Data for Example 3 and Example 4 #####26p <- 0.952728png(paste(figures.dir, "relative_barvar.png", sep="/"))29charts.BarVaR(R[,1:3], width=60, methods=c("ModifiedES", "ModifiedVaR"),30main="Relative Value", colorset=rep(my_colors[2], 3))31dev.off()3233png(paste(figures.dir, "directional_barvar.png", sep="/"))34charts.BarVaR(R[,4:6], width=60, methods=c("ModifiedES", "ModifiedVaR"),35main="Directional", colorset=rep(my_colors[4], 3))36dev.off()373839colors <- c(rep(my_colors[2], 3), rep(my_colors[4], 3))40ES.order <- order(ES(R, p=p, invert=FALSE))4142png(paste(figures.dir, "edhec_box.png", sep="/"), height = 500, width = 1000)43boxplot(coredata(R[,ES.order]),44cex.axis=0.8, las=3, ylab="Returns", pch=18,45col=colors[ES.order],46main="Return Distribution\n(sorted by Modified ES (95%))")47legend("topleft", legend=c("Relative Value", "Directional"),48fill=c(my_colors[1], my_colors[2]), bty="n", cex=0.8)49dev.off()5051# script for data analysis5253# library(PerformanceAnalytics)54# library(lattice)55# library(corrplot)5657# load("data/edhec.rda")58#59# head(edhec)60# R <- edhec[,1:4]61# p <- 0.9562#63# first(R)64# last(R)6566# plot the timeseries of returns67# plot(as.zoo(edhec))68# xyplot(R, scales=list(y="same"))69# charts.BarVaR(R, width=36, methods=c("ModifiedES", "ModifiedVaR"))70# dev.off()7172# boxplot to compare return distributions73# mar(bottom, left, top, right)74# default: par(mar=c(5, 4, 4, 2) + 0.1)75# par(mar=c(10, 4, 4, 2) + 0.1)76# boxplot(coredata(R[,order(ES(R, p=p, invert=FALSE))]),77# cex.axis=0.8, las=3, ylab="Returns", pch=18,78# main="Return Distribution\n(sorted by Modified ES (95%))")79# par(mar=c(5, 4, 4, 2) + 0.1)80# dev.off()8182# head(R[,order(ES(R, invert=FALSE))])83# head(R[,order(StdDev(R))])84# chart.Boxplot(R[,order(ES(R, invert=FALSE))])85# chart.Boxplot(R[,order(StdDev(R))])86# boxplot(coredata(R), col=c(2:5), cex.names=0.8, las=3)8788# chart the distribution of returns89# for(i in 1:ncol(R)){90# chart.Histogram(R[,i], methods=c("add.density", "add.normal"),91# colorset=c("lightgray", "black", "blue"))92# legend("topleft", legend=c("kernel density estimate", "normal"),93# lty=c(1,1), col=c("black", "blue"), bty="n")94# Sys.sleep(1)95# }969798# chart the correlation and covariance99# from http://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html100# cor.mtest <- function(mat, conf.level = 0.95) {101# mat <- as.matrix(mat)102# n <- ncol(mat)103# p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)104# diag(p.mat) <- 0105# diag(lowCI.mat) <- diag(uppCI.mat) <- 1106# for (i in 1:(n - 1)) {107# for (j in (i + 1):n) {108# tmp <- cor.test(mat[, i], mat[, j], conf.level = conf.level)109# p.mat[i, j] <- p.mat[j, i] <- tmp$p.value110# lowCI.mat[i, j] <- lowCI.mat[j, i] <- tmp$conf.int[1]111# uppCI.mat[i, j] <- uppCI.mat[j, i] <- tmp$conf.int[2]112# }113# }114# return(list(p.mat, lowCI.mat, uppCI.mat))115# }116# res <- cor.mtest(R)117#118# corrplot(cor(R), p.mat=res[[1]], main="Correlation",119# sig.level=0.05, tl.cex=0.8)120121# corrplot(M, method="number", bg="gray", tl.cex=0.8)122# corrplot.mixed(M, bg="gray", tl.cex=0.8)123124# If I compare sample min variance portfolio to a ledoit-shrinkage or robust,125# I should use plotcov to compare covaiance matrices126127128129130131132