R中的同一公式的两个不同的协方差返回



我被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)

我认为您不使用正确的公式。两个向量之间的协方差 XY之间的协方差,每个长度为 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

相关内容

  • 没有找到相关文章

最新更新