I am trying to estimate the lagged covariance between two vector. I have used gsignal::xcov
, forecast::Ccf
and stats::ccf
. My code has several nested loops so computing time is pilling up, with gsignal::xcov
being the fastest but still extremely slow. The very same code runs on MATLAB (using xcov
) 10+ times faster. I also tried lapply
, but it was even slower. I know I could use parallel computing to speed it up, but for irrelevant reasons I cannot do that.
Thanks!
Edit: I included an example of my code in R and Matlab. R took 47.8 seconds, Matlab only 1.4 seconds.
R Code
rm(list=ls())
starting_time <- Sys.time()
library(utils)
library(IRISSeismic)
library(pracma)
library(gsignal)
set.seed(1)
a <- rnorm(10000)
b <- rnorm(10000)
c <- rnorm(10000)
d <- rnorm(10000)
e <- rnorm(10000)
f <- rnorm(10000)
data <- cbind(a,b,c,d,e,f)
channels_number <- dim(data)[2]
combinations <- combn(1:channels_number,2)
scales <- seq(15,100,5)
for (scale in scales){
finish <- 0
for (window in 1:floor(dim(data)[1]/scale)){
print(paste0("Starting scale: ",scale, " window: ", window))
start <- finish + 1
finish <- start + scale-1
tmp <- data[start:finish,]
detrended <- zeros(dim(tmp)[1],dim(tmp)[2])
for (channel in 1:channels_number){
fit <- polyfit(1:dim(tmp)[1],tmp[,channel],2)
fit <- polyval(fit,1:dim(tmp)[1])
detrended[,channel] <- tmp[,channel] - fit
}
tmp <- detrended
##### Resource Heavy Part #####
for (combo in 1:dim(combinations)[2]){
ch1 <- combinations[1,combo]
ch2 <- combinations[2,combo]
signal_1 <- tmp[,ch1]
signal_2 <- tmp[,ch2]
xcov_results <- xcov(signal_1,signal_2,scale = 'biased')
}
}
}
ending_time <- Sys.time()
ending_time - starting_time
Matlab Code
clear all
clc
tic
a = rand(10000,1);
b = rand(10000,1);
c = rand(10000,1);
d = rand(10000,1);
e = rand(10000,1);
f = rand(10000,1);
data = [a,b,c,d,e,f];
channels_number = size(data,2);
combinations = nchoosek(1:channels_number,2);
scales = 15:5:100;
for s = 1:size(scales,2)
scale = scales(s);
finish = 0;
for window = 1:floor(size(data,1)/scale)
disp("Starting scale: " + scale + " window: " + window)
start = finish + 1;
finish = start + scale-1;
tmp = data(start:finish,:);
tmp = detrend(tmp,2);
%%%%% Resource Heavy Part %%%%%
for combo = 1:size(combinations,2)
ch1 = combinations(1,combo);
ch2 = combinations(2,combo);
signal_1 = tmp(:,ch1);
signal_2 = tmp(:,ch2);
xcov_results = xcov(signal_1,signal_2,'biased');
end
end
end
toc