Fractional Difference

Calculate the fractional difference of the log prices per asset: a stationary series that keeps most of the memory of the prices, (1 - L)^d with a d between 0 and 1.

A price series has a unit root, so models that need a stationary input usually take returns, the first difference (\(d = 1\)), which throws away the memory of the series: the return of today says nothing about where the price stands. López de Prado (2018) shows that a fractional d, often between 0.2 and 0.6 for prices, already gives a stationary series that keeps most of that memory (a correlation with the prices well above that of the returns). Find the smallest such d with get_minimum_fractional_difference.

The formula is as follows, with \(X_{t}\) the log of the column (Adj Close by default):

\[\omega_{0} = 1,\;\; \omega_{k} = - \frac{\omega_{k-1} \cdot (d - k + 1)}{k},\;\; \text{the binomial weights of} \;(1 - L)^{d(\text{Hosking},\; 1981)}\] \[\text{Fixed-width window (FFD):}\;\; \tilde{X}_{t} = \sum_{k=0}^{l*} \omega_{k} \cdot X_{t-k},\;\; \text{with } l \text{* the last lag whose weight is at least the threshold (1e-5 by default) in absolute value, so every date has the same weights}\] \[\text{Expanding window:}\;\; \tilde{X}_{t} = \sum_{k=0}^{t} \omega_{k} \cdot X_{t-k} \;\text{over the whole history up to}\; t,\;\; \text{kept from the first date at which the share of the weight that falls before the start of the series},\;\; \frac{\sum(j > t)\left|\omega_{j}\right|}{\sum_{j=0}^{n-1}\left|\omega_{j}\right|},\;\; \text{is at most the threshold (0.01 by default)}\] \[d = 0 \;\text{returns the}\; \log \;\text{prices},\;\; d = 1 \;\text{the}\; \log \;\text{returns}\]

Also known as: fractional differentiation, fractionally differenced prices, FFD, fixed-width window fracdiff, memory-preserving stationarity transform.

Example · AAPL, MSFT · 2016 to 2025

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

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_fractional_difference(d=0.4)

Which returns:

Using Your Own Data

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

Parameters

get_fractional_difference accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column to differentiate. Defaults to “Adj Close”."Return"
dfloatThe order of differentiation, at least 0. Defaults to 0.4.1
methodLiteral["fixed_width", "expanding"]The window. Options are: “fixed_width” (fixed-width window, FFD), “expanding” (expanding window). Defaults to “fixed_width”."fixed_width"
thresholdfloat | NoneThe smallest absolute weight kept by the fixed-width window, or the largest share of weight left out by the expanding window. Defaults to None, which is 1e-5 for “fixed_width” and 0.01 for “expanding”.
log_pricesboolWhether to take the natural logarithm of the series first. Pass False for a series that can be zero or negative, such as a rate. 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 López de Prado, M. (2018). “Advances in Financial Machine Learning.” Wiley, chapter 5 (Snippets 5.1 to 5.3).
  • Paper Hosking, J.R.M. (1981). “Fractional Differencing.” Biometrika, 68(1), 165-176.
  • Library Referenced against RiskLabAI’s fractional_difference_std
  • Library Referenced against RiskLabAI’s fractional_difference_fixed
Share