subport_long <- function(weights){
go_long <- ifelse(weights >= 0, weights, 0)
return(go_long)
}
subport_short <- function(weights){
go_short <- ifelse(weights < 0, weights, 0)
return(go_short)
}
weights_long<-subport_long(weights)
weights_short<-subport_short(weights)
#calculate daily performance by subportfolio
final_weights_long <- weights_long[1:(nrow(weights_long)-1),]
portfolio_long <- final_weights_long * final_returns
portfolio_returns_byday_long <- apply(portfolio_long, MARGIN = 1, FUN = sum)
final_weights_short <- weights_short[1:(nrow(weights_short)-1),]
portfolio_short <- final_weights_short * final_returns
portfolio_returns_byday_short <- apply(portfolio_short, MARGIN = 1, FUN = sum)
# get correlation
long_vs_short <- bind_cols(portfolio_returns_byday_long, portfolio_returns_byday_short)
colnames(long_vs_short) <- c("long", "short")
cor(long_vs_short$long,long_vs_short$short)