Calculating Rolling Pairwise Correlations Across Equity Returns in R
Summary
This tutorial shows how to estimate rolling correlations for every pair of stocks in a universe, then summarize them as a daily mean. It starts by calculating each stock’s daily close-to-close return, joins the return data to itself by date to form ticker pairs, removes self-pairs and duplicate pair orderings, and applies a rolling correlation function to each pair. The example uses tidyverse tools and slider windows on financial-sector ETF constituents.
The displayed output includes sample pair correlations and a chart of average pairwise correlation over time. The method illustrates how to construct pair-level time series from long-format data and aggregate them into a broad measure of co-movement. It is a data-processing example rather than a trading strategy: it provides no interpretation of the plotted history, validation of the signals, or guidance on window selection. The stated period is 60, and the code’s window setting counts observations with `.before` set to that period, so users should check whether the resulting window length matches their intent.
Key ideas
- Daily close-to-close returns provide the inputs for rolling correlations between stocks.
- A self-join by date creates the full set of same-day return pairs.
- Removing diagonal entries and canonicalizing ticker order avoids self-correlations and duplicate pairings.
- A rolling correlation can be computed separately for each pair and summarized across pairs by date.
- The example produces a mean pairwise correlation series but does not establish a trading edge.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.