【问题标题】:R: Is there a fast approximate correlation library for large time series?R:是否有用于大时间序列的快速近似相关库?
【发布时间】:2012-02-15 06:35:47
【问题描述】:

我正在尝试制作一个软件,可以实时找到前 N 个相关时间序列窗口(查询序列)。

大约有 5000 个窗口,每个窗口长 34 行。关于查询系列,我需要 300 个最相关的窗口。

目前我正在使用cor 函数,但事实证明它太慢了。我需要不到一秒钟的响应时间。低于 250 毫秒会很棒,但附近的任何东西都可以。

是否有一个 R 的“快速近似相关”库,我可以使用它来减少我的大型“参赛者列表”(5000 个窗口)的大小?

如果没有,是否有另一种方法可以稍微缩小此列表?

这是我正在运行的函数:

GetTopN<-function(n)
{ 
  Rprof()

  x<- LastBars()
  x<-as.data.frame(cbind(x[-1,1],diff(x[,2])))

  colnames(x)<-c('RPos','M1')
  actionlist<-GetFiltered()
  print(nrow(actionlist))
  crs<-mat.or.vec(nrow(actionlist),2) #will hold correlations  


  for(i in 1:nrow(actionlist))
  {

       crs[i,2]<-cor(z[actionlist$RPos[i]+n1,2],x[,2])  
  }

  crs[,1]<-actionlist$OpenTime
  sorted <- crs[order(crs[,2], decreasing=T),1:2] 
  topx<- head(sorted,n)
  bottomx <- tail(sorted,n)
  rownames(bottomx)<-NULL
  DF<-as.data.frame(rbind(topx,bottomx),row.names=NULL ) 
  colnames(DF)<-c('ptime','weight')  
  sqlSave(channel,dat=DF,tablename='ReducedList',append=F,rownames=F,safer=F) 
  FillActionList()
  Rprof(NULL)
  summaryRprof()  
}

这是summaryRprof的输出:

$by.self
              self.time self.pct total.time total.pct
[.data.frame       0.68    25.37       0.98     36.57
.Call              0.22     8.21       0.22      8.21
cor                0.16     5.97       2.30     85.82
is.data.frame      0.14     5.22       1.26     47.01
[                  0.14     5.22       1.12     41.79
stopifnot          0.14     5.22       0.30     11.19
sys.call           0.14     5.22       0.18      6.72
GetTopN            0.12     4.48       2.68    100.00
eval               0.10     3.73       0.46     17.16
deparse            0.10     3.73       0.34     12.69
%in%               0.10     3.73       0.22      8.21
$                  0.10     3.73       0.10      3.73
c                  0.08     2.99       0.08      2.99
.deparseOpts       0.06     2.24       0.14      5.22
formals            0.06     2.24       0.08      2.99
pmatch             0.06     2.24       0.08      2.99
names              0.06     2.24       0.06      2.24
match              0.04     1.49       0.12      4.48
sys.parent         0.04     1.49       0.04      1.49
match.arg          0.02     0.75       0.58     21.64
length             0.02     0.75       0.02      0.75
matrix             0.02     0.75       0.02      0.75
mode               0.02     0.75       0.02      0.75
order              0.02     0.75       0.02      0.75
parent.frame       0.02     0.75       0.02      0.75
sys.function       0.02     0.75       0.02      0.75

$by.total
              total.time total.pct self.time self.pct
GetTopN             2.68    100.00      0.12     4.48
cor                 2.30     85.82      0.16     5.97
is.data.frame       1.26     47.01      0.14     5.22
[                   1.12     41.79      0.14     5.22
[.data.frame        0.98     36.57      0.68    25.37
match.arg           0.58     21.64      0.02     0.75
eval                0.46     17.16      0.10     3.73
deparse             0.34     12.69      0.10     3.73
stopifnot           0.30     11.19      0.14     5.22
.Call               0.22      8.21      0.22     8.21
%in%                0.22      8.21      0.10     3.73
sqlQuery            0.20      7.46      0.00     0.00
sys.call            0.18      6.72      0.14     5.22
odbcQuery           0.18      6.72      0.00     0.00
GetFiltered         0.16      5.97      0.00     0.00
match.call          0.16      5.97      0.00     0.00
.deparseOpts        0.14      5.22      0.06     2.24
match               0.12      4.48      0.04     1.49
$                   0.10      3.73      0.10     3.73
c                   0.08      2.99      0.08     2.99
formals             0.08      2.99      0.06     2.24
pmatch              0.08      2.99      0.06     2.24
names               0.06      2.24      0.06     2.24
sys.parent          0.04      1.49      0.04     1.49
LastBars            0.04      1.49      0.00     0.00
length              0.02      0.75      0.02     0.75
matrix              0.02      0.75      0.02     0.75
mode                0.02      0.75      0.02     0.75
order               0.02      0.75      0.02     0.75
parent.frame        0.02      0.75      0.02     0.75
sys.function        0.02      0.75      0.02     0.75
mat.or.vec          0.02      0.75      0.00     0.00
odbcFetchRows       0.02      0.75      0.00     0.00
odbcUpdate          0.02      0.75      0.00     0.00
sqlGetResults       0.02      0.75      0.00     0.00
sqlSave             0.02      0.75      0.00     0.00
sqlwrite            0.02      0.75      0.00     0.00

$sample.interval
[1] 0.02

$sampling.time
[1] 2.68

查看summaryRprofs 的输出,似乎[.data.frame 花费的时间最长。我不知道如何解决这个问题。

【问题讨论】:

  • 您的数据是什么样的?使用cor 计算 34 x 50k 矩阵和 34 元素向量的最高 300 个相关系数需要 30 毫秒。与system.time 上的 34 x 5000 矩阵寄存器 0 相关。查询向量与创建 here 的 30 x 199971 矩阵的相关性大约需要 180 毫秒
  • 您是否分析过您的代码并检查大部分时间确实花在了cor 函数上?您使用的是哪个linear algebra library?您是否尝试过并行化您的代码(一些线性代数库会为您做到这一点)?
  • 我只是在使用统计库。我没有尝试并行化它。
  • 85% 的时间花在 cor 函数上,但只有 8% 实际计算相关性(在 .Call 中):大部分时间花在“簿记任务”上(检查参数、重新格式化数据、查找缺失值等)。如果您设法跳过所有这些处理并直接调用.Internal(cor(...)),您应该能够实现显着的加速。
  • 另一个潜在的(更容易)加速是摆脱你调用cor的循环:如果函数看起来很慢,可能是因为它被调用了很多很多次。

标签: r time-series


【解决方案1】:

正如 Vincent 在 cmets 中指出的那样,计算 (Pearson) 相关性本身非常快。一旦你用尽了基本的 R 分析和加速技巧,你总是可以去

  • 多核和/或通过适当的 R 包并行

  • 使用编译后的代码,我可以想到一个包来促进它

  • 甚至将 GPU 视为例如我的 Intro to High-Performance Computing with R 幻灯片(在我的 presentations page 上)包含一个计算(更昂贵的 Kendall)相关性以获得大收益的示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2011-06-29
    • 2012-02-07
    相关资源
    最近更新 更多