我想创建一个时间序列数据的协方差矩阵,以进行蒙特卡洛模拟,但是此矩阵在多个资产之间。
即。我不仅想知道x(t),x(t 1),...,x(t n)之间的协方差,而且还知道x(t),y(t),y(t )之间的协方差1)等。
是否有一种简单的方法来生成r中的协方差矩阵,即数据框中的每个元素之间的协方差?
谢谢
这会产生协方差,直到nl
滞后:
set.seed(123)
c1 <- data.frame(matrix(rnorm(90),ncol=3))
nl=2 # number of lags
c1wlags=embed(as.matrix(c1),nl+1)
nams=colnames(c1)
ndfs=paste(rep(nams,nl),rep(1:nl,each=ncol(c1)),sep="t-")
colnames(c1wlags)=c(nams,ndfs)
cov(c1wlags)
> cov(c1wlags)
X1 X2 X3 X1t-1 X2t-1 X3t-1 X1t-2 X2t-2 X3t-2
X1 1.02206766 -0.13725252 0.153975857 -0.04152948 0.02664973 -0.123077418 -0.16800464 0.26851379 -0.25855822
X2 -0.13725252 0.73845275 -0.213758648 0.09490394 -0.11391726 -0.032365562 0.01863737 0.02661447 -0.11817727
X3 0.15397586 -0.21375865 0.797592802 -0.44057820 0.05532353 -0.007062553 -0.15315625 0.24462652 -0.19898668
X1t-1 -0.04152948 0.09490394 -0.440578196 0.96044907 -0.13594448 0.101988693 0.01357564 0.02734684 -0.11077465
X2t-1 0.02664973 -0.11391726 0.055323528 -0.13594448 0.74666015 -0.206596930 0.10511073 -0.11827071 -0.03883939
X3t-1 -0.12307742 -0.03236556 -0.007062553 0.10198869 -0.20659693 0.758188218 -0.38571731 0.05271998 -0.00125309
X1t-2 -0.16800464 0.01863737 -0.153156254 0.01357564 0.10511073 -0.385717313 0.92688519 -0.14256158 0.08240385
X2t-2 0.26851379 0.02661447 0.244626522 0.02734684 -0.11827071 0.052719977 -0.14256158 0.74893806 -0.20346301
X3t-2 -0.25855822 -0.11817727 -0.198986683 -0.11077465 -0.03883939 -0.001253090 0.08240385 -0.20346301 0.76041872