Skip to content
All library documents

Rolling Exponentially Weighted Covariance with a Fixed Window

Article Quant Q&A · Author: Ringleader

Summary

The document considers estimating a sequence of covariance matrices from asset returns while limiting each estimate to a fixed number of recent observations. The motivating example uses a 20-observation half-life and caps each window at 40 observations, aiming to combine exponential decay with a rolling cutoff.

The response expresses each window’s covariance entry as a weighted sum of products of returns, or equivalently as a matrix product using a diagonal weight matrix. It assumes returns have zero mean and describes processing each successive window with a loop. This gives the mathematical structure of the calculation, but it does not supply an implementation, explain how to choose or normalize the weights, or meet the question’s preference to avoid loops. Nonzero return means would require centering the observations or otherwise adjusting the covariance calculation.

Key ideas

  • A rolling estimate can be formed from a fixed-length window of recent return observations.
  • Exponential weights can be represented by a diagonal matrix applied within each window.
  • Under a zero-mean assumption, each covariance matrix is a weighted sum of return cross-products.
  • The proposed computation advances window by window and does not provide a vectorized implementation.

Tags

Full text
# Calculate Exponentially-Weighted Covariance Matrix over Finite Window


# Calculate Exponentially-Weighted Covariance Matrix over Finite Window












I have an `(n,m)` array (specifically containing asset returns over `n` days for `m` assets). I'm trying to calculate the rolling exponentially-weighted covariance matrix for these assets over this time frame, but I want to limit how much data with which each covariance matrix is calculated.

To be more specific, I'm wanting to calculate these covariance matrices using 20-observation half-lives, but I don't want to include more than 40 observations in each of these calculations.

I've come as far as constructing a pandas DataFrame which has a shape of `(n, m, 40)`, so each value of `n` contains the last 40 observations of the `m` assets. I was thinking I'd be able to calculate a single exponentially-weighted covariance matrix with 20-observation half-life at each `n` using the data in that row , but I'm coming up short. Am I able to calculate it this way or is there a different approach I should take?

Edit: I'm looking to avoid `for` loops in this solution.

## Answer by Kermittfrog (score 1, accepted)

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

If I understand you correctly, you are aiming to compute a series of covariance matrices based on windows of your return data. To this end, let $X$ denote the $n\times k$ matrix of observed returns for $n$ dates and $k$ instruments. Further, you have a window of length $h<n$.

Then, the typical entry $(k,l)$of each component covariance matrix is calculated as (assuming zero mean)

$$ C_i(k,l)=\sum_{t=i}^{h+i-1}w_tx_{k,t}x_{l,t}=X_i^TWX_i $$

where $X_i$ is the $i$th window of the data matrix, i.e.

$$ X_i\equiv \begin{pmatrix} x_{1,i}&x_{2,i}&\ldots&x_{k,i}\\ x_{1,i+1}&x_{2,i+1}&\ldots&x_{k,i+1}\\ \ldots&\ldots&\ldots&\ldots\\ x_{1,i+h-1}&x_{2,i+h-1}&\ldots&x_{k,i+h-1} \end{pmatrix} $$

and $W$ is a diagonal matrix of the weights. The corresponding computation is a simple `for`-loop. HTH?

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.