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:
| Frequency | AAPL | MSFT |
|---|---|---|
| 0.0 | 0.0005 | 0.0000 |
| 0.00390625 | 0.0024 | 0.0011 |
| 0.0078125 | 0.0012 | 0.0018 |
| 0.01171875 | 0.0017 | 0.0009 |
| 0.015625 | 0.0035 | 0.0012 |
| 0.01953125 | 0.0023 | 0.0007 |
| 0.0234375 | 0.0020 | 0.0016 |
| 0.02734375 | 0.0068 | 0.0023 |
| 0.03125 | 0.0058 | 0.0015 |
| 0.03515625 | 0.0044 | 0.0007 |
| 0.0390625 | 0.0049 | 0.0011 |
| 0.04296875 | 0.0034 | 0.0006 |
| 0.046875 | 0.0026 | 0.0007 |
| 0.05078125 | 0.0012 | 0.0016 |
| 0.0546875 | 0.0007 | 0.0008 |
| 0.05859375 | 0.0051 | 0.0021 |
| 0.0625 | 0.0068 | 0.0026 |
| 0.06640625 | 0.0024 | 0.0006 |
| 0.0703125 | 0.0042 | 0.0010 |
| 0.07421875 | 0.0032 | 0.0023 |
| 0.078125 | 0.0056 | 0.0023 |
| 0.08203125 | 0.0046 | 0.0015 |
| 0.0859375 | 0.0034 | 0.0004 |
| 0.08984375 | 0.0046 | 0.0012 |
| 0.09375 | 0.0042 | 0.0031 |
| 0.09765625 | 0.0017 | 0.0043 |
| 0.1015625 | 0.0011 | 0.0023 |
| 0.10546875 | 0.0020 | 0.0007 |
| 0.109375 | 0.0014 | 0.0021 |
| 0.11328125 | 0.0030 | 0.0030 |
| 0.1171875 | 0.0063 | 0.0051 |
| 0.12109375 | 0.0039 | 0.0034 |
| 0.125 | 0.0022 | 0.0021 |
| 0.12890625 | 0.0022 | 0.0016 |
| 0.1328125 | 0.0019 | 0.0028 |
| 0.13671875 | 0.0024 | 0.0029 |
| 0.140625 | 0.0004 | 0.0002 |
| 0.14453125 | 0.0014 | 0.0014 |
| 0.1484375 | 0.0016 | 0.0012 |
| 0.15234375 | 0.0006 | 0.0025 |
| 0.15625 | 0.0013 | 0.0035 |
| 0.16015625 | 0.0026 | 0.0020 |
| 0.1640625 | 0.0032 | 0.0014 |
| 0.16796875 | 0.0017 | 0.0013 |
| 0.171875 | 0.0015 | 0.0011 |
| 0.17578125 | 0.0025 | 0.0022 |
| 0.1796875 | 0.0022 | 0.0030 |
| 0.18359375 | 0.0026 | 0.0015 |
| 0.1875 | 0.0034 | 0.0018 |
| 0.19140625 | 0.0017 | 0.0015 |
| 0.1953125 | 0.0045 | 0.0020 |
| 0.19921875 | 0.0081 | 0.0025 |
| 0.203125 | 0.0021 | 0.0004 |
| 0.20703125 | 0.0008 | 0.0012 |
| 0.2109375 | 0.0011 | 0.0016 |
| 0.21484375 | 0.0010 | 0.0030 |
| 0.21875 | 0.0007 | 0.0017 |
| 0.22265625 | 0.0015 | 0.0012 |
| 0.2265625 | 0.0040 | 0.0011 |
| 0.23046875 | 0.0059 | 0.0013 |
| 0.234375 | 0.0038 | 0.0030 |
| 0.23828125 | 0.0009 | 0.0010 |
| 0.2421875 | 0.0035 | 0.0019 |
| 0.24609375 | 0.0041 | 0.0006 |
| 0.25 | 0.0034 | 0.0021 |
| 0.25390625 | 0.0018 | 0.0026 |
| 0.2578125 | 0.0015 | 0.0003 |
| 0.26171875 | 0.0032 | 0.0018 |
| 0.265625 | 0.0024 | 0.0012 |
| 0.26953125 | 0.0017 | 0.0015 |
| 0.2734375 | 0.0003 | 0.0029 |
| 0.27734375 | 0.0025 | 0.0020 |
| 0.28125 | 0.0092 | 0.0033 |
| 0.28515625 | 0.0068 | 0.0031 |
| 0.2890625 | 0.0032 | 0.0037 |
| 0.29296875 | 0.0046 | 0.0047 |
| 0.296875 | 0.0021 | 0.0030 |
| 0.30078125 | 0.0011 | 0.0013 |
| 0.3046875 | 0.0011 | 0.0008 |
| 0.30859375 | 0.0022 | 0.0019 |
| 0.3125 | 0.0013 | 0.0024 |
| 0.31640625 | 0.0027 | 0.0018 |
| 0.3203125 | 0.0035 | 0.0030 |
| 0.32421875 | 0.0018 | 0.0068 |
| 0.328125 | 0.0047 | 0.0071 |
| 0.33203125 | 0.0042 | 0.0028 |
| 0.3359375 | 0.0084 | 0.0042 |
| 0.33984375 | 0.0044 | 0.0044 |
| 0.34375 | 0.0021 | 0.0029 |
| 0.34765625 | 0.0029 | 0.0020 |
| 0.3515625 | 0.0055 | 0.0052 |
| 0.35546875 | 0.0033 | 0.0026 |
| 0.359375 | 0.0011 | 0.0013 |
| 0.36328125 | 0.0016 | 0.0023 |
| 0.3671875 | 0.0030 | 0.0003 |
| 0.37109375 | 0.0065 | 0.0044 |
| 0.375 | 0.0042 | 0.0045 |
| 0.37890625 | 0.0011 | 0.0016 |
| 0.3828125 | 0.0038 | 0.0023 |
| 0.38671875 | 0.0036 | 0.0029 |
| 0.390625 | 0.0031 | 0.0018 |
| 0.39453125 | 0.0036 | 0.0025 |
| 0.3984375 | 0.0025 | 0.0021 |
| 0.40234375 | 0.0030 | 0.0031 |
| 0.40625 | 0.0034 | 0.0034 |
| 0.41015625 | 0.0012 | 0.0017 |
| 0.4140625 | 0.0024 | 0.0008 |
| 0.41796875 | 0.0012 | 0.0018 |
| 0.421875 | 0.0013 | 0.0016 |
| 0.42578125 | 0.0019 | 0.0019 |
| 0.4296875 | 0.0012 | 0.0011 |
| 0.43359375 | 0.0048 | 0.0011 |
| 0.4375 | 0.0034 | 0.0023 |
| 0.44140625 | 0.0021 | 0.0027 |
| 0.4453125 | 0.0050 | 0.0036 |
| 0.44921875 | 0.0020 | 0.0032 |
| 0.453125 | 0.0025 | 0.0034 |
| 0.45703125 | 0.0062 | 0.0040 |
| 0.4609375 | 0.0064 | 0.0050 |
| 0.46484375 | 0.0054 | 0.0025 |
| 0.46875 | 0.0039 | 0.0021 |
| 0.47265625 | 0.0022 | 0.0026 |
| 0.4765625 | 0.0031 | 0.0024 |
| 0.48046875 | 0.0028 | 0.0018 |
| 0.484375 | 0.0023 | 0.0014 |
| 0.48828125 | 0.0019 | 0.0011 |
| 0.4921875 | 0.0025 | 0.0020 |
| 0.49609375 | 0.0026 | 0.0013 |
| 0.5 | 0.0016 | 0.0004 |
129 rows
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.
| Parameter | Type | Description | Example |
|---|---|---|---|
method | str | The estimator. Options are: “periodogram” (periodogram), “welch” (Welch’s averaged periodogram). Defaults to “periodogram”. | "periodogram" |
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" |
segment_length | int | None | The 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_benchmark | bool | Whether to include “Benchmark” among the assets. 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 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)