Coherence
Calculate the coherence and phase of every series with another: how strongly the two move together at each cycle length, a correlation per frequency, and which one leads.
Two series can be unrelated from day to day and still share their slow cycles, or the other way around; the coherence separates the two. It is near 1 at the frequencies where one series is a linear filter of the other (the same cycle, possibly shifted) and near 0 where they are unrelated. The phase is the shift of the shared cycle: a negative phase at frequency f means the other series lags by -Phase / (\(2 \pi f\)) periods.
The formula is as follows:
\[S_{\text{xy}}(f_{k}) = \frac{2}{S \cdot \sum w_{t}^{2}} \cdot \sum_{s=1}^{S} \text{Conj}(X_{s}(f_{k})) \cdot Y_{s}(f_{k})\] \[\text{Coherence}(f) = \frac{\left\lvert S_{\text{xy}}(f)\right\rvert^{2}}{S_{\text{xx}}(f) \cdot S_{\text{yy}}(f)}\] \[\text{Phase}(f) = \text{Arg}(S_{\text{xy}}(f))\]with \(S_{\text{xx}}\), \(S_{\text{yy}}\) and \(S_{\text{xy}}\) Welch’s (1967) estimates of the spectral densities of x (the series, column, Return by default) and y (the series it is compared with) and of their cross-spectrum, over the S segments of L values (segment_length) that overlap by half, each with its mean removed and tapered by a Hann window \(w_{t}\); Conj is the complex conjugate. The coherence lies between 0 and 1 and the phase, the angle Arg of the cross-spectrum, is in radians between -pi and pi.
A single segment always gives a coherence of 1, so the default segment is 256 values or, for a shorter sample, half of it (three segments).
Also known as: spectral coherence, coherency, squared coherence, cross-spectrum, cross-spectral analysis, phase spectrum, lead-lag by frequency.
Example · AAPL, MSFT · 2016 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_coherence:
from financetoolkit import Toolkit
toolkit = Toolkit(
["AAPL", "MSFT"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2016-01-01",
end_date="2025-12-31",
)
coherence = toolkit.econometrics.get_coherence(period="weekly", segment_length=52)
coherence["AAPL"]
Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:
How strongly did the weekly returns of Apple move with the S&P 500 ETF at the five longest cycles, and which one led, with one-year segments on data from 2016 to 2025?
Which returns:
| Frequency | Coherence | Phase |
|---|---|---|
| 0.0 | 0.5547 | 0.0000 |
| 0.019230769230769232 | 0.5225 | 0.2078 |
| 0.038461538461538464 | 0.4737 | 0.2658 |
| 0.057692307692307696 | 0.6197 | 0.0298 |
| 0.07692307692307693 | 0.5831 | -0.0916 |
| 0.09615384615384616 | 0.4396 | 0.1408 |
| 0.11538461538461539 | 0.3428 | 0.2935 |
| 0.1346153846153846 | 0.0969 | -0.1721 |
| 0.15384615384615385 | 0.4142 | -0.5251 |
| 0.17307692307692307 | 0.5099 | -0.5104 |
| 0.19230769230769232 | 0.6129 | -0.0390 |
| 0.21153846153846154 | 0.5225 | 0.0207 |
| 0.23076923076923078 | 0.5023 | -0.1840 |
| 0.25 | 0.4250 | 0.1525 |
| 0.2692307692307692 | 0.4281 | 0.1428 |
| 0.28846153846153844 | 0.4603 | -0.0502 |
| 0.3076923076923077 | 0.4671 | 0.1400 |
| 0.3269230769230769 | 0.5759 | 0.1169 |
| 0.34615384615384615 | 0.5188 | 0.1382 |
| 0.36538461538461536 | 0.4214 | -0.0166 |
| 0.38461538461538464 | 0.3800 | 0.0579 |
| 0.40384615384615385 | 0.4819 | 0.1391 |
| 0.4230769230769231 | 0.5830 | -0.0320 |
| 0.4423076923076923 | 0.5148 | 0.0444 |
| 0.46153846153846156 | 0.5684 | -0.0781 |
| 0.4807692307692308 | 0.5597 | -0.2852 |
| 0.5 | 0.6997 | 0.0000 |
27 rows
Using Your Own Data
get_coherence runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:
from financetoolkit import Economics, Econometrics
economics = Economics(start_date="2000-01-01")
unemployment = economics.get_unemployment_rate(
countries=["United States", "Germany"], period="monthly"
)
econometrics = Econometrics(data=unemployment, changes="difference")
econometrics.get_coherence()
Parameters
get_coherence accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column. Defaults to “Return”. | "Return" |
against | str | None | The series to compare every other series with, a ticker of the Toolkit or a series of the data. Defaults to None, which takes the Benchmark, or the second series when there is no Benchmark. | None, which takes the Benchmark, or the second series when there is no Benchmark |
segment_length | int | None | The values per segment of the Welch estimates. Defaults to None, which takes 256 (or half of a shorter sample). | None, which takes 256 (or half of a shorter sample) |
include_benchmark | bool | Whether to include “Benchmark” among the assets compared. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
References
- Paper Granger, C.W.J. and Hatanaka, M. (1964). “Spectral Analysis of Economic Time Series.” Princeton University Press.
- Paper Priestley, M.B. (1981). “Spectral Analysis and Time Series.” Academic Press.
- Library Referenced against scipy’s signal.coherence
- Library Referenced against scipy’s signal.csd (phase)