I am working on market efficiency tests for a research project, where I am using dynamic hurst exponentials and Lo & MacKinlay variance ratio tests. To do this, I was planning on using the pracma and vrtest packages. The problem occurs when I am running the tests, because they are not inherently dynamic, meaning that to get a time-varying measure I will essentially need to run the following codes:
hurstexp(data$GB[2:101])$Hs
hurstexp(data$GB[3:102])$Hs
hurstexp(data$GB[4:103])$Hs
Lo.Mac(data$GB[2:101] , c(2, 4, 8, 16))
Lo.Mac(data$GB[3:102] , c(2, 4, 8, 16))
Lo.Mac(data$GB[4:103] , c(2, 4, 8, 16))
Since I am working on a data set with over 3000 observations it would be useful if this could be automated.
My thought was that I could create a variable which loops the interval of 100 sample values through the entire population, i.e. a rolling window variable. If everything works, then I would be able to insert the rolling window variable in to the package functions and get the 100 day hurst exponent/VR(k) for each overlapping increment. This is the loop and the results I arrived at:
lv = for (i in 1:(length(data$GB)-interval)) {
j = i + interval
data$GB[i:j]
}
lv
library(pracma)
hurstexp(lv)$Hs
library(vrtest)
Lo.Mac(lv , c(2, 4, 8, 16))
Errors/output: NULL Error in hurstexp(lv) : is.numeric(x) is not TRUE Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),:'data' must be of a vector type, was 'NULL'
The data variable data$GB consists of log returns. To reproduce the data the following code can be used:
reproduce = rnorm(3000, mean = -0.0004832595, sd = 0.003566892)
Here are some examples. The third argument to rollapplyr
can be any function of the input window that returns a scalar or vector.
library(zoo)
x <- 1:10 # test data
rollapplyr(x, 3, sum, fill = NA)
## [1] NA NA 6 9 12 15 18 21 24 27
rollapplyr(x, 3, c, fill = NA)
## [,1] [,2] [,3]
## [1,] NA NA NA
## [2,] NA NA NA
## [3,] 1 2 3
## [4,] 2 3 4
## [5,] 3 4 5
## [6,] 4 5 6
## [7,] 5 6 7
## [8,] 6 7 8
## [9,] 7 8 9
## [10,] 8 9 10
rollapplyr(x, 3, toString, fill = NA)
## [1] NA NA "1, 2, 3" "2, 3, 4" "3, 4, 5" "4, 5, 6"
## [7] "5, 6, 7" "6, 7, 8" "7, 8, 9" "8, 9, 10"
library(vrtest)
library(zoo)
set.seed(123)
y <- rnorm(10, mean = -0.0004832595, sd = 0.003566892)
rollapplyr(y, 7, function(y) Lo.Mac(y = y, kvec = c(2, 4))$Stats)
## M11 M12 M21 M22
## [1,] -0.73484310 -1.0934219 -1.0728625 -1.5237487
## [2,] -1.14450313 -1.1145049 -3.1935343 -1.8419056
## [3,] -0.09729379 -1.0237798 -0.1648527 -1.4823474
## [4,] 0.73536076 -0.7280281 1.1079997 -0.9003987