What is the equation used in R's ccf and Julia's crosscor?

892 views Asked by At

I've been scratching my head a lot about this. Now, except for the fact that Julia gives out a seemingly backwards result with respect to the lags

julia> crosscor([1,2,3,4], [1,2,3,1])'
1x7 Array{Float64,2}:
 -0.30339  0.0  0.64049  0.13484  -0.37081  -0.40452  0.30339

> print(ccf(c(1,2,3,4), c(1,2,3,1), type="correlation", plot=F))

Autocorrelations of series ‘X’, by lag

    -3     -2     -1      0      1      2      3 
 0.303 -0.405 -0.371  0.135  0.640  0.000 -0.303

which is easily solved by inverting the x and y ("easily" is relative when it takes a hour of your life to figure out), the numbers don't resemble any of the equations I'm familiar with (which aren't many, anyway).

So, I opened up my stats textbook and found a host of other formulas which I am not going to test with pen and paper. I suspected for a moment that removing the means from the vectors played a role, so I tried not doing that (sadly, this can only be done in Julia)

julia> crosscor([1,2,3,4], [1,2,3,1], demean=false)'
1x7 Array{Float64,2}:
 0.188562  0.518545  0.942809  0.848528  0.518545  0.235702  0.0471405

but this still does not resemble the normalised cross-correlation that I computed with pen and paper (pending mistakes which I may have made).

In short: I need to cite the formula, so what is it?

1

There are 1 answers

3
Forrest R. Stevens On BEST ANSWER

I assume that citing this excerpt here is ok by StackOverflow's terms of use? From page 390 (section 14.1) of Venables and Ripley (2002) you can find the definition that they use here for the acf() function:

enter image description here

If you look at the source code of the ccf() function in R (type "ccf" at the prompt) you can see how acf() is being used and can interrogate how the above acf() implementation relates to the calculation.