Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/RFinance2014/data_analysis.R
1433 views
1
library(PerformanceAnalytics)
2
3
source("data_prep.R")
4
5
figures.dir <- "figures"
6
7
# mix of blue, green, and red hues
8
my_colors <- c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c")
9
10
##### Equity Data for Example 1 and Example 2 #####
11
colors <- c(rep(my_colors[1], 15), rep(my_colors[2], 15), rep(my_colors[3], 5))
12
sd.order <- order(StdDev(equity.data))
13
14
# boxplot to compare return distributions
15
# mar(bottom, left, top, right)
16
# default: par(mar=c(5, 4, 4, 2) + 0.1)
17
png(paste(figures.dir, "equity_box.png", sep="/"), height = 500, width = 1000)
18
boxplot(coredata(equity.data[,sd.order]),
19
cex.axis=0.8, las=3, ylab="Returns", pch=18,
20
col=colors[sd.order],
21
main="Return Distribution\n(sorted by StdDev)")
22
legend("topleft", legend=c("Large Cap", "Mid Cap", "Small Cap"),
23
fill=c(my_colors[1], my_colors[2], my_colors[3]), bty="n", cex=0.8)
24
dev.off()
25
26
##### edhec Data for Example 3 and Example 4 #####
27
p <- 0.95
28
29
png(paste(figures.dir, "relative_barvar.png", sep="/"))
30
charts.BarVaR(R[,1:3], width=60, methods=c("ModifiedES", "ModifiedVaR"),
31
main="Relative Value", colorset=rep(my_colors[2], 3))
32
dev.off()
33
34
png(paste(figures.dir, "directional_barvar.png", sep="/"))
35
charts.BarVaR(R[,4:6], width=60, methods=c("ModifiedES", "ModifiedVaR"),
36
main="Directional", colorset=rep(my_colors[4], 3))
37
dev.off()
38
39
40
colors <- c(rep(my_colors[2], 3), rep(my_colors[4], 3))
41
ES.order <- order(ES(R, p=p, invert=FALSE))
42
43
png(paste(figures.dir, "edhec_box.png", sep="/"), height = 500, width = 1000)
44
boxplot(coredata(R[,ES.order]),
45
cex.axis=0.8, las=3, ylab="Returns", pch=18,
46
col=colors[ES.order],
47
main="Return Distribution\n(sorted by Modified ES (95%))")
48
legend("topleft", legend=c("Relative Value", "Directional"),
49
fill=c(my_colors[1], my_colors[2]), bty="n", cex=0.8)
50
dev.off()
51
52
# script for data analysis
53
54
# library(PerformanceAnalytics)
55
# library(lattice)
56
# library(corrplot)
57
58
# load("data/edhec.rda")
59
#
60
# head(edhec)
61
# R <- edhec[,1:4]
62
# p <- 0.95
63
#
64
# first(R)
65
# last(R)
66
67
# plot the timeseries of returns
68
# plot(as.zoo(edhec))
69
# xyplot(R, scales=list(y="same"))
70
# charts.BarVaR(R, width=36, methods=c("ModifiedES", "ModifiedVaR"))
71
# dev.off()
72
73
# boxplot to compare return distributions
74
# mar(bottom, left, top, right)
75
# default: par(mar=c(5, 4, 4, 2) + 0.1)
76
# par(mar=c(10, 4, 4, 2) + 0.1)
77
# boxplot(coredata(R[,order(ES(R, p=p, invert=FALSE))]),
78
# cex.axis=0.8, las=3, ylab="Returns", pch=18,
79
# main="Return Distribution\n(sorted by Modified ES (95%))")
80
# par(mar=c(5, 4, 4, 2) + 0.1)
81
# dev.off()
82
83
# head(R[,order(ES(R, invert=FALSE))])
84
# head(R[,order(StdDev(R))])
85
# chart.Boxplot(R[,order(ES(R, invert=FALSE))])
86
# chart.Boxplot(R[,order(StdDev(R))])
87
# boxplot(coredata(R), col=c(2:5), cex.names=0.8, las=3)
88
89
# chart the distribution of returns
90
# for(i in 1:ncol(R)){
91
# chart.Histogram(R[,i], methods=c("add.density", "add.normal"),
92
# colorset=c("lightgray", "black", "blue"))
93
# legend("topleft", legend=c("kernel density estimate", "normal"),
94
# lty=c(1,1), col=c("black", "blue"), bty="n")
95
# Sys.sleep(1)
96
# }
97
98
99
# chart the correlation and covariance
100
# from http://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html
101
# cor.mtest <- function(mat, conf.level = 0.95) {
102
# mat <- as.matrix(mat)
103
# n <- ncol(mat)
104
# p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)
105
# diag(p.mat) <- 0
106
# diag(lowCI.mat) <- diag(uppCI.mat) <- 1
107
# for (i in 1:(n - 1)) {
108
# for (j in (i + 1):n) {
109
# tmp <- cor.test(mat[, i], mat[, j], conf.level = conf.level)
110
# p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
111
# lowCI.mat[i, j] <- lowCI.mat[j, i] <- tmp$conf.int[1]
112
# uppCI.mat[i, j] <- uppCI.mat[j, i] <- tmp$conf.int[2]
113
# }
114
# }
115
# return(list(p.mat, lowCI.mat, uppCI.mat))
116
# }
117
# res <- cor.mtest(R)
118
#
119
# corrplot(cor(R), p.mat=res[[1]], main="Correlation",
120
# sig.level=0.05, tl.cex=0.8)
121
122
# corrplot(M, method="number", bg="gray", tl.cex=0.8)
123
# corrplot.mixed(M, bg="gray", tl.cex=0.8)
124
125
# If I compare sample min variance portfolio to a ledoit-shrinkage or robust,
126
# I should use plotcov to compare covaiance matrices
127
128
129
130
131
132