R 计算滚动窗口协方差矩阵

R 计算滚动窗口协方差矩阵,r,xts,R,Xts,我试图计算一系列资产的滚动窗口(移动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)) 但一切都在循环中,因为我在数据集中有很多行 如果我想通过将滚动窗口移动1天,在滚动的基础上计算协方差矩阵,那么可能的for循环会是什么样子?或者我应该使用一些applyf

我试图计算一系列资产的滚动窗口(移动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))
但一切都在循环中,因为我在数据集中有很多行

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

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

  • 我就是搞不懂


    致以最诚挚的问候

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

    ## 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
    
    ##您的data.frame
    
    df请展示您期望输出的样子。非常感谢!感谢:)关于如何“重命名”计算出的协方差矩阵,你有什么好的解决方案吗。例如,不是像“2011-01-01到2011 02-30”这样的$
    3291
    ?最好的regards@user1665355如果问题解决了,请将问题标记为已回答。对于如何创建滚动窗口,而不是提前一天滚动,而是在间隔内滚动,您是否有任何清晰的建议。比如说,60天的数据用于创建协方差矩阵。然后,在60天后计算下一个cov.矩阵。再次感谢:)@user1665355增加
    windowSize
    ?!
    ## 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