Welch Method

The Welch Method of Welch (1967) averages the periodograms of overlapping segments of the series, each tapered by a window. It trades frequency resolution for a much lower variance, and the fast Fourier transform of short segments made it quick to calculate.

The formula for the Welch Method first tapers each segment of L values (256 by default) with the Hann window \(w_{t}\), which falls to zero at both ends of the segment, with pi the circle constant:

\[w_{t} = 0.5 - 0.5 \cdot \cos\left(\frac{2 \cdot \pi \cdot t}{L}\right)\]

It then averages the periodograms of the S segments, which overlap by half, each with its own mean bar(x_s) removed and with i the imaginary unit, scaled by the power of the window:

\[P(f_{k}) = \frac{2}{S \cdot \sum_{t=0}^{L - 1} w_{t}^{2}} \cdot \sum_{s=1}^{S} \left\lvert \sum_{t=0}^{L - 1} w_{t} \cdot (x_{t} - \bar{x}_{s}) \cdot \exp\left(-\frac{2 \cdot \pi \cdot i \cdot k \cdot t}{L}\right)\right\rvert^{2}\] \[f_{k} = \frac{k}{L}\]

Averaging S segments divides the variance of the estimate by about S, so peaks that stand out are less likely to be chance; the resolution falls to 1 / L, so cycles longer than a segment cannot be seen and close frequencies merge.

The spectral density splits the variance of a series over frequencies, from slow cycles (a frequency near 0) to the fastest one that can be seen, a cycle of two periods (0.5). A frequency f is a cycle of 1 / f periods: 0.25 on quarterly data is a yearly season, and 0.02 to 0.17 on quarterly data the business cycle of 1.5 to 12 years. Returns without memory have a flat density, as white noise has the same power at every frequency; a peak at a frequency shows a cycle, and a density that rises towards 0 shows persistence. Where the autocorrelation function shows the dependence of a series lag by lag, the spectral density shows it cycle by cycle.

Also known as: Welch’s method, Welch periodogram, averaged periodogram, power spectral density.

In the Finance Toolkit this is get_spectral_density with method="welch", the same function as Spectral Density.

Example · AAPL, MSFT · 2016 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_spectral_density:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2016-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_spectral_density(method="welch", period="weekly")

Which returns:

Using Your Own Data

get_spectral_density 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_spectral_density()

References

  • Paper Priestley, M.B. (1981). “Spectral Analysis and Time Series.” Academic Press.
  • Paper Welch, P.D. (1967). “The use of fast Fourier transform for the estimation of power spectra: A method based on time averaging over short, modified periodograms.” IEEE Transactions on Audio and Electroacoustics, 15(2), 70-73.
  • Library Referenced against scipy’s signal.welch (Welch Method)
Share