[Python] Rolling-window analysis in python

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to python and just using it for my undergrad thesis. I want to perform a rolling window analysis using "fathon" package that explores on mulftifractality. Are there any libraries or documentation that I can refer to when employing my own rolling window MFDCCA analysis? All I can see is rolling window basic statistics from rolling window function (Pandas).

    Basically, I want to generate the fluctuation function and generalized Hurst exponent (which requires wins - an array of window sizes) for each window

    If I were to perform an analysis over a set of data, this is how it goes:

    #define q list (change)
    qs = np.array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    wins = fu.linRangeByStep(30, 340, step=10)

    #zero-mean cumulative sum
    a = fu.toAggregated(a)
    b = fu.toAggregated(b)

    #initialize mfdfa object
    pymfdcca = fathon.MFDCCA(a, b)

    #compute fluctuation function and generalized Hurst exponent
    n,F = pymfdcca.computeFlucVec(wins, qs, revSeg=True, polOrd=2)
    list_H, list_H_intercept = pymfdcca.fitFlucVec()


    I have tried this one but I am not sure

    # ROLLING WINDOW ANALYSIS

    # Define rolling window parameters
    window_size = 100 # Example: 1 year of trading days
    step_size = 30 # Example: 1 month of trading days

    # Define q list
    qs = np.array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    wins = fu.linRangeByStep(10, 100)

    # Initialize lists to store maximum multifractal spectrum and corresponding dates
    max_spectrum = []
    window_end_dates = []
    rolling_results = []
    rolling_mfspect = []
    rolling_alpha = []

    # Rolling window analysis for PSEI and Brent Oil
    for start in range(0, len(a) - window_size + 1, step_size):
    end = start + window_size
    a_window = a[start:end]
    b_window = b[start:end]

    # Zero-mean cumulative sum
    a_window = fu.toAggregated(a)
    b_window = fu.toAggregated(b)

    # Initialize MFDCCA object
    rw_mfdcca = fathon.MFDCCA(a_window, b_window)

    # Compute fluctuation function and generalized Hurst exponents
    n, F = rw_mfdcca.computeFlucVec(wins, qs, revSeg=True, polOrd=1)
    list_H_rw, list_H_intercept_rw = rw_mfdcca.fitFlucVec()

    # Compute mass exponents
    tau_rw = rw_mfdcca.computeMassExponents()

    # Compute multifractal spectrum
    alpha_rw, mfSpect_rw = rw_mfdcca.computeMultifractalSpectrum()

    # Store maximum multifractal spectrum and corresponding date
    max_spectrum.append(np.max(mfSpect_rw))
    window_end_dates.append(dates[end-1])

    # Store results
    rolling_results_psei.append((n, F, list_H_rw, list_H_intercept_rw, tau_rw, alpha_rw, mfSpect_rw))
    rolling_mfspect.append((mfSpect_PSEIrw))
    rolling_alpha.append((alpha_PSEIrw))

    Continue reading...

Compartilhe esta Página