Entropy

Calculate the Entropy of a series, by default the returns: how irregular and unpredictable it is, from the sample entropy of Richman and Moorman to the entropies of encoded returns of López de Prado, over the full history or a rolling window.

An entropy is low for a series that repeats itself, trends or is driven by a few cycles, and high for a series that looks like noise. On daily returns, which are close to noise, most entropies sit near their maximum, so they are mainly of use to compare assets or periods with each other (e.g. with rolling for a rolling value) rather than as an absolute level.

The methods are as follows (with the default order \(m = 2\) for the sample and approximate entropy and \(n = 3\) for the permutation and SVD entropy):

\[\text{Sample (Richman and Moorman, 2000):}\;\; \text{SampEn} = - \ln\left(\frac{A}{B}\right),\;\; \text{with}\;\; B \;\text{the number of pairs of templates of}\; m \;\text{values within a Chebyshev distance of}\; r = \text{tolerance} \cdot \operatorname{Std}(x) \;\text{of each other} \;(\text{a template not counted with itself}) \;\text{and}\; A \;\text{the same for}\; m + 1 \;\text{values}\] \[\text{Approximate (Pincus, 1991):}\;\; \text{ApEn} = \Phi(m) - \Phi(m + 1),\;\; \Phi(m) = \operatorname{Mean}(\ln C_{i}(m)),\;\; C_{i} (m) \text{ the share of templates within } r \text{ of template } i \text{, itself included}\] \[\text{Permutation (Bandt and Pompe, 2002):}\;\; H = - \operatorname{Sum}(p(\pi) \cdot \operatorname{log2}(p(\pi))) \;\text{over the ordinal patterns}\; \pi \;\text{of}\; n \;\text{values} \;\text{delay} \;\text{apart},\;\; \text{divided by log2(} n \text{!) when normalized}\] \[\text{Spectral (Inouye et al., 1991):}\;\; H = - \operatorname{Sum}(p(f) \cdot \operatorname{log2}(p(f))),\;\; p \text{(} f \text{) the power spectrum (the periodogram or Welch's estimate) as shares of the total, divided by log2 of the number of frequencies when normalized}\] \[\text{SVD (Roberts, Penny and Rezek, 1999):}\;\; H = - \operatorname{Sum}(s_{k} \cdot \operatorname{log2}(s_{k})),\;\; s_{k} \text{ the singular values of the delay embedding of } n \text{ values as shares of their sum, divided by log2(} n \text{) when normalized}\] \[\text{Shannon:}\;\; \text{the plug-in estimate} - \frac{\operatorname{Sum}(p(w) \cdot \operatorname{log2}(p(w)))}{L} \;\text{over the words}\; w \;\text{of}\; L \;\text{symbols} \;(\text{word\_length}) \;\text{of the encoded returns},\;\; \text{in bits per symbol}\]
  • Lempel-Ziv: the number of distinct phrases of the Lempel-Ziv (1978) parsing of the encoded returns divided by their number.
\[\text{Kontoyiannis (Kontoyiannis et al., 1998):}\;\; \operatorname{Mean}\left(\frac{\operatorname{log2}(i + 1)}{L_{i}}\right) \;\text{over the first half of the encoded returns},\;\; L_{i} \text{ one more than the longest match of the symbols from position } i \text{ with the } i \text{ symbols before it, in bits per symbol}\] \[\text{Share (Shannon, 1948):}\;\; H = - \operatorname{Sum}(P \cdot \operatorname{log\_base}(P)),\;\; P = \frac{x}{\operatorname{Sum}(x) \;\text{the values} \;(\text{by default the prices}) \;\text{as shares of their sum}},\;\; \text{divided by ln(} n \text{) of the } n \text{ values when normalized}.\;\; \text{It is at its maximum, log(} n \text{), when all values are equal and falls as they spread out, so for prices a lower entropy points to a larger move}\]

The Shannon, Lempel-Ziv and Kontoyiannis entropy encode the returns into symbols first: binary (1 for a rise, 0 for a fall, returns of 0 left out), by quantile (the decile of the return with 10 bins) or by sigma (the bucket of one standard deviation the return falls in).

Also known as: SampEn, ApEn, ordinal pattern entropy, spectral entropy, SVD entropy, plug-in entropy, LZ entropy, Kontoyiannis entropy, entropy features, ENTP.

Example · AAPL, MSFT, NVDA · 2021 to 2025

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

from financetoolkit import Toolkit

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

toolkit.econometrics.get_entropy(method="permutation", rolling=252)

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

How random were the daily returns of Apple, Microsoft and Nvidia over the last five trading days of 2025, measured by their permutation entropy over a rolling 252-day window?

Which returns:

Using Your Own Data

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

Parameters

get_entropy accepts the following parameters.

ParameterTypeDescriptionExample
methodstrThe entropy. Options are: “sample” (Sample Entropy), “approximate” (Approximate Entropy), “permutation” (Permutation Entropy), “spectral” (Spectral Entropy), “svd” (SVD Entropy), “shannon” (Shannon entropy rate), “lempel_ziv” (Lempel-Ziv entropy), “kontoyiannis” (Kontoyiannis entropy), “share” (Shannon entropy of the values as shares of their sum). Defaults to “sample”."sample"
periodstr | NoneThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to None, which uses “daily”."weekly"
columnstr | NoneThe historical data column the entropy is calculated on. Options are: “Return” (the returns, the changes of the series), “Adj Close” (the prices, the series as given with Econometrics(data=...)), “Close” (the traded prices). Defaults to None, which uses “Adj Close” for the share entropy, whose values must be positive, and “Return” for the others."Return"
rollingint | NoneThe number of periods of a rolling window, at least the shortest window with a value: order + 2 for the sample and approximate entropy (4 by default), (order - 1) * delay + 1 for the permutation and SVD entropy (3 by default), word_length for the Shannon entropy and 2 otherwise. Defaults to None, which calculates one value over the full history of every asset.4
orderint | NoneThe template length m of the sample and approximate entropy or the number of values n of the permutation and SVD entropy. Defaults to None, which uses 2 for the sample and approximate and 3 for the permutation and SVD entropy.2
delayintThe number of periods between the values of a pattern of the permutation and SVD entropy. Defaults to 1.1
tolerancefloatThe tolerance r of the sample and approximate entropy as a multiple of the standard deviation (ddof=1). Defaults to 0.2.1.0e-06
normalizeboolWhether to divide the permutation, spectral, SVD and share entropy by their maximum, so they lie between 0 and 1. Defaults to True.True
basefloatThe base of the logarithm of the share entropy when it is not normalized. Defaults to 2.0, in bits.
estimatorstrThe power spectrum of the spectral entropy. Options are: “periodogram” (the periodogram), “welch” (segments of 256 periods with a Hann window). Defaults to “periodogram”."parkinson"
encodingstrThe encoding of the Shannon, Lempel-Ziv and Kontoyiannis entropy. Options are: “binary” (the direction), “quantile” (the quantile of the return), “sigma” (the standard deviation bucket of the return). Defaults to “binary”."binary"
binsintThe number of quantiles of the quantile encoding. Defaults to 10.10
word_lengthintThe number of symbols per word of the Shannon entropy. Defaults to 1.1
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, which uses the rounding of the Toolkit (4 unless set otherwise).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 Richman, J.S. & Moorman, J.R. (2000). “Physiological time-series analysis using approximate entropy and sample entropy.” American Journal of Physiology - Heart and Circulatory Physiology, 278(6), H2039-H2049.
  • Paper Pincus, S.M. (1991). “Approximate entropy as a measure of system complexity.” Proceedings of the National Academy of Sciences, 88(6), 2297-2301.
  • Paper Bandt, C. & Pompe, B. (2002). “Permutation entropy: a natural complexity measure for time series.” Physical Review Letters, 88(17), 174102.
  • Paper Inouye, T. et al. (1991). “Quantification of EEG irregularity by use of the entropy of the power spectrum.” Electroencephalography and Clinical Neurophysiology, 79(3), 204-210.
  • Paper Roberts, S.J., Penny, W. & Rezek, I. (1999). “Temporal and spatial complexity measures for electroencephalogram based brain-computer interfacing.” Medical & Biological Engineering & Computing, 37(1), 93-98.
  • Paper Kontoyiannis, I., Algoet, P.H., Suhov, Y.M. & Wyner, A.J. (1998). “Nonparametric entropy estimation for stationary processes and random fields, with applications to English text.” IEEE Transactions on Information Theory, 44(3), 1319-1327.
  • Paper López de Prado, M. (2018). “Advances in Financial Machine Learning.” Wiley, chapter 18.
  • Library Referenced against nolds’ sampen
  • Library Referenced against antropy’s sample_entropy
  • Library Referenced against antropy’s app_entropy
  • Library Referenced against antropy’s perm_entropy
  • Library Referenced against antropy’s spectral_entropy
  • Library Referenced against antropy’s svd_entropy
  • Library Referenced against pandas-ta-classic’s entropy (share)
  • Library Shares similar logic as RiskLabAI’s plug_in_entropy_estimator
  • Library Shares similar logic as RiskLabAI’s lempel_ziv_entropy
  • Library Shares similar logic as RiskLabAI’s kontoyiannis_entropy
Share