[Python] plot_acf vs acf (graphical vs numerical way of finding significal coefficients in...

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm following along a book that shows how to plot the autocorrelation function (using plot_acf) to investigate the coefficients for the various lags, to see with which lag they abruptly become non-significant (by falling within the shaded confidence area on the plot).

    I have tried to obtain this result through a numerical way by using acf to extract the "acf_values" and the "confidence interval boundaries" to compare them, but here I obtain a different result.

    For example, with "plot_acf", I find there are significant coefficients up until lag 2, while the "numerical way", no coefficients show as significant, as they all fall within the "confidence interval boundaries" provided by acf.

    Any insights? Why do these two methods produce different results?

    Thanks

    SUMMARY: I have tried to recreate the results I obtained using a graphical method through a "numerical", more systematic method, and I have reached different conclusions. I am now confused.

    ###################################################

    MINIMAL REPRODUCIBLE EXAMPLE:

    # Import libraries
    from statsmodels.graphics.tsaplots import plot_acf
    from statsmodels.tsa.stattools import acf
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd

    # ____ DATASETS ____
    # Load the original dataset
    df_original = pd.read_csv('widget_sales.csv')

    # Create the "differenced" dataset
    df_differenced = np.diff(df_original['widget_sales'], n=1)


    # ____ THE PLOT WAY ____
    # Look for significant coefficients usning "plot_acf"
    plot_acf(df_differenced, lags=50);


    # ____ THE NUMERICAL WAY ____
    # Now trying to look for significant coefficients, but in a more systematic numerical way:

    # Extract all the acf_values and the confidence interval bounds:
    acf_values, confint = acf(df_differenced, nlags=50, alpha=0.05, fft=True)

    # ____ COMPARE VALUES / BOUNDS ____
    # Check which ACF values are outside the confidence interval
    # (PS I tried implementing this "checking" part in different ways as well,
    # but even just eyeing the "acf_values" and "confint" values, I can see that they all fall within the interval)

    significant_lags = np.where((acf_values < confint[:, 0]) | (acf_values > confint[:, 1]))[0]
    significant_lags # returns an empty array


    The dataset I'm using can be found at:

    https://github.com/marcopeix/TimeSeriesForecastingInPython/blob/master/data/widget_sales.csv

    Thanks

    Continue reading...

Compartilhe esta Página