I want to create a covariance matrix of time series data for the purpose of Monte Carlo simulation however this matrix is between multiple assets.
I.e. I not only want to know the covariance between X(t), X(t+1), ... , X(t+n) but also the covariances between X(t), Y(t), Y(t+1), etc.
Is there a simple way to generate a covariance matrix in R that is the covariance between every element in an dataframe?
Thanks
This produces the covariances up to nl
lags:
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