# Cross correlation with StatsBase.crosscor answer does not make sense

**URL:** <https://discourse.julialang.org/t/cross-correlation-with-statsbase-crosscor-answer-does-not-make-sense/44374>\
**Category:** Statistics\
**Tags:** statistics\
**Created:** [August 5, 2020, 10:57pm UTC](https://discourse.julialang.org/t/cross-correlation-with-statsbase-crosscor-answer-does-not-make-sense/44374 "2020-08-05T22:57:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruan\_Stander](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruan_stander/32/11304_2.png) [@Ruan\_Stander](https://discourse.julialang.org/u/Ruan_Stander)\
**Post date:** [August 5, 2020, 10:57pm UTC](https://discourse.julialang.org/t/cross-correlation-with-statsbase-crosscor-answer-does-not-make-sense/44374/1 "2020-08-05T22:57:28Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [August 6, 2020, 1:55am UTC](https://discourse.julialang.org/t/cross-correlation-with-statsbase-crosscor-answer-does-not-make-sense/44374/2 "2020-08-06T01:55:58Z")

</div>

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

```julia
# 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.

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

```

but not for small n.
