滚动 Mean/standard 与条件的偏差

Rolling Mean/standard deviation with conditions

我有一些关于根据条件计算滚动 Mean/standard 偏差的问题。老实说,这更像是一个语法问题,但由于我认为它大大降低了我的代码速度,所以我认为我应该在这里询问它以了解发生了什么。我有一些包含 Stock NameMidquotes 等列的财务数据,我想根据股票计算滚动平均值和滚动标准偏差。

现在我想计算每只股票的波动率,这是通过采用前 20 个中间报价的滚动标准差来完成的。为此,经过Whosebug论坛的搜索,找到了使用data.table包的一行如下:

DT[, volatility:=( roll_sd(DT$Midquotes, 20, fill=0, align = "right") ), by = Stock]

其中 DT 是包含我所有数据的 data.table

现在,这在计算上非常慢,尤其是当我将它与没有任何条件的典型滚动标准偏差计算进行比较时:

DT$volatility <- roll_sd(DT$Midquotes, 20, fill=0, align = "right")

但是当我尝试用条件做滚动标准偏差类似的事情时,R 不会让我这样做:

DT$volatility <- DT[, ( roll_sd(DT$Midquotes, 20, fill=0, align = "right") ), by = Stock]

这一行出现错误:

Error: cannot allocate vector of size 10.9 Gb

所以我只是想知道,为什么这条线:DT[, volatility:=( roll_sd(DT$Midquotes, 20, fill=0, align = "right") ), by = Stock] 这么慢?每次为每个不同的股票计算滚动标准偏差时,它是否可能复制整个 data.table

我认为你的问题是你使用了 := 函数并且你在方括号内使用了 DT 。我假设您的设置类似于:

> library(data.table)
> set.seed(83385668)
> DT <- data.table(
+   x     = rnorm(5 * 3), 
+   stock = c(sapply(letters[1:3], rep, times = 5)),
+   time  = c(replicate(3, 1:5)))
> DT
              x stock time
 1:  0.25073356     a    1
 2: -0.24408170     a    2
 3: -0.87475856     a    3
 4:  0.50843761     a    4
 5: -1.91331773     a    5
 6:  0.07850094     b    1
 7: -0.15922989     b    2
 8:  1.09806870     b    3
 9:  0.27995610     b    4
10:  0.45090842     b    5
11:  0.03400554     c    1
12: -0.34918734     c    2
13:  2.16602740     c    3
14: -0.04758261     c    4
15:  1.24869663     c    5

我不确定 roll_sd 函数的来源。但是,您可以计算例如zoo 库的滚动平均值如下:

> library(zoo)
> setkey(DT, stock, time) # make sure data is sorted by time
> DT[, rollmean := rollmean(x, k = 3, fill = 0, align = "right"), 
+    by = .(stock)]
> DT
              x stock time   rollmean
 1:  0.25073356     a    1  0.0000000
 2: -0.24408170     a    2  0.0000000
 3: -0.87475856     a    3 -0.2893689
 4:  0.50843761     a    4 -0.2034676
 5: -1.91331773     a    5 -0.7598796
 6:  0.07850094     b    1  0.0000000
 7: -0.15922989     b    2  0.0000000
 8:  1.09806870     b    3  0.3391132
 9:  0.27995610     b    4  0.4062650
10:  0.45090842     b    5  0.6096444
11:  0.03400554     c    1  0.0000000
12: -0.34918734     c    2  0.0000000
13:  2.16602740     c    3  0.6169485
14: -0.04758261     c    4  0.5897525
15:  1.24869663     c    5  1.1223805

或等同于

> DT[, `:=`(rollmean = rollmean(x, k = 3, fill = 0, align = "right")), 
+    by = .(stock)]
> DT
              x stock time   rollmean
 1:  0.25073356     a    1  0.0000000
 2: -0.24408170     a    2  0.0000000
 3: -0.87475856     a    3 -0.2893689
 4:  0.50843761     a    4 -0.2034676
 5: -1.91331773     a    5 -0.7598796
 6:  0.07850094     b    1  0.0000000
 7: -0.15922989     b    2  0.0000000
 8:  1.09806870     b    3  0.3391132
 9:  0.27995610     b    4  0.4062650
10:  0.45090842     b    5  0.6096444
11:  0.03400554     c    1  0.0000000
12: -0.34918734     c    2  0.0000000
13:  2.16602740     c    3  0.6169485
14: -0.04758261     c    4  0.5897525
15:  1.24869663     c    5  1.1223805

我在数据处理中遇到了计算滚动标准的同样问题process.So我查看了这个网站。而且我认为您的问题是使用 DT$Midquotes 而不是 .SD$Midquotes。 .SD 是一个 data.table,包含每个组的 x 数据子集。 roll_sd 函数来自包"RcppRoll"。 你可以这样试试

DT[, (sd = roll_sd(.SD$Midquotes, 20, fill=0, align = "right")), by = .(Stock)]

现在 data.table 本身也有一个滚动平均函数,请参阅 github disscussion 了解详情。实现非常简单。

DT[, rollmean := data.table::frollmean(x, n = 3, fill = 0, align = "right"), 
by = .(stock)]

两者的快速基准测试表明 data.table 版本(大部分时间)更快一些。

library(microbenchmark)

microbenchmark(a = DT[, rollmean := data.table::frollmean(x, n = 3, fill = 0, align = "right"), 
                      by = .(stock)]
               , b = DT[, rollmean := rollmean(x, k = 3, fill = 0, align = "right"),
                            by = .(stock)]
, times = 100L

)

Unit: milliseconds
expr    min      lq     mean  median     uq     max neval cld
   a 1.5695 1.66605 2.329675 1.79340 2.1980 39.3750   100  a 
   b 2.6711 2.82105 3.660617 2.99725 4.3577 20.3178   100   b