1

I have two vectors, a1 and a2. They're identical except for a certain lag:

enter image description here

I expected their cross-correlation to have a maximum of 1 at their lag. However that's not the case: their max cross-correlation is close to 1, but not quite (0.9998). (If I just remove the lag manually and correlate, I of course get a correlation of exactly 1.)

My intuitive understanding was that cross-correlation took two vectors, "slide one over" by a certain amount, and calculated a correlation. Clearly that's not the case, because I don't get a value of exactly 1. Why isn't the peak 1?

(This question is followup to another question I asked, where the discrepancy is much bigger, so I take this is a general feature of ccf.)

Code

# make vectors
a0 = runif(25)
a1 = c(rep(0.5, 2), a0)
a2 = c(a0, rep(0.5, 2))

# plot
require(ggplot2)
ggplot() + geom_line(aes(x=1:27, y=a1), color="red") + geom_line(aes(x=1:27, y=a2), color="blue")

# cross-correlate
tmp = ccf(a1, a2, type="correlation", plot=F)
print(paste("maximum correlation is", max(tmp$acf)))

R Greg Stacey
  • 2,202
  • 2
  • 15
  • 30
  • I'm not sure this is a duplicate. The first question asked why the location of the peak was different between ccf and my intuition. This question is specifically asking why the peak of ccf isn't 1. I edited this question to be more focused ("why isn't the peak 1?"). – R Greg Stacey Mar 30 '20 at 17:09

0 Answers0