Correlation Coefficient
systemic:core-corcMathInputs
| Name | Type | Description |
|---|---|---|
| Window | Whole number | How many recent pairs each coefficient uses, as a fixed whole number; 0 uses every pair so far. |
| First Dataset | Number series | The first numeric series to compare. |
| Second Dataset | Number series | The numeric series to compare with **First Dataset**. |
Outputs
| Name | Type | Description |
|---|---|---|
| Coefficient | Number series | Pearson correlation of the two inputs, from **−1 to +1**, with no unit. |
Cost
0.198 + 0.00598·n credits for n bars, rounded up. The marks are each plan's limit of bars.
Imagine holding shares in two different countries because their markets usually do not move in lockstep. During a difficult stretch, both begin falling together. Has the relationship changed, or are you seeing a few unusual observations? That is a question worth asking before you assume a second holding has diversified the first.
This concern has a research history. In a study of international equity markets, Longin and Solnik found stronger correlation in bearish extremes than in bullish extremes. They also warned that a naive comparison of correlations across volatile and calm periods can mislead. Their finding is about the markets and method they studied, not a rule that every asset pair moves toward +1 in every crisis. Read their original study.
Correlation Coefficient gives you a way to investigate relationships in your own series. It calculates Pearson correlation, a measure of linear association. It can illuminate a diversification idea, reveal a change worth studying, or test whether a pair deserves deeper research. It cannot explain why two series move together or promise that they will keep doing so. Its ordinary rolling or expanding calculation is not the specialized tail analysis used in Longin and Solnik's study.
Read the coefficient
| Coefficient | Meaning for the observations being compared |
|---|---|
| +1 | A perfect upward-sloping straight-line relationship. |
| −1 | A perfect downward-sloping straight-line relationship. |
| Near 0 | Little straight-line relationship in this sample. A curved relationship can still be present. |
Between those endpoints, the sign gives the direction and the distance from zero gives the strength of the linear association. A coefficient of zero does not establish that the series are independent. Nor is there a universal number above which a relationship becomes “strong”: ten observations and ten thousand observations call for different levels of confidence.
Correlation is symmetric: switching First Dataset and Second Dataset gives the same coefficient. It also has no unit, so comparing a dollar-denominated price series with a percentage series is mathematically possible. Whether that comparison answers a sensible investing question is up to you.
The mathematics, from the middle outward
For the valid paired readings in a chosen window (W), Pearson correlation is
Start in the numerator. (x_i-\bar{x}) says how far one reading lies from the average of the selected (X) readings; (y_i-\bar{y}) does the same for (Y). Multiplying those departures gives a positive contribution when both readings are on the same side of their respective averages and a negative one when they are on opposite sides. Summing the products describes their joint movement—covariance, before scaling.
The denominator divides by the spread of each series. This turns joint movement into a number between −1 and +1. If either series is flat across the selected pairs, its spread is zero and correlation is undefined, not zero. NIST gives the Pearson formula and its linear interpretation.
Take three paired readings from a series with gaps: ((4,8),(5,10),(6,5)). The average of the first series is 5; the average of the second is (23/3). Their departures are ((-1,0,1)) and ((1/3,7/3,-8/3)). The products add to −3, while the squared departures add to 2 and (38/3), respectively. The coefficient is therefore (-3/\sqrt{2\times38/3}), approximately −0.596040. The first two pairs alone had correlation +1. Adding the third changes the answer because correlation describes the selected sample, not a permanent property of the series.
This also explains why a two-pair reading deserves restraint. Provided neither series is flat, two points always lie on a straight line, so their coefficient is exactly +1 or −1. More pairs let the statistic express how tightly the observations cluster around a line.
Decide what history should matter
Window changes the question. The default is 0.
| Window | Pairs used at a valid bar | What you gain | What you give up |
|---|---|---|---|
| 0 | Every valid pair so far in the run | A growing view of the available history | Older observations continue to influence the latest reading. |
| N ≥ 2 | The most recent N valid numeric pairs | A view that can respond to recent changes | A short sample can swing sharply and look more conclusive than it is. |
With Window = 0, the first coefficient appears only after two valid pairs. With a rolling window, the first appears after the full (N) pairs. Window = 1 produces no defined coefficient: one pair cannot show how two series vary together.
The module sees only the bars of the run. Window 0 cannot use observations from before the run starts, and a rolling window needs enough usable pairs within the run.
Consider six valid pairs ((1,2),(2,4),(3,6),(4,8),(5,10),(6,5)), with two empty bars before the last pair. The first five line up perfectly; the sixth breaks that pattern. At the sixth valid pair, an expanding window includes all six pairs and reads approximately +0.654654. A rolling window of three uses only pairs 4–6 and reads approximately −0.596040. They answer different questions about the same data. A sharp change in a short rolling reading may deserve investigation; it is not, by itself, proof of a new market regime.
flowchart LR
P1["Pairs 1–3"] --> P2["Pair 4"] --> P3["Pair 5"] --> G["Two gaps"] --> P4["Pair 6"]
P1 -. retained .-> E["Expanding: pairs 1–6"]
P4 --> E
P2 -. retained .-> R["Rolling 3: pairs 4–6"]
P4 --> R
The diagram shows which pairs enter each reading; it does not plot market data. In a rolling calculation, empty bars do not use up a place in the window. “20 pairs” can therefore cover more than 20 bars.
Make the data comparable
For a portfolio-risk question, correlate returns over matching intervals. Prices and returns tell different stories: two prices can rise together over a long span even when their day-to-day changes do not line up. Likewise, comparing a daily return with an hourly reading without examining how the two were aligned can give the coefficient a meaning you did not intend.
Systemic aligns connected series on a shared timeline and may carry a slower series' latest available reading across faster bars. Those repeated readings are actual inputs to this calculation. Look at the connected series and their periods before giving the result an economic interpretation. Equal numbers of bars do not, on their own, guarantee comparable observations.
Ask the same question at more than one period
An hourly correlation, a daily correlation, and a weekly correlation can differ because each groups market movement into different intervals. Consider two assets that respond to the same monthly news but trade at different times during the day: short-interval readings may fail to line up even if longer-interval returns do. The reverse is possible too. No period is the correct one; choose one that matches the decision you are researching.
| Research horizon | A useful question | Check before interpreting |
|---|---|---|
| Short intervals | Do the assets' immediate returns line up? | Trading hours, stale quotes, and any repeated values. |
| Daily | Did they tend to move together from one session to the next? | Whether both returns cover the same sessions. |
| Weekly or monthly | Does the relationship persist after short swings are grouped? | The smaller number of paired observations and the dates used to form each return. |
Changing the period also changes the sample size. A 20-pair window at one-hour intervals and a 20-pair window at one-month intervals represent very different stretches of history. If the series include gaps, even the period does not tell you exactly how much time a rolling window covers. Inspect dates, not just Window.
Gaps are not zeros
A gap is a bar where an input has no usable number: the value is empty. A gap never enters Pearson's calculation. If either side is empty at the current bar, the coefficient there is empty; the next valid pair resumes the calculation. A numeric zero is a reading and does count.
This arithmetic example is deliberately small; these are not asset prices. Set Window = 2.
| Bar | First Dataset | Second Dataset | Coefficient | Why |
|---|---|---|---|---|
| 1 | 0 | 0 | empty | Only one valid pair exists. |
| 2 | 1 | 2 | +1 | The first two pairs rise together. |
| 3 | empty | 4 | empty | A bar with a missing value supplies no pair. |
| 4 | 2 | 4 | +1 | The window now contains pairs 2 and 4. |
| 5 | 3 | 6 | +1 | The window now contains pairs 4 and 5. |
The zeros at bar 1 are part of the first coefficient. And an empty coefficient is not a measured zero correlation: it may mean a missing pair, too little history, or a flat series.
Put correlation to work
1. Test a diversification idea
Imagine holding one asset and considering a second. A chart of their returns alongside a rolling correlation can show whether they have historically tended to move together over your chosen horizon. Then compare that recent view with an expanding one. If the readings disagree, inspect the periods that each includes before changing a portfolio.
The connection to risk is precise. For a portfolio of two assets, return variance is
(w_A,w_B) are portfolio weights; (\sigma_A,\sigma_B) are the assets' return standard deviations. The last term is their joint contribution. With positive weights in both assets, holding weights and individual volatilities fixed, a lower correlation lowers this calculated variance. A short position changes the sign of the weight product, so the same conclusion does not apply unchanged. The coefficient alone says nothing about expected returns, liquidity, or future relationships. Markowitz's original portfolio-selection paper develops the variance-and-covariance framework.
There is a subtle point here: correlation is only one ingredient in the variance equation. Even an asset with a low historical correlation can contribute substantial risk if its own returns are very volatile or its weight is large. Conversely, a small allocation may have little effect on the whole portfolio. CFA Institute's portfolio-risk reading treats weights, individual variation, and correlation together.
Try this: For two holdings you actually track, compare rolling correlations of their daily returns over several window lengths. Do the apparent diversification benefits survive a different sample period? What happens to estimated portfolio variance if you keep the volatilities and weights fixed but substitute a different observed correlation?
2. Investigate a changing relationship
Connect the same two return series to two Correlation Coefficient modules: one with Window = 0, the other with a rolling window that fits your question. Plot both. A widening difference is a cue to examine the underlying dates and values. Did one series have gaps? Did its period change? Was one series unusually flat? Are a few outlying observations driving the short-window result?
This is a research tool rather than an automatic regime detector. The expanding view can be slow to reflect recent observations, while a short rolling view can jump because a single pair entered or left the sample. Vary the window and the date range before treating a movement as meaningful.
Try this: Compare two sector return series across two nonoverlapping periods. First, plot both return series and mark empty bars. Next, compare rolling readings under two window lengths and note the dates at which a particular pair enters or leaves each window. Finally, calculate a reading on a later period that did not inform your choice of window. Does a relationship seen in one period also appear in the other? If not, which observations account for the difference?
3. Research a possible pair without assuming a trade
Two assets can have highly correlated returns yet offer no stable relationship between their price levels. Even two price paths with a high historical correlation need not have a spread that returns toward a typical level after they part. Correlation asks whether movements line up linearly in a selected sample; cointegration asks a different, longer-run question about a combination of certain nonstationary series. One statistic cannot answer both. Engle and Granger's original paper develops the cointegration and error-correction framework.
flowchart LR
C["Correlation: do selected readings move together?"] --> S["Separate question: does a chosen spread return toward a level?"]
S --> V["Separate question: does that behavior persist in later data?"]
The arrows connect research questions, not automatic trading steps. This module answers the first question only. A persistent price spread would need its own definition, evidence, and stability checks; correlation does not test it. Execution costs, liquidity, and structural change remain relevant even if a relationship once appeared stable.
Try this: Pick two related assets and inspect their aligned returns before calculating correlation. If they move together, plot their price levels and a thoughtfully defined difference between them over two separate periods. Does that difference repeatedly return toward a level, or does it drift? Treat a high coefficient as a reason to ask the question, never as the answer.
Keep an edge-case reference nearby
| Situation at the current bar | Coefficient | What to look at next |
|---|---|---|
| One valid pair so far | empty | At least two pairs are needed; a rolling window also needs its full pair count. |
| Selected sample has exactly two non-flat pairs | +1 or −1 | Treat the perfect-looking result as a very small sample. |
| Either input is empty | empty | Inspect the source bars; the gap does not erase earlier valid pairs. |
| Either selected series is flat | empty | Its zero variation leaves no defined Pearson coefficient. |
| Either input contains numeric zero | A valid numeric pair if the other side is numeric | Zero participates like any other number; the coefficient still depends on the selected sample. |
| Window = 1 | empty throughout | Choose at least two pairs for a rolling coefficient. |
| Window = 0 | Expanding result once two valid pairs exist | Earlier pairs remain in the sample. |
Pearson correlation sees straight-line association. It can miss a curved relationship; it can also reflect a common influence acting on both series. A coefficient of +1 does not make two investments interchangeable, and a coefficient near 0 does not make them independent. Estimates change with the period, window length, and the observations selected. None of those limitations disappears because a threshold turns the number into a signal.
Further reading
- NIST: Correlation — the Pearson formula, interpretation, and related statistical measures.
- Harry Markowitz: Portfolio Selection (1952) — the original portfolio variance-and-covariance framework.
- CFA Institute: Portfolio Risk and Return — how weights, individual volatility, and correlation interact in portfolio risk.
- François Longin and Bruno Solnik: Extreme Correlation of International Equity Markets — a careful empirical study of international equity relationships in extreme markets, using methods beyond ordinary Pearson windows.
- Robert Engle and Clive Granger: Co-Integration and Error Correction (1987) — the foundational paper behind a different question about long-run relationships.