# Finding when the autocorrelation of a set of timeseries switches from positive to negative

In an artificial dataset comprised of 1,000 timeseries with 1,000 timesteps each, let's assume there is a specific timestep at which the second coefficient of the autocorrelation function switches from positive to negative (or vice versa).

The timeseries pass the test of stationarity and the distribution of their means has a mean of close to zero with a distribution that seems like a mixture of a normal distribution and some positive outliers:

I can find the coefficient of the autocorrelation function for any timeseries using the acf method of the statsmodels package. I use the following code to visualize the distribution of the values of the second acf coefficient for different timesteps and I observe that I have a mixed picture instead with both negative and positive values at different timesteps but with a distribution that gradually moves towards assigning more probability mass to negative values after a certain point.

I wonder how I can estimate the exact timestep at which the transition occurs given that I am dealing apparently with noisy data. One way to go would be to calculate at each timestep the relative frequency of the second coefficients of the autocorrelation function that have positive value and decide that a regime change occurs when this number reaches its peak.

Here is the code I use and some visualizations of the distributions:

from statsmodels.tsa.stattools import acf

timesteps = np.arange(50, 1000, 20)
for i in np.arange(5):

timesteps_temp = timesteps[i*12 : (i+1)*12 ]

fig = plt.figure()
colors = ['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928']

for timestep, i in zip(timesteps_temp, np.arange(1, 13)):
data = stationary_ts.iloc[:timestep,:]  # stationary_ts is the 1000x1000 dataframe containing as columns the 1000 timeseries
acfs = [acf(data[col])[1] for col in data.columns]
sns.distplot(acfs,ax=ax, color = colors[i-1])
ax.set_title(f'Data from beginning and up to timestep {timestep}')
plt.show()


I can use the approach I described to find the peak of the relative frequency of the positive values of the second coefficient of the autocorrelation function with the following code:

figsize(31,16)
sns.distplot(means, bins = 100)
plt.title('Distribution of the means of the timeseries')

frequency_of_positive_examples = pd.Series(frequency_of_positive_examples_list, index = stationary_ts.index[50:])

frequency_of_positive_examples.plot()
plt.axvline(frequency_of_positive_examples[550:].argmax(), color = 'black')
plt.title('Relative frequency of positive examples')


Is this approach valid? Is there a better approach?