Python - generate an array of specific autocorrelation

I am interested in creating an array (or numpy Series) of length N, which will show a certain autocorrelation in lag 1. Ideally, I also want to specify the average value and variance and get the data obtained from the (multi) normal distribution. But most importantly, I want to indicate autocorrelation. How to do this using numpy or scikit-learn?

To be explicit and accurate, this is the autocorrelation that I want to control:

numpy.corrcoef(x[0:len(x) - 1], x[1:])[0][1] 
+6
source share
1 answer

If you are only interested in autocorrelation with a delay of one, you can generate a first-order autoregressive process with a parameter equal to the desired autocorrelation; this property is mentioned on the Wikipedia page , but it is not difficult to prove.

Here is a sample code:

 import numpy as np def sample_signal(n_samples, corr, mu=0, sigma=1): assert 0 < corr < 1, "Auto-correlation must be between 0 and 1" # Find out the offset 'c' and the std of the white noise 'sigma_e' # that produce a signal with the desired mean and variance. # See https://en.wikipedia.org/wiki/Autoregressive_model # under section "Example: An AR(1) process". c = mu * (1 - corr) sigma_e = np.sqrt((sigma ** 2) * (1 - corr ** 2)) # Sample the auto-regressive process. signal = [c + np.random.normal(0, sigma_e)] for _ in range(1, n_samples): signal.append(c + corr * signal[-1] + np.random.normal(0, sigma_e)) return np.array(signal) def compute_corr_lag_1(signal): return np.corrcoef(signal[:-1], signal[1:])[0][1] # Examples. print(compute_corr_lag_1(sample_signal(5000, 0.5))) print(np.mean(sample_signal(5000, 0.5, mu=2))) print(np.std(sample_signal(5000, 0.5, sigma=3))) 

The corr parameter allows corr to set the desired autocorrelation to delay 1, and the optional parameters mu and sigma allow you to control the average value and standard deviation of the generated signal.

+4
source

Source: https://habr.com/ru/post/1236721/


All Articles