1

This is my code:

import matplotlib.pyplot as plt
import numpy as np
from pandas.plotting import autocorrelation_plot

y = np.sin(np.arange(1,6*3.14,0.1))
autocorrelation_plot(y)
plt.show()

And this is the output of the auto-correlation plot:

auto-correlation plot of y

I would like to figure out a way to classify whether the function is periodic or not automatically (without using the bare-eye to look at the autocorrelation plot). I read that it is related to the confidence interval which is the line shown in the attached plot, but still have doubt on what I should do with it to better decide. So is there an automated way for using auto-correlation to decide the perdiodicity of the data?

Though, this is my try for an automated way:

result = np.correlate(y, y, mode = "full")
ACF = result[np.round(result.size/2).astype(int):]
ACF = ACF/ACF[0]
acceptedVar = []
for i in range(len(ACF)):
    if ACF[i] > 0.05:
        acceptedVar = np.append(acceptedVar, ACF[i])

percent = len(acceptedVar)/len(ACF) * 100

I just made a threshold of 0.05 to detect the points for which the confidence interval is 95%. Don't know if this is right or wrong statistically and logically. I then see if percent is bigger than 95% for a periodic pattern. I'm not sure of that as well.

Credit to: the first answer to How can I use numpy.correlate to do autocorrelation?

ezzeddin
  • 499
  • 2
  • 5
  • 25

1 Answers1

0

To start, with e.g. ax = autocorrelation_plot(y) you can use ax.lines[5].get_data()[1] to use the values from the pandas autocorrelation function directly.

This may be a somewhat naïve solution, but say you are just looking for the first, most significant, periodicity, you could just grab the first index of the highest peak in the plot:

first_max = np.argmax(autocorr) + 1

Which gives you the lag for which autocorrelation is highest = the period of interest (in units of your data's sampling interval.)

Say you wanted the next most significant period:

second_max = np.argmax(autocorr[first_max:]) + first_max + 1

And so on and so forth...

To note: this wouldn't work as well if your data is not as regular and periodic as it seems to be from your autocorrelation plot.