我被R中的协方差所困惑。当我使用e(x*y(-e(x(e(y(时,它将返回COV((的另一个值。你能帮我理解吗?我的代码:
spot<- c(0.5,0.61,-0.22,-0.35,0.79,0.04,0.15,0.7,-0.51,-0.41)
future<- c(0.56,0.63,-0.12,-0.44,0.6,-0.06,0.01,0.8,-0.56,-0.46)
ms<-mean(spot)
mf<-mean(future)
msf<-mean(spot*future)
cov<- msf-mf*ms
#the way above is wrong for giving 0.22272 while cov gives 0.2474667
covr<- cov(spot,future)
我认为您不使用正确的公式。两个向量之间的协方差 X
和 Y
之间的协方差,每个长度为 n
是:
cov(X,Y) = sigma((X-mean(X))*(Y-mean(Y)))/(n-1)
spot<- c(0.5,0.61,-0.22,-0.35,0.79,0.04,0.15,0.7,-0.51,-0.41)
future<- c(0.56,0.63,-0.12,-0.44,0.6,-0.06,0.01,0.8,-0.56,-0.46)
covar = sum((spot-mean(spot))*(future-mean(future)))/(length(spot)-1)
#covar
#0.2474667