python - generate array of specific autocorrelation

  • Last Update :
  • Techknowledgy :

Here is some 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)))

Suggestion : 2

The parameter corr lets you set the anycodings_numpy desired auto-correlation at lag one and anycodings_numpy the optional parameters, mu and sigma, anycodings_numpy let you control the mean and standard anycodings_numpy deviation of the generated signal.,I am interested in generating an array(or anycodings_numpy numpy Series) of length N that will exhibit anycodings_numpy specific autocorrelation at lag 1. Ideally, anycodings_numpy I want to specify the mean and variance, as anycodings_numpy well, and have the data drawn from anycodings_numpy (multi)normal distribution. But most anycodings_numpy importantly, I want to specify the anycodings_numpy autocorrelation. How do I do this with anycodings_numpy numpy, or scikit-learn?,Just to be explicit and precise, this is the anycodings_numpy autocorrelation I want to control:,If you are interested only in the anycodings_numpy auto-correlation at lag one, you can anycodings_numpy generate an auto-regressive process of anycodings_numpy order one with the parameter equal to anycodings_numpy the desired auto-correlation; this anycodings_numpy property is mentioned on the Wikipedia anycodings_numpy page, but it's not hard to prove it.

Just to be explicit and precise, this is the anycodings_numpy autocorrelation I want to control:

numpy.corrcoef(x[0: len(x) - 1], x[1: ])[0][1]

Here is some 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)))

Suggestion : 3

Last Updated : 17 Jan, 2022

Syntax:

pd.plotting.lag_plot(data, lag = 1)

Syntax:

pandas.DataFrame.corr(method = 'pearson')

Suggestion : 4

The definition of correlation above is not unique and sometimes correlation may be defined differently. Another common definition is:,uses FFT which has superior performance on large arrays.,numpy.correlate may perform slowly in large arrays (i.e. n = 1e5) because it does not use the FFT to compute the convolution; in that case, scipy.signal.correlate might be preferable.,This function computes the correlation as generally defined in signal processing texts:

>>> np.correlate([1, 2, 3], [0, 1, 0.5])
array([3.5]) >>>
   np.correlate([1, 2, 3], [0, 1, 0.5], "same")
array([2., 3.5, 3.]) >>>
   np.correlate([1, 2, 3], [0, 1, 0.5], "full")
array([0.5, 2., 3.5, 3., 0.])
>>> np.correlate([1 + 1 j, 2, 3 - 1 j], [0, 1, 0.5 j], 'full')
array([0.5 - 0.5 j, 1.0 + 0. j, 1.5 - 1.5 j, 3.0 - 1. j, 0.0 + 0. j])
>>> np.correlate([0, 1, 0.5 j], [1 + 1 j, 2, 3 - 1 j], 'full')
array([0.0 + 0. j, 3.0 + 1. j, 1.5 + 1.5 j, 1.0 + 0. j, 0.5 + 0.5 j])

Suggestion : 5

Number of lags in the sample ACF, specified as a positive integer. autocorr uses lags 0:NumLags to estimate the ACF.,Number of lags in a theoretical MA model of the input time series, specified as a nonnegative integer less than NumLags.,The default is min([20,T – 1]), where T is the effective sample size of the input time series.,[acf,lags] = autocorr(y) returns the sample autocorrelation function (ACF) acf and associated lags lags of the univariate time series y.

load Data_GDP
plot(Data)
ret = price2ret(Data);
[acf, lags] = autocorr(ret);
[acf lags]
ans = 21× 2

1.0000 0
0.3329 1.0000
0.1836 2.0000
   -
   0.0216 3.0000 -
   0.1172 4.0000 -
   0.1632 5.0000 -
   0.0870 6.0000 -
   0.0707 7.0000 -
   0.0380 8.0000
0.0554 9.0000⋮
load Data_ElectricityPrices.mat
DataTable.Properties.VariableNames
ans = 1× 1 cell array {
   'SpotPrice'
}