Q:

why covariance2() is giving wrong answer?

sMean = function(x) {
  sum = 0;
  for (i in 1:length(x)) {
    sum = sum + x[i]
  }
  return(sum/length(x))
}

covariance = function(x,y) {
  #Formula is E((x-E[x])(y-E[y])) => E[xy] - E[x]E[y]
  meanX = sMean(x)
  meanY = sMean(y)
  cov = 0;
  for (i in 1:length(x)) {
    cov = cov + ((x[i] - meanX) * (y[i] - meanY))
  }
  cov = cov/(length(x)-1)
  return (cov)
}

covariance2 = function(x,y) {
  #Formula is E((x-E[x])(y-E[y])) => E[xy] - E[x]E[y]
  meanX = (sMean(x) * length(x)) /(length(x)-1)
  meanY = (sMean(y) * length(x)) /(length(x)-1)
  meanXY = (sMean(x*y) * length(x)) /(length(x)-1)
  return (meanXY - meanX*meanY)
}

#Output
  #> cov(arr,arr2)
  #[1] 16.75
  #> covariance(arr,arr2)
  #[1] 16.75
  #> covariance2(arr,arr2) #Why this function give wrong output?
  #[1] -9.5

Why covariance2 is giving wrong output? According to http://en.wikipedia.org/wiki/Covariance#Definition output should be same.

A:

In covariance2(), you need to divide by length(x) rather than length(x) - 1. 
covariance2 = function(x,y) {
    meanX <- sMean(x)
    meanY <- sMean(y)
    meanXY <- sMean(x*y)
    return((meanXY - meanX * meanY) * length(x) / (length(x) - 1))
}

The maths behind this is that denoting expectation (i.e. mean) of a length N vector x by E[x]=sum(x)/N, you can show that E[xy]-E[x]E[y] = E[(x-E(x))(y-E(y))]. This is a general fact that comes from the linearity of the expectation operator. Your covariance() function returns the right-hand side of this equality multiplied by N/(N-1) (which is the sample covariance). So the left-hand side, which is computed in covariance2(), also needs to be multiplied by the same factor. 

