r - 计算滚动窗口协方差矩阵

标签 r xts

我正在尝试计算多个 Assets 的滚动窗口(移动 1 天)协方差矩阵。

假设我的 df 看起来像这样:

df <- data.frame(x = c(1.5,2.3,4.7,3,8.4), y =c(5.3,2.4,8.4,1.3,2.5),z=c(2.5,1.3,6.5,4.3,2.8),u=c(1.1,2.5,4.3,2.5,6.3))

我希望输出如下所示:

cov(df[1:3,])  :

         x        y        z        u
x 2.773333 3.666667 4.053333 2.613333
y 3.666667 9.003333 7.846667 2.776667
z 4.053333 7.846667 7.413333 3.413333
u 2.613333 2.776667 3.413333 2.573333



cov(df[2:4,])  :

         x         y        z    u
x 1.523333  4.283333 3.053333 1.23
y 4.283333 14.603333 7.253333 3.93
z 3.053333  7.253333 6.813333 2.22
u 1.230000  3.930000 2.220000 1.08



cov(df[3:5,])  :

           x          y         z          u
x  7.6233333 -0.5466667 -3.008333  5.1633333
y -0.5466667 14.4433333  5.941667  0.9233333
z -3.0083333  5.9416667  3.463333 -1.5233333
u  5.1633333  0.9233333 -1.523333  3.6133333

但是一切都在循环中进行,因为我在数据集中有很多行...

  1. 如果我想通过将滚动窗口移动 1 天来滚动计算协方差矩阵,可能的 for 循环会是什么样子?或者我应该使用一些 apply 系列函数吗?

  2. 如果我想为上面的循环创建一个时间序列对象,哪个时间序列类更好?现在我使用 fPortfolio 包中的 as.timeSeries

我根本无法理解...

最好的问候

最佳答案

要创建滚动窗口,您可以使用 embed

## your data.frame
df <- data.frame(x=c(1.5,2.3,4.7,3,8.4), y=c(5.3,2.4,8.4,1.3,2.5), z=c(2.5,1.3,6.5,4.3,2.8), u=c(1.1,2.5,4.3,2.5,6.3))

## define windows
windowSize <- 3
windows <- embed(1:nrow(df), windowSize)

lapplyApproach <- function(df, windows) {
    ## convert window matrix to a list
    windowsList <- split(t(windows), rep(1:nrow(windows), each=ncol(windows)))
    ## edit: customize names: "from:to"
    names(windowsList) <- unlist(lapply(windowsList, function(x)paste(range(x), sep="", collapse=":")))
    return(lapply(windowsList, function(x)cov(df[x, ])))
}

forApproach <- function(df, windows) {
    l <- vector(mode="list", length=nrow(windows))
    for (i in 1:nrow(windows)) {
        l[[i]] <- cov(df[windows[i, ], ])
    }
    return(l)
}

## check results
all.equal(forApproach(df, windows), unname(lapplyApproach(df, windows)))
# TRUE

## test running time
library("rbenchmark")

## small example
benchmark(lapplyApproach(df, windows), forApproach(df, windows), order="relative")
#                         test replications elapsed relative user.self sys.self user.child sys.child
#2    forApproach(df, windows)          100   0.075     1.00     0.072        0          0         0                                                                          
#1 lapplyApproach(df, windows)          100   0.087     1.16     0.084        0          0         0

## a larger one
n <- 1e3
df <- data.frame(x=rnorm(1:n), y=rnorm(1:n), z=rnorm(1:n), u=rnorm(1:n))
windows <- embed(1:nrow(df), windowSize)
benchmark(lapplyApproach(df, windows), forApproach(df, windows), order="relative")
#                         test replications elapsed relative user.self sys.self user.child sys.child
#1 lapplyApproach(df, windows)          100  26.386    1.000    26.301    0.004          0         0                                                                          
#2    forApproach(df, windows)          100  26.932    1.021    26.838    0.000          0         0

编辑:对于时间序列,您可以使用包 xts 及其函数 endpointsperiod.applyapply.daily, ...

关于r - 计算滚动窗口协方差矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12409821/

相关文章:

r - 通过不同变量的不同函数创建多个列

r - ggvis 和 data.table 冲突?

r - 在 R 中增量绘图而不是重置

将 NA 替换为 r 中每个组的同一列另一行中的值

r - 使用bca非参数方法进行引导的最小数量是多少

r - 在 R 中按日期绘制箱线图

对不规则时间序列进行定期分析

r - 使用 ts 对象或 xts 对象的 ccf 提供不同的滞后

r - 如何更有效地重新格式化 R 中的 xts 数据?

r - 性能分析错误 na.omit.xts(x) : unsupported type 中的错误