Multifractal Detrended Fluctuation Analysis

repository·master·Indexed 41 days ago

https://github.com/lrydin/mfdfa

A Python library for Multifractal Detrended Fluctuation Analysis (MFDFA), a model-independent method used to uncover self-similarity and multifractality in stochastic processes or auto-regressive models. It includes utilities for generating fractional Gaussian noise (fGn), estimating the Hurst exponent, and extensions for Empirical Mode Decomposition (EMD) detrending, Extended DFA (eDFA), and moving window analysis.

Tokens
4.7K
Snippets
20
Records
25
Agent score
50%

What's inside MFDFA

  1. Overview of the MFDFA library

    master
    The MFDFA library is a Python implementation of Multifractal Detrended Fluctuation Analysis. It is based on the original method developed by Peng et al. and extended by Kantelhardt et al. to study multifractality in nonstationary time series.
  2. Explore MFDFA extensions for detrending and analysis

    master

    The MFDFA library supports several enhancements to the standard Multifractal Detrended Fluctuation Analysis. Key extensions include:

    • Empirical Mode Decomposition (EMD) Detrending: Uses EMD as a source of detrending instead of traditional polynomial fittings. This allows for more precise removal of known trends in a time series.
    • Extended DFA: Advanced variations of the Detrended Fluctuation Analysis.
    • Moving Window Analysis: Implementation of window-based analysis techniques.
  3. Use the moving window extension for segmentation

    master

    When analyzing short time series, large lags can result in poor statistics because there are too few possible segments (e.g., a lag of 512 on a 2048-point series only allows 4 segments). To improve statistical reliability at large lags, you can use the window extension in the MFDFA function.

    To implement this, pass a dictionary to the extensions parameter where the key 'window' is set to the desired step-size (the number of data points the window moves over the data).

    # Example: Using a moving window with a step-size of 32
    # to increase statistics for large lags
    lag, dfa, edfa = MFDFA(y, lag=lag, extensions={'window': 32})
  4. Perform (MF)DFA with EMD detrending

    master

    To perform multifractal detrended fluctuation analysis using EMD, pass the desired IMF indices to the extensions dictionary in the MFDFA function.

    When you provide IMFs via the extensions dictionary (e.g., extensions = {"EMD": [6,7,8]}), the library subtracts those specific IMFs from the timeseries. This effectively sets the polynomial detrending order to 0, as the trend is handled by the EMD components instead of polynomial fitting.

    import numpy as np
    from MFDFA import MFDFA
    
    # Define the lag range (e.g., from small to long segments)
    lag = np.logspace(0.7, 4, 30).astype(int)
    
    # Perform (MF)DFA by declaring which IMFs to subtract via the 'extensions' dictionary
    lag, dfa = MFDFA(y, lag=lag, extensions={"EMD": [6,7,8]})
  5. Generate a fractional Ornstein–Uhlenbeck process

    master

    You can generate a fractional Ornstein–Uhlenbeck process using the Euler–Maruyama integration method. This requires generating fractional Gaussian noise (fGn) first.

    Key parameters:

    • t_final: Total integration time.
    • delta_t: Time sampling interval.
    • theta: Drift parameter.
    • sigma: Diffusion parameter.
    • H: Hurst exponent (self-similarity exponent).

    Note: This process requires the fgn function from the MFDFA library to generate the fractional Gaussian noise component.

    # Imports
    from MFDFA import MFDFA
    from MFDFA import fgn
    import numpy as np
    
    # integration time and time sampling
    t_final = 500
    delta_t = 0.001
    
    # Some drift theta and diffusion sigma parameters
    theta = 0.3
    sigma = 0.1
    
    # The time array of the trajectory
    time = np.arange(0, t_final, delta_t)
    
    # The fractional Gaussian noise
    H = 0.7
    dB = (t_final ** H) * fgn(N = time.size, H = H)
    
    # Initialise the array y
    y = np.zeros([time.size])
    
    # Integrate the process
    for i in range(1, time.size):
       y[i] = y[i-1] - theta * y[i-1] * delta_t + sigma * dB[i]
  6. Estimate the Hurst exponent from MFDFA results

    master

    To estimate the Hurst exponent ($H$) from the MFDFA output, you must fit a line to the results on a log-log scale.

    Note that because the plot is double-logarithmic, you must fit the logs of the lag and dfa values. The slope of this fit relates to the Hurst exponent as: $\text{slope} = H + 1$ (or similar depending on the specific scaling implementation, but the example uses H_hat[0] as the slope).

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Assuming 'lag' and 'dfa' are obtained from MFDFA()
    plt.loglog(lag, dfa, 'o')
    
    # Fit the line to the logs of the results
    # Using a slice (e.g., [4:20]) to avoid edge effects at very small/large scales
    H_hat = np.polyfit(np.log(lag)[4:20], np.log(dfa[4:20]), 1)[0]
    
    print('Estimated H = ' + '{:.3f}'.format(H_hat))
  7. Install the MFDFA library via pip

    master

    You can install the MFDFA library directly from PyPI using pip.

    If you intend to use the extension for Empirical Mode Decomposition (EMD) detrending, you must also install the EMD-signal package.

    pip install MFDFA
    
    # Required if using Empirical Mode Decomposition detrending
    pip install EMD-signal
  8. Use the eDFA extension in MFDFA

    master

    Extended Detrended Fluctuation Analysis (eDFA) is a metric that calculates the difference between the maximum and minimum values for each data segmentation: $\mathrm{d}F (n) = \mathrm{max}[F(n)] - \mathrm{min}[F(n)]$. This results in a power-law exponent $\mathrm{d}F(n) \sim n^\beta$.

    To use eDFA with the MFDFA library, pass a dictionary to the extensions parameter where the key 'eDFA' is set to True. When this extension is enabled, the MFDFA function will return an additional output value representing the eDFA results.

    # Select a band of lags, which usually ranges from
    # very small segments of data, to very long ones, as
    lag = np.logspace(0.7, 4, 30).astype(int)
    
    # Obtain the (MF)DFA by declaring the IMFs to subtract
    # in a list in the dictionary of the extensions
    lag, dfa, edfa = MFDFA(y, lag = lag, extensions = {'eDFA': True})
  9. Estimate the Hurst exponent (H) from MFDFA results

    master

    The Hurst exponent $H$ can be estimated by analyzing the relationship between the lag and the fluctuation function on a log-log scale.

    In a double logarithmic scale, the slope of the line is equal to $H + 1$. To find this slope, fit a first-order polynomial to the logarithms of the lag and dfa values using numpy.polyfit.

    Note: It is common practice to exclude the very smallest lags (e.g., using a slice like [4:20]) to avoid instabilities in the estimation.

    import numpy as np
    import matplotlib.pyplot as plt
    
    # To uncover the Hurst index, get log-log plots
    plt.loglog(lag, dfa, 'o', label='fOU: MFDFA q=2')
    
    # Fit the line to find the slope in double logarithmic scales
    # The slope in log-log space is H + 1
    H_hat = np.polyfit(np.log(lag)[4:20], np.log(dfa[4:20]), 1)[0]
    
    print('Estimated H = ' + '{:.3f}'.format(H_hat))