Search code examples
rtime-seriesentropyrolling-computation

Entropy analysis using rolling window


I want to calculate the entropy values for the multiple time series with the rolling window size of 500. I used roll-apply function with my code, but it is not working.

Please help me to calculate entropy (i.e. with the below code) in the rolling window size of 500.

For your convenience, here I am providing my entropy code so that it can be easier for you to suggest the application of rolling window size of 500 in the entropy method.

N<-nrow(ts)
r<-matrix(0, nrow = N, ncol = 1)
for (i in 1:N){
    r[i]<-approx_entropy(ts[,i], edim = 2, r = 0.2*sd(ts[,i]), elag = 1)
}

Solution

  • Modify version: code:

    library('zoo')
    ts <- matrix(rnorm(100000),1000,100)
    library('zoo')
    ts <- matrix(rnorm(100000),1000,100)
    roll <- function(x){
      entropy <- function(x){
        output <- any function or code you like to put hear
        return(output)
      }
      r <- rollapply(x,width=500,by=1,FUN=entropy)
      return(r)
    }
    res <- apply(ts ,2,roll)
    

    Old version: the avobe code should work, it is an example of how can you do it:

    library('zoo')
    ts <- rnorm(10000)
    entropy <- function(x){
       return(approx_entropy(x,edim = 2, r = 0.2*sd(x), elag = 1))
    }
    r <- rollapply(ts,width=500,by=1,FUN=entropy)
    

    Realize that as you are computing a rolling window function the output will have size equal to length(ts)-window_length+1. If this don't work, with your data, please post an example of your data in order to see which is the problem. Also your function approx_entropy, because the problem/error may be there. If you probe the code with a simple functions as mean it work. Hope it help !