Look Ahead Bias

Hi,


Can you please explain me why with monthly returns I have Look Ahead Bias even with using a lag , and not with daily returns, following code snippet is just example to reproduce my problem ?


###########################################

# Daily returns DO NOT generate Look Ahead Bias #

###########################################

slideFunct <- function(rets, window, step){

 sd_data <- na.omit(rollapply(rets, width = window, FUN = sd, na.rm = TRUE, align = “right”))


 final <- sd_data

 final$TYa <- sd_data$TYa / (sd_data$TYa + sd_data$USHYa + sd_data$BNKa)

 final$USHYa <- sd_data$USHYa / (sd_data$TYa + sd_data$USHYa + sd_data$BNKa)

 final$BNKa <- sd_data$BNKa / (sd_data$TYa + sd_data$USHYa + sd_data$BNKa)


 return(na.omit(final))

}


portfolioPayout = function(rets){

 weights = slideFunct(rets, window = 3, step = 1)

 return(weights)

}


portStrat = portfolio(list(“TYa”,“USHYa”,“BNKa”)) %>% 

 payout(portfolioPayout) %>% 

 evaluate()

analyse(portStrat)


stressTest(portStrat)



#########################################

# Monthly returns GENERATE Look Ahead Bias #

#########################################

slideFunct <- function(rets, window, step){

data <- to.monthly(rets, indexAt = “last”, OHLC = FALSE)

data <- na.omit(lag(data))

  

 sd_data <- na.omit(rollapply(data, width = window, FUN = sd, na.rm = TRUE, align = “right”))


 final <- sd_data

 final$TYa <- sd_data$TYa / (sd_data$TYa + sd_data$USHYa + sd_data$BNKa)

 final$USHYa <- sd_data$USHYa / (sd_data$TYa + sd_data$USHYa + sd_data$BNKa)

 final$BNKa <- sd_data$BNKa / (sd_data$TYa + sd_data$USHYa + sd_data$BNKa)


 return(na.omit(final))

}


portfolioPayout = function(rets){

 weights = slideFunct(rets, window = 3, step = 1)

 return(weights)

}


portStrat = portfolio(list(“TYa”,“USHYa”,“BNKa”)) %>% 

 payout(portfolioPayout) %>% 

 evaluate()

analyse(portStrat)


stressTest(portStrat)


Hi Greg. Let me follow up using the object portStrat you derived from the monthly returns portion. The error probably comes from the function to.monthly that you are using.


If you check out the snippet below, your original payout is not generating weights for the date 2015-11-02. However, when stressTest replicates your code via an event based manner

i.e. using rets["::2015-11-02"] instead of just rets as an input, your payout generates weights for 2015-11-02. This causes the stressTest to fail as its not replicable when subjected to a ‘live’ data feed.


rets = portStrat@returnsMatrix

tail(portfolioPayout(rets)["::2015-11-02"])

tail(portfolioPayout(rets["::2015-11-02"]))


Hope this clarifies things. You can reply to this message if you need additional information.


P.S. I would advice against using to.monthly to subset returns as it only returns you the returns of the last day of the month (and not the returns for the entire month)


Hi Yong and many thanks for your reply.


We continued our discussion on chat room and concerning the problem of “to.monthly” function that do not manage correctly missing weekend data from BB assets and you provide me the following link that help me to fix my issue:


https://stackoverflow.com/questions/22592193/r-xts-get-the-first-dates-and-values-for-each-month-from-a-daily-time-series


So I’ve been able to replace the to.monthly function “data <- to.monthly(rets, indexAt = “last”, OHLC = FALSE)” by :


data <- rets[head(endpoints(rets, “months”) + 1, -1)]


After what, no more look ahead bias during the stresTest.


Many thanks

Regards

Greg