-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCA_Clustering_Parity_BackTest.Rmd
297 lines (252 loc) · 10.1 KB
/
PCA_Clustering_Parity_BackTest.Rmd
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
title: "PCA-Cluster ParityToolkit"
output: md_document
---
```{r, message=FALSE, results='hide'}
#setwd("/home/creatrol/GHack")
library(Hmisc); library(xlsx); library(dplyr); library(gtable); library(ggplot2); library(data.table)
```
## Load raw data and transformation considering currencies
```{r, warning=FALSE,message=FALSE}
if (FALSE){
############################################# Load data
Equities_data <- read.csv('data/Equities_data.csv',1)
Equities_metadata <- read.csv('data/Equities_metadata.csv',1)
Currencies_data <- read.csv('data/Currencies_data.csv',1)
Currencies_metadata <- read.csv('data/Currencies_metadata.csv',1)
######################### select open price as daily price & join datasets
Edata <- select(Equities_data, 1:3,8)
rm(Equities_data)
EdataM <- select(Equities_metadata, 1,3)
Cdata <- select(Currencies_data,1:3)
CdataM <- select(Currencies_metadata,1,3)
semiData <- left_join(Edata, EdataM)
######################### treat date
semiData$Date <- as.numeric(
as.Date(as.character(semiData$Date), origin = '1899-12-30') - as.Date('1899-12-30'))
semiCurrencies <- left_join(Cdata, CdataM)
semiCurrencies$Date <- as.numeric(
as.Date(as.character(semiCurrencies$Date), origin = '1899-12-30') - as.Date('1899-12-30'))
semiCurrencies <- semiCurrencies %>% rename(Rate = Open)%>%select(-1)
######################## use data.table to realize rolling for missing data
semiCurrenciesTable <- data.table(semiCurrencies)
rm(semiCurrencies)
semiDataTable <- data.table(semiData)
rm(semiData)
setkey(semiCurrenciesTable, CRNCY, Date)
setkey(semiDataTable, CRNCY, Date)
mydata <- semiCurrenciesTable[semiDataTable, roll = +Inf]
myData <- data.frame(mydata)
rm(mydata)
######################### focus on US market
myData <- myData %>% mutate(Rate2 = ifelse(grepl("_US_",as.character(Ticker)),1,Rate)) %>%
mutate(Price = Open/Rate2) %>%
select(Ticker, Price, Date, Market.Cap)
finalOut <- myData[complete.cases(myData),]
USfinal <- finalOut[grepl("_US_",as.character(finalOut$Ticker)),]
######################### save data
save(USfinal, file = "USfinal.RData")
head(USfinal, 5)
rm(list = ls())
}
```
### using logathemic return
```{r, warning=FALSE}
load("USfinal.RData")
rankOfMarket <- USfinal %>% filter(Date == max(Date)) %>%
arrange(desc(Market.Cap))
######################### choose how many tickers
nTickets <- 150
ticketChoice <- rankOfMarket$Ticker[1:nTickets]
USfinal <- USfinal %>% filter(Ticker %in% ticketChoice) %>%
select(-Market.Cap) %>% arrange(Date)
library(tidyr)
USOut <- spread(USfinal, Ticker,Price)
#USOut[complete.cases(USOut),]
######################### derive log return
USOutT1 <- USOut[2:nrow(USOut),-1]
USOutT <- USOut[1:(nrow(USOut)-1),-1]
USOutTime <- USOut[2:nrow(USOut),1]
USlogReturn <- log(USOutT1/USOutT)
USReturn <- cbind(USOutTime,USlogReturn)
USReturn[1:5,1:5]
######################## start day
beginning <- min(USReturn$USOutTime) + as.Date('1899-12-30')
```
## Build Clustering Function
```{r, warning=FALSE,message=FALSE}
clustering <- function(dataset, numCluster, clusterIteNum, removeOutliers = 0.999){
############################################ derive cor & cov
UScorMatrix <- cor(dataset[,-1], use = "pairwise.complete.obs")
UScovMatrix <- cov(dataset[,-1], use = "pairwise.complete.obs")
################# remove NAs for cor
delete <- c()
i <- 1
for (col in 1:ncol(UScorMatrix)){
if(sum(is.na(UScorMatrix[,col])) == nrow(UScorMatrix)){
delete[i] <- col
i = i+1 }}
if (length(delete) > 0) UScorMatrix <- UScorMatrix[-(delete),-(delete)]
for (col in 1:ncol(UScorMatrix)){
for (row in 1:nrow(UScorMatrix)){
if (is.na(UScorMatrix[row,col]))
UScorMatrix[row,col] <- 0 }}
################## remove NAs for cov
delete <- c()
i <- 1
for (col in 1:ncol(UScovMatrix)){
if(sum(is.na(UScovMatrix[,col])) == nrow(UScovMatrix)){
delete[i] <- col
i = i+1 }}
if (length(delete) > 0) UScovMatrix <- UScovMatrix[-(delete),-(delete)]
for (col in 1:ncol(UScovMatrix)){
for (row in 1:nrow(UScovMatrix)){
if (is.na(UScovMatrix[row,col]))
UScovMatrix[row,col] <- 0 }}
################# change the data storage type
corDataFrame <- data.frame(UScorMatrix)
covDataFrame <- data.frame(UScovMatrix)
# covDataFrame <- data.frame(normalcovMatrix)
formap <- corDataFrame
########################################## derive matrix for clustering
for (i in 1:nrow(corDataFrame)){
for (j in 1:ncol(corDataFrame)){
corDataFrame[i,j] <- sqrt((1 - corDataFrame[i,j])/2)
}}
######################################### PCA to choose initial centers
pca.model <- prcomp(corDataFrame, center = FALSE, scale. = TRUE)
Rotation <- pca.model[2]
pca.rotation1 <- Rotation[[1]][,1]
positivePC <- pca.rotation1[order(abs(pca.rotation1), decreasing = TRUE)]
centersNames <- names(positivePC[1:numCluster])
########################################## Clustering
kcenters <- corDataFrame[centersNames, ]
set.seed(2016)
###################### inital heatmap
tmap1 <- as.matrix(formap)
map1 <- t(tmap1)
###################### clustering
corCluster <- kmeans(corDataFrame, centers=kcenters, iter.max=clusterIteNum)
kcenters <- data.frame(corCluster$centers)
####################### reorder matrix and heatmap
orderCluster <- corCluster$cluster[order(corCluster$cluster)]
clusterSize <- corCluster$size
nameList <- names(orderCluster)
newCorDataFrame <- formap[nameList,]
newCorDataFrame <- newCorDataFrame[,nameList]
tmap2 <- as.matrix(newCorDataFrame)
map2 <- t(tmap2)
return(list(orderCluster, clusterSize, covDataFrame, map1, map2))
}
```
# back test
```{r}
library(caret)
library(stats)
startDate <- 128
oneYear <- 252
intervial <- 21
marketValue <- 1
historyValue <- c(marketValue)
portfolio <- USReturn[1,-1]
change <- 0
endDate <- nrow(USReturn)
# i <- startDate
updateOrNor <- 0
for (i in startDate:endDate){
if (updateOrNor %% intervial == 0){
########################################### build portfolio by history
trainset <- USReturn[max(1,i-oneYear):(i-1),]
################### Clustering
numCluster <- 30
clusterIteNum <- 500
clusterResult <- clustering(trainset, numCluster, clusterIteNum)
orderClusters <- clusterResult[[1]]
clusterSize <- clusterResult[[2]]
################### change the portfolio
## real reasonable portfolio
covDataFrame <- clusterResult[[3]]
portfolio[1,] <- 0
##### calculate weights across clusters by variance of clusters
V_piaoList <- c()
for (clustera in 1:numCluster){
## for each cluster, calculate the distribution of portfolio to get minimum cluster variance
equityInCluster <- names(orderClusters[orderClusters == clustera])
aalpha <- as.vector(rep(1,length(equityInCluster)))
if (length(equityInCluster) > 1){
covMatrixInCluster <- as.matrix(covDataFrame[equityInCluster,equityInCluster])
divider <- as.numeric(aalpha %*%
solve(covMatrixInCluster + diag(rep(0.00001,length(equityInCluster))))
%*% aalpha)
wInCluster <- t(solve(covMatrixInCluster + diag(rep(0.00001,length(equityInCluster))))
%*% aalpha / divider)
V_piao <- as.numeric(wInCluster %*% covMatrixInCluster %*% t(wInCluster))
}
### Inf for delete outlier
else{
V_piao <- Inf#as.numeric(covDataFrame[equityInCluster,equityInCluster])
}
V_piaoList <- c(V_piaoList,V_piao)
}
## distribute weights across clusters
wAcrossCluster <- 1 / V_piaoList / sum(1 / V_piaoList)
##### calculate weights in each clusters by covariance matrix of equities
for (clustera in 1:numCluster){
clusterW <- wAcrossCluster[clustera]
## for each cluster, calculate the distribution of portfolio to get minimum cluster variance
equityInCluster <- names(orderClusters[orderClusters == clustera])
aalpha <- as.vector(rep(1,length(equityInCluster)))
if (length(equityInCluster) > 1){
covMatrixInCluster <- as.matrix(covDataFrame[equityInCluster,equityInCluster])
divider <- as.numeric(aalpha %*% solve(covMatrixInCluster + diag(rep(0.00001,length(equityInCluster))))
%*% aalpha)
wInCluster <- t(solve(covMatrixInCluster + diag(rep(0.00001,length(equityInCluster))))
%*% aalpha / divider)
totalWListInCluster <- wInCluster * clusterW
for (equities in 1:ncol(totalWListInCluster)){
portfolio[1, colnames(totalWListInCluster)[equities]] <-
totalWListInCluster[1 ,colnames(totalWListInCluster)[equities]]
}
}
}
########## check risk parity among clusters
test <- FALSE
if (test) {
## inital 0 data.frame
covDataTest <- covDataFrame
for (i in 1:nrow(covDataTest)){
for (j in 1:ncol(covDataTest)){
covDataTest[i,j] <- 0
}
}
## impute sub-covMatrix of clusters
for (clusterc in 1:numCluster){
equityInCluster <- names(orderClusters[orderClusters == clusterc])
covDataTest[equityInCluster,equityInCluster] <- covDataFrame[equityInCluster,equityInCluster]
}
riskDistribution <- as.matrix(portfolio[1, portfolio != 0]) %*% as.matrix(covDataTest)
riskAcrossClusters <- c()
}
}
updateOrNor <- updateOrNor + 1
########################################### get the current return
flush.console()
tmp <- USReturn[i,-1]
tmp <- exp(tmp)
## treat stop board
tmp[is.na(tmp)] <- 1
# print(max(abs(portfolio)))
#print(sum(portfolio))
todayValue <- sum(tmp * portfolio, na.rm = T) * marketValue
marketValue <- todayValue
historyValue <- c(historyValue, marketValue)
################## plot
time <- (startDate-1):i
#Sys.sleep(.001)
#print(USReturn[i,1]+ as.Date("1899-12-30"))
}
plot(time,historyValue,type = 'l')
title(main = paste0(as.character(nTickets), " equities, in ",
as.character(numCluster), " clusters"))
# save(historyValue, file = "historyValue.RData")
```