Detrended Fluctuation Analysis
The Detrended Fluctuation Analysis (DFA) of Peng et al. (1994), introduced for the long-range correlations in DNA sequences, estimates the Hurst exponent from how the fluctuations of the cumulative returns around a local trend grow with the window length. Removing the trend in every window keeps slow drifts in the level from passing for long memory.
The formula for the Detrended Fluctuation Analysis starts from the profile of the N log returns x, the cumulative sum of their deviations from the mean, for \(k = 1\), …, N:
\[Y_{k} = \sum_{i=1}^{k} \left(x_{i} - \operatorname{mean}\left(x\right)\right)\]The profile is cut into windows of n periods, and in every window a polynomial trend \(Y_{n} (k)\) (a straight line by default) is fitted by least squares. The fluctuation F(n) is the root mean square of the profile around those trends, the mean running over the points of all windows:
\[F(n) = \sqrt{\operatorname{mean}\left((Y_{k} - Y_{n}(k))^{2}\right)}\]The exponent H, the scaling exponent alpha of the paper, is the slope of the logarithm of the fluctuation on that of the window length, with Slope(y, x) the least squares slope of y on x over the window lengths n:
\[H = \text{Slope}\left(\ln\left(F(n)\right), \ln\left(n\right)\right)\]For returns without memory the fluctuation grows with the square root of \(n (H = 0.5)\); a higher H is a trending and a lower H a mean-reverting price.
The Hurst Exponent (H) measures the long-term memory of a series: whether a move tends to be followed by a move in the same direction or by a reversal. Harold Edwin Hurst found it in the yearly floods of the Nile, whose wet and dry years cluster more than independent years would, and Benoit Mandelbrot carried it over to prices. Each method measures how fast some spread of the price path grows with the horizon: for a random walk it grows with the square root of the horizon, which gives \(H = 0.5\). The Hurst Exponent is interpreted as follows:
- \(H < 0.5\): the series is mean-reverting (anti-persistent).
- \(H = 0.5\): the series is a random walk (no memory).
- \(H > 0.5\): the series is trending (persistent).
For liquid stocks the estimate is usually close to 0.5, and from daily data over short horizons it describes short-term behaviour only. A value slightly away from 0.5 over a few years is usually within the estimation noise rather than evidence of a tradable pattern, so the exponent is mostly of use to compare assets with each other or a rolling estimate over time.
Also known as: DFA, DFA exponent, detrended fluctuation.
In the Finance Toolkit this is get_hurst_exponent with method="detrended_fluctuation", the same function as Hurst Exponent.
Example · EURUSD, GCUSD, BTCUSD · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_hurst_exponent:
from financetoolkit import Toolkit
toolkit = Toolkit(
["EURUSD", "GCUSD", "BTCUSD"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.risk.get_hurst_exponent(method="detrended_fluctuation")
| Parameter | Type | Description | Example |
|---|---|---|---|
max_lag | int | The maximum lag of the generalized method, included: the regression runs over the lags 2 up to and including max_lag. Defaults to 20. | 50 |
method | str | The estimation method. Options are: “generalized” (Generalized Hurst Exponent), “rescaled_range” (Rescaled Range Analysis), “detrended_fluctuation” (Detrended Fluctuation Analysis). Defaults to “generalized”. | "generalized" |
period | str | None | The data frequency. Options are: “daily”, “weekly”, “monthly”, “quarterly”, “yearly”. Defaults to None, which uses “daily”. | "weekly" |
rolling | int | None | The number of periods of a rolling window, at least the shortest window with a value: 5 for “generalized”, and for the other methods the second smallest window size, 120 with the default window sizes. Defaults to None, which calculates one value over the full history of every asset. | 4 |
q | float | list[float] | The order of the moment of the generalized method, or a list of orders for a column per order. Defaults to 2.0. | 2.0 |
window_sizes | list[int] | None | The window sizes of the rescaled range and the detrended fluctuation. Defaults to None, which uses 10 up to a tenth of the observations, growing by 20%. | None, which uses 10 up to a tenth of the observations, growing by 20% |
corrected | bool | Whether to apply the Anis-Lloyd-Peters correction to the rescaled range. Defaults to True. | True |
order | int | The degree of the polynomial removed from every window of the detrended fluctuation. Defaults to 1. | 2 |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 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 |
Which returns:
| Item | 0 |
|---|---|
| EURUSD | 0.4624 |
| GCUSD | 0.4782 |
| BTCUSD | 0.5192 |
| Benchmark | 0.4678 |
4 rows
References
- Paper Peng, C.-K., Buldyrev, S.V., Havlin, S., Simons, M., Stanley, H.E. & Goldberger, A.L. (1994). “Mosaic organization of DNA nucleotides.” Physical Review E, 49(2), 1685-1689.
- Library Referenced against nolds’ dfa (Detrended Fluctuation Analysis)
- Library Shares similar logic as antropy’s detrended_fluctuation (Detrended Fluctuation Analysis)