Spectral Density

Calculate the spectral density of every series: how much of its variance comes from cycles of each length, so a peak marks a cycle in the series.

The spectral density splits the variance of a series over frequencies, from slow cycles (frequency near 0) to the fastest one that can be seen, every other period (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; a peak at a frequency shows a cycle, and a density that rises towards 0 shows persistence (see get_long_memory_test).

The formula for the periodogram (Schuster, 1898) is as follows:

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

for \(k = 0\), …, floor(n / 2), with \(x_{t}\) the column series (Return by default) and i the imaginary unit. The density is one-sided (the frequency 0 and, for an even n, 0.5 are not doubled), so the density summed over the frequencies and divided by n equals the variance (with n in the denominator).

The formula for Welch’s averaged periodogram (Welch, 1967) is as follows:

\[w_{t} = 0.5 - 0.5 \cdot \cos\left(\frac{2 \cdot \pi \cdot t}{L}\right)\] \[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}\]

over the S segments of L values (segment_length, 256 by default) that overlap by half, each with its mean bar(x_s) removed and tapered by the Hann window \(w_{t}\).

The periodogram is unbiased but noisy at every frequency, however long the sample; Welch’s estimate trades frequency resolution (1 / L) for a much lower variance.

Also known as: power spectrum, spectrum, periodogram, Welch, frequency domain, spectral analysis, cycles.

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")

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

What was the Welch spectral density of the weekly returns of Apple and Microsoft from 2016 to 2025 at the five lowest frequencies?

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

Parameters

get_spectral_density accepts the following parameters.

ParameterTypeDescriptionExample
methodstrThe estimator. Options are: “periodogram” (periodogram), “welch” (Welch’s averaged periodogram). Defaults to “periodogram”."periodogram"
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column. Defaults to “Return”."Return"
segment_lengthint | NoneThe values per segment of Welch’s estimate. Defaults to None, which takes 256 (or every value of a shorter series).None, which takes 256 (or every value of a shorter series)
include_benchmarkboolWhether to include “Benchmark” among the assets. 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 Priestley, M.B. (1981). “Spectral Analysis and Time Series.” Academic Press.
  • Paper Schuster, A. (1898). “On the investigation of hidden periodicities with application to a supposed 26 day period of meteorological phenomena.” Terrestrial Magnetism, 3(1), 13-41.
  • 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.periodogram (periodogram)
  • Library Referenced against scipy’s signal.welch (Welch)
Share