Skip to content
All library documents

Computing Rolling Covariance Matrices in MATLAB

Article Quant Q&A · Author: Marco Wi

Summary

The document shows how to calculate covariance matrices from successive blocks of asset returns in MATLAB and store the results in one stacked array. It gives two approaches: an overlapping window that advances one observation at a time, and a non-overlapping window that advances by the full window length. Both approaches use the dimensions of the return matrix to allocate storage and use a loop to calculate and insert each covariance matrix.

The example uses a window of ten observations and 260 dates, but it does not report performance comparisons or discuss statistical estimation choices. The non-overlapping example discards any trailing observations that do not fill a complete window. The overlapping version produces more estimates, but neighboring matrices share most of their input observations. The code is a basic implementation; users should check indexing and output shape against their own data conventions, especially when choosing how to label or analyze the stacked matrices.

Key ideas

  • Overlapping windows advance one observation at a time and yield more covariance estimates.
  • Non-overlapping windows divide the data into complete blocks of the selected length.
  • Each covariance matrix can be stored as a consecutive block of rows in a larger array.
  • A non-overlapping procedure based on complete windows leaves any incomplete remainder unused.

Tags

Full text
# How to efficiently get covariance matrices from a rolling window in Matlab?


# How to efficiently get covariance matrices from a rolling window in Matlab?












I'am trying to produce a rolling window to estimate a covariance matrix using a for-loop. I have my returns under the variable `returns_sec` and I have 260 observations stored under `N_ret`.

I now want to produce a covariance matrix estimate based on ten return series at a time and obtain one big variable with all covariance matrices in it (Top lines: `Matrix1`, below `Matrix2` and so on).

I could to this by hand by writing:

```
kov_test=cov(returns_sec(1:10,:));  
kov_test2=cov(returns_sec(11:21,:));  ...
```

and copy all results in one variable. But I think there should also be a more effective and easy way using a for-loop.

Would be great if anyone of you could help me out!

## Answer by user18489 (score 1)

https://quant.stackexchange.com/a/30474

I would recommend you to use rolling overlapping window (1st implementation) instead of non-overlapping (your example - 2nd implementation)

```
% overlapping rolling covariance
[n,m] = size(returns_sec); % n-> number of dates, m->number of assets
rolling_window = 10;
cov_test = nan(m*(n - rolling_window + 1),m);
for i = rolling_window:n
    start_index = m*(i - rolling_window) + 1; % aggregate covariance matrix start index
    end_index = m*(i - rolling_window +1); % aggregate covariance matrix end index
    covariance_mtx = cov(returns_sec(i-rolling_window +1:i,:));
    cov_test(start_index:end_index,:) = covariance_mtx;
end

% non-overlapping rolling covariance
[n,m] = size(returns_sec); % n-> numberg of dates, m->number of assets
rolling_window = 10;
num = floor(n/rolling_window); % number of non-overllaping intervals
cov_test = nan(m*num,m);
for i = 1:num
    start_index = m*(i - 1) + 1; % aggregate covariance matrix start index
    end_index = m*i; % aggregate covariance matrix end index
    covariance_mtx = cov(returns_sec(rolling_window*(i - 1) + 1: rolling_window*i,:));
    cov_test(start_index:end_index,:) = covariance_mtx;
end
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.