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

时间:2018-02-26 08:48:09

标签: r covariance

我对R中的协方差感到困惑。 当我使用E(x * y)-E(x)E(y)时,它为cov()返回不同的值。 你能帮我理解一下吗? 我的代码:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 
'file:///Databases.csv' AS line
WITH line

CREATE (database:DATABASE {databaseName: line.DatabaseName})
SET database.server = line.Server;

1 个答案:

答案 0 :(得分:1)

我不认为你使用了正确的配方。每个长度为X的两个向量Yn之间的协方差公式为:

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