Lempel-Ziv Complexity

Calculate the Lempel-Ziv Complexity of a series, by default of the direction of the returns: the number of new patterns needed to write the series down, over the full history or a rolling window.

The returns are encoded into symbols (by default 1 for a rise and 0 for a fall) and parsed from the start into phrases, each the longest stretch that can be copied from earlier in the series plus one new symbol. A series that repeats itself needs few phrases, a random one many. Normalized, the complexity approaches 1 for independent, equally likely symbols and falls towards 0 for a regular series, so on daily returns it is mostly close to 1 and of use to compare assets or periods with each other.

The formula is as follows:

\[c(N) = \text{the number of phrases of the Lempel-Ziv} \;(1976) \;\text{parsing of the}\; N \;\text{symbols}\] \[\text{Normalized} \;c(N) = \frac{c(N) \cdot \log_{k}(N)}{N}\]

with k the number of possible symbols: 2 for the binary encoding, the number of bins for the quantile encoding and the number of distinct symbols for the sigma encoding.

Also known as: LZ complexity, LZ76, Lempel-Ziv-Welch complexity, LZC.

Example · AAPL, MSFT, NVDA · 2021 to 2025

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

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_lempel_ziv_complexity(rolling=252)

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

How complex were the daily returns of Apple, Microsoft and Nvidia over a rolling 252-day window in the last five trading days of 2025, according to the Lempel-Ziv complexity?

Which returns:

Using Your Own Data

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

Parameters

get_lempel_ziv_complexity accepts the following parameters.

ParameterTypeDescriptionExample
periodstr | NoneThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to None, which uses “daily”."weekly"
columnstrThe historical data column that is encoded: “Return” for the returns (the changes of the series), “Adj Close” for the prices (the series as given with Econometrics(data=...)) or “Close” for the traded prices. Defaults to “Return”."Return"
rollingint | NoneThe number of periods of a rolling window, at least 2. Defaults to None, which calculates one value over the full history of every asset.4
encodingstrThe encoding. Options are: “binary” (1 for a positive and 0 for a negative value, values of 0 left out), “quantile” (the quantile of the value), “sigma” (the bucket of one standard deviation the value falls in). Defaults to “binary”."binary"
binsintThe number of quantiles of the quantile encoding. Defaults to 10.10
normalizeboolWhether to normalize the number of phrases by N / log_k(N). Defaults to True.True
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 Lempel, A. & Ziv, J. (1976). “On the complexity of finite sequences.” IEEE Transactions on Information Theory, 22(1), 75-81.
  • Paper Kaspar, F. & Schuster, H.G. (1987). “Easily calculable measure for the complexity of spatiotemporal patterns.” Physical Review A, 36(2), 842-848.
  • Paper López de Prado, M. (2018). “Advances in Financial Machine Learning.” Wiley, chapter 18.
  • Library Referenced against antropy’s lziv_complexity
Share