Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:47:17+00:00 2026-06-11T10:47:17+00:00

I am trying to compute a rolling window (shifting by 1 day) covariance matrix

  • 0

I am trying to compute a rolling window (shifting by 1 day) covariance matrix for a number of assets.

Say my df looks like this:

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))

I expect the output to look like following :

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

But everything made in a loop because I have a lot of rows in the data set…

  1. How would a possible for loop look like if I want to calculate a covariance matrix on a rolling basis by shifting the rolling window by 1 day? Or should I use some apply family function?

  2. What time series class would be preferrable if I want to create a time series object for the loop above? Now I use as.timeSeries from fPortfolio package.

I simply can’t get it…

Best Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:47:18+00:00Added an answer on June 11, 2026 at 10:47 am

    To create your rolling windows you could use 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
    

    EDIT: for time series you could use package xts and its functions endpoints, period.apply, apply.daily, …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a matrix to compute stuff. The code is this import
I am trying to compute and count the number of rows inside a C#
I'm trying to compute the maximal weight in a heap with this function: unsigned
I'm trying to compute statistics for data held in an Access .mdb database. In
I am trying to compute the covariance between two vectors in matlab: x =
I am trying to compute the cube root of a negative number in VBA
I'm new to NumPy and I'm trying to compute some simple statistics like the
I'm trying to compute this equation: int n = 89; int SUMx = 347762,
I'm trying to compute de Bruijn sequences for alphabets which have a number of
Im trying to compute a 24 hour rms (root mean squared) from a dataset

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.