简体   繁体   English

构造2个具有固定相关性的时间序列随机变量

[英]Construct 2 time series random variables with fixed correlation

Is there an easy way to generate two time-series with a fixed correlation? 是否有一种简单的方法来生成具有固定相关性的两个时间序列? For instance 0.5. 例如0.5。

Does anyone know a solution in R or Python? 有人知道R或Python中的解决方案吗? Thanks! 谢谢!

This question is quite general, I think. 我认为这个问题很笼统。 It is not limited to just time-series. 它不仅限于时间序列。 What you are asking is to generate 2d random variable, with known covariance. 您要问的是生成具有已知协方差的2d随机变量。 r==0.5, std1=1 and std2=2 would translate to a covariance matrix of [[1,1],[1,4]] . r==0.5, std1=1 and std2=2将转换为[[1,1],[1,4]]的协方差矩阵。 Therefore, if we assume the data is multidimensional normal distributed, we can generate such a random variable: 因此,如果我们假设数据是多维正态分布的,则可以生成这样的随机变量:

In [42]:
import numpy as np
val=np.random.multivariate_normal((0,0),[[1,1],[1,4]],1000)
In [43]:

np.corrcoef(val.T)
Out[43]:
array([[ 1.      ,  0.488883],
       [ 0.488883,  1.      ]])
In [44]:

np.cov(val.T)
Out[44]:
array([[ 1.03693888,  0.96490767],
       [ 0.96490767,  3.75671707]])
In [45]:

val=np.random.multivariate_normal((0,0),[[1,1],[1,4]],10)
In [46]:

np.corrcoef(val.T)
Out[46]:
array([[ 1.        ,  0.56807297],
       [ 0.56807297,  1.        ]])
In [48]:

val[:,0]
Out[48]:
array([-0.77425116,  0.35758601, -1.21668939, -0.95127533, -0.5714381 ,
        0.87530824,  0.9594394 ,  1.30123373,  1.92511929,  0.98070711])
In [49]:

val[:,1]
Out[49]:
array([-1.75698285,  2.24011423, -3.5129411 , -1.33889305,  2.32720257,
        0.53750133,  3.23935645,  2.96819425, -0.72551024,  3.0743096 ])

As shown in this example, if your sample size is small, the resulting random variable may deviate from r=0.5 , considerably. 如本例所示,如果样本量较小,则所得的随机变量可能会偏离r=0.5很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM