I have two vectors, a1
and a2
. They're identical except for a certain lag:
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)))