###### (1) STOCK & BOND CASE ######
library(PerformanceAnalytics)
library(quantmod)
library(FRAPO)
price = read.csv("raw.csv", row.names = 1, header = TRUE)
ro = nrow(price)
co = ncol(price)
ret = matrix(0,ro, co)
for(i in 1:2) {
ret[, i] = Delt(price[, i]) }
ret_kospi = as.matrix(ret[,1])
ret_bond = as.matrix(ret[,2])
ret_kospi[is.na(ret_kospi)] = 0
ret_bond[is.na(ret_bond)] = 0.0018
ret_kospi_12 = rbind(matrix(0,11,1),rollapply((ret_kospi+1), 12, prod) - 1)
ret = matrix(0,ro,1)
rownames(ret_kospi_12) = rownames(price)
rownames(ret) = rownames(price)
rownames(ret_kospi) = rownames(price)
rownames(ret_bond) = rownames(price)
for (i in which(rownames(ret_kospi_12) == "1995-11-30") : (ro-1) ) {
if ( ret_kospi_12[i] < 0 ) {
w = 0 }
else {
w = 1
}
w_b = 1 - w
ret[i+1] = w * ret_kospi[i+1,] + w_b * ret_bond[i+1,]
}
par(mfrow = c(3,1))
barplot( (cumprod(ret + 1) - 1) - (cumprod(ret_kospi + 1) - 1) ,axes=F, bg = "transparent", col = 0.5)
axis(4, col="black",las=1)
par(new = TRUE)
chart.CumReturns(cbind(ret_kospi,ret))
legend('topleft', c("KOSPI", "AAA"), col = 1:2, lty=1, bty='n')
chart.RollingPerformance(ret_kospi, 12)
barplot(t(cbind(apply.yearly(ret, Return.cumulative), apply.yearly(ret_kospi, Return.cumulative) )), col=1:2, beside = TRUE)
legend('topleft', c("KOSPI", "AAA"), col = 1:2, lty=1, bty='n')
###### (2) SECTOR CASE ######
price_d = read.csv("sector.csv", row.names=1)
ret_d = matrix(0,dim(price_d)[1], dim(price_d)[2])
for (i in 1 : dim(price_d)[2] ) {
ret_d[,i] = Delt(price_d[, i])
}
rownames(ret_d) = rownames(price_d)
date_m = as.matrix(read.csv("date_m.csv", header = FALSE))
price_m = matrix(0,dim(date_m)[1],dim(price_d)[2])
rownames(price_m) = date_m
for (i in 1 : dim(price_m)[1]) {
for (j in 1 : dim(price_m)[2]) {
price_m[i,j] = price_d[which(rownames(ret_d) == rownames(price_m)[i]) , j ]
}
}
ret_m = matrix(0,dim(price_m)[1], dim(price_m)[2])
for (i in 1 : dim(price_m)[2] ) {
ret_m[,i] = Delt(price_m[, i])
}
rownames(ret_m) = rownames(price_m)
ret_kospi = as.matrix(ret_m[,dim(ret_m)[2]])
ret_kospi_12 = rbind(matrix(0,11,1),rollapply((ret_kospi+1), 12, prod) - 1)
ret_m = ret_m[, 1 : (dim(ret_m)[2]-1)]
ret_d = ret_d[, 1 : (dim(ret_d)[2]-1)]
ro = dim(ret_m)[1]
co = dim(ret_m)[2]
num = 5
ret_mom = matrix(0,ro,co)
for (i in which(rownames(ret_m) == "2002-01-31") : ro ) {
for (j in 1: co) {
ret_mom[i,j] = price_m[i,j] / price_m[i-12,j] - 1
}
}
ret_mom_1m = matrix(0,ro,co)
for (i in which(rownames(ret_m) == "2002-01-31") : ro ) {
for (j in 1: co) {
ret_mom_1m[i,j] = price_m[i-1,j] / price_m[i-12,j] - 1
}
}
rownames(ret_mom) = rownames(price_m)
rownames(ret_mom_1m) = rownames(price_m)
std_roll = rbind( matrix(0,12,co), rollapply(ret_m,11,sd)[-1,][-nrow(rollapply(ret_m,11,sd)[-1,]),] )
#----------- AAA Compare -----------#
ret_port = matrix(0,dim(ret_m),5)
rownames(ret_port) = rownames(price_m)
colnames(ret_port) = c("EW","VW","MOM & EW","MOM& VW","MOM & MV" )
ret_mom_stra = matrix(0,dim(ret_m),5)
rownames(ret_mom_stra) = rownames(price_m)
colnames(ret_mom_stra) = c("12M","12M SKIP 1M","Risk Adjusted","Risk Adjusted & VW ", "Risk Adjusted & MV")
ret_mom_mix = matrix(0,dim(ret_m),5)
rownames(ret_mom_mix) = rownames(price_m)
for ( i in which(rownames(ret_m) == "2002-01-31") : ( dim(price_m)[1] - 1 ) ) {
#----------- 1. EW -----------#
ret_port[i+1,1] = mean(ret_m[i+1,])
#----------- 2. Volatility Weighted -----------#
sd = matrix(0, 1, dim(ret_m)[2] )
for (w in 1 : dim(ret_m)[2]) {
sd[1, w] = sd( ret_d[ ( which(rownames(ret_d) == rownames(ret_m)[i]) - 60 ) : which(rownames(ret_d) == rownames(ret_m)[i]), w])
}
sd = as.matrix( (1/sd) / sum((1/sd)) )
ret_port[i+1 , 2] = ret_m[i+1,] %*% t(sd)
#----------- 3. TOP 5 MOMENTUM, EW -----------#
ret_port[i+1, 3] = mean(ret_m[ i+1, which(rank(-ret_mom[i,]) <= num)])
#----------- 4. TOP 5 MOMENTUM, VW -----------#
k = which(rank(-ret_mom[i,]) <= num)
sd_vw = sd[, k]
sd_vw = (1 / sd_vw) / sum( 1/ sd_vw)
ret_port[i+1 , 4] = ret_m[i+1,k] %*% sd_vw
#----------- 5. TOP 10 MOMENTUM, MV -----------#
MV는 생략합니다
#----------- Momentum Compare -----------#
ret_mom_stra[i+1, 1] = mean(ret_m[ i+1, which(rank(-ret_mom[i,]) <= num)])
ret_mom_stra[i+1, 2] = mean(ret_m[ i+1, which(rank(-ret_mom_1m[i,]) <= num)])
ret_mom_stra[i+1, 3] = mean(ret_m[ i+1, which(rank(- ret_mom_1m[i,] / std_roll[i, ]) <= num)])
k2 = which(rank(- ret_mom_1m[i,] / std_roll[i, ]) <= num)
sd_mom = sd[,k2 ]
sd_mom = (1/sd_mom) / sum(1/sd_mom)
ret_mom_stra[i+1, 4] = ret_m[i+1,k2] %*% sd_mom
MV는 생략합니다
}
ret_port = ret_port[ which(rownames(ret_port) == "2002-01-31") : nrow(ret_port) , ]
ret_mom_stra = ret_mom_stra[ which(rownames(ret_mom_stra) == "2003-01-31") : nrow(ret_mom_stra) , ]
ret_kospi = as.matrix(ret_kospi[ which(rownames(ret_kospi) == "2003-01-31") : nrow(ret_kospi) , ])
table.AnnualizedReturns(ret_port)
table.AnnualizedReturns(ret_mom_stra)
Return.cumulative(ret_port)
Return.cumulative(ret_mom_stra)
#----------- CUMULATIVE RETURN -----------#
plot(0,type='n',axes=FALSE,ann=FALSE)
par(mfrow = c(2,1))
chart.CumReturns(cbind(ret_port, ret_kospi), main = "Adaptive Asset Allocation")
legend('topleft',c("EW","VW","MOM & EW","MOM& VW","MOM & MV","KOSPI" ),col=1:6, lty=1, cex = 0.5, bty='n')
chart.CumReturns(cbind(ret_mom_stra, ret_kospi), main = "Comparing Momentum")
legend('topleft',c("12M","12M SKIP 1M","Risk Adjusted","Risk Adjusted & VW ", "Risk Adjusted & MV", "KOSPI"),col=1:6, lty=1, cex = 0.5, bty='n')
#----------- YEARLY BARPLOT -----------#
plot(0,type='n',axes=FALSE,ann=FALSE)
par(mfrow = c(2,1))
barplot(t(cbind( apply.yearly(ret_port, Return.cumulative), apply.yearly(ret_kospi, Return.cumulative) )), col=1:6, beside = TRUE)
legend('topright',c("EW","VW","MOM & EW","MOM & VW","MOM & MV","KOSPI" ), horiz = TRUE, col=1:6, lty=1, cex = 0.4, bty='n')
barplot(t(cbind( apply.yearly(ret_mom_stra, Return.cumulative), apply.yearly(ret_kospi, Return.cumulative) )), col=1:6, beside = TRUE)
legend('topright',c("12M","12M SKIP 1M","Risk Adjusted","Risk Adjusted & VW ", "Risk Adjusted & MV", "KOSPI"), horiz = TRUE, col=1:6, lty=1, cex = 0.4, bty='n')
#----------- DRAWDOWN -----------#
plot(0,type='n',axes=FALSE,ann=FALSE)
par(mfrow = c(2,1))
chart.Drawdown(ret_port, main = "")
legend('bottomleft',c("EW","VW","MOM & EW","MOM& VW","MOM & MV"), horiz= TRUE, col=1:5, lty=1, cex = 0.6, bty='n')
chart.Drawdown(ret_mom_stra, main = "")
legend('bottomleft',c("12M","12M SKIP 1M","Risk Adjusted","Risk Adjusted & VW ", "Risk Adjusted & MV"), horiz = TRUE, col=1:5, lty=1, cex = 0.6, bty='n')