Python-生成特定自相关数组 [英] Python - generate array of specific autocorrelation
问题描述
我对生成长度为N的数组(或numpy系列)感兴趣,该数组将在滞后1时表现出特定的自相关.理想情况下,我也想指定均值和方差,并从(多)中提取数据正态分布.但最重要的是,我想指定自相关.我该如何使用numpy或scikit-learn?
I am interested in generating an array(or numpy Series) of length N that will exhibit specific autocorrelation at lag 1. Ideally, I want to specify the mean and variance, as well, and have the data drawn from (multi)normal distribution. But most importantly, I want to specify the autocorrelation. How do I do this with numpy, or scikit-learn?
为了明确和精确,这是我要控制的自相关:
Just to be explicit and precise, this is the autocorrelation I want to control:
numpy.corrcoef(x[0:len(x) - 1], x[1:])[0][1]
推荐答案
如果您只对滞后一时的自相关感兴趣,则可以生成维基百科页面上提到了此属性,但这并不难证明这一点.
If you are interested only in the auto-correlation at lag one, you can generate an auto-regressive process of order one with the parameter equal to the desired auto-correlation; this property is mentioned on the Wikipedia page, but it's not hard to prove it.
以下是一些示例代码:
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)))
参数corr
可让您在滞后一处设置所需的自相关,而可选参数mu
和sigma
可让您控制所生成信号的均值和标准差.
The parameter corr
lets you set the desired auto-correlation at lag one and the optional parameters, mu
and sigma
, let you control the mean and standard deviation of the generated signal.
这篇关于Python-生成特定自相关数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!