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:

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.

ParameterTypeDescriptionExample
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column. Defaults to “Return”."Return"
againststr | NoneThe 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_lengthint | NoneThe 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_benchmarkboolWhether to include “Benchmark” among the assets compared. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
show_columnsstr | list[str] | NoneThe 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)
Share