Cross correlation with StatsBase.crosscor answer does not make sense

Using StatsBase.crosscor and finding unexpected answers
Example below:
answer below is 0.66666 and I think it should be 1 since crosscor calculates the correlation of numone with the 1 lag correlation of numtwo which is perfectly correlated

numone=[2,3,4,5]
numtwo=[1,2,3,4]
answer=StatsBase.crosscor(numone,numtwo,[1],demean=false)
1 Like

Here’s what crosscor(x, y, [1], demean=false) is doing:

# Julia 1.5
using LinearAlgebra
numone, numtwo = [2,3,4,5], [1,2,3,4]
x, y = numone[1:end-1], numtwo[2:end]
ra = dot(x,y)/sqrt(dot(numone,numone)*dot(numtwo,numtwo)) # what crosscor does
rb = dot(x,y)/sqrt(dot(x,x)*dot(y,y)) # what you expect
[ra rb]

1×2 Array{Float64,2}:
 0.720511  1.0

Note that ra~rb for large n, e.g.

numone, numtwo = [2:1001;], [1:1000;]

but not for small n.

3 Likes