# Why does cross-correlation function ccf in R have nonzero values?

When I try to compute the cross-correlation function between two vectors c(0, 0, 0, 1, 0, 0) and c(0, 1, 0, 0, 0, 0) via

ccf(c(0, 0, 0, 1, 0, 0), c(0, 1, 0, 0, 0, 0))


I get the following result:

Here all values are nonzero, which seems strange for me, because for most lags, when I multiply two vectors (for example for zero lag), I should get zero, because where one vector has nonzero element, another has zero and vice versa. Why do I get some nonzero results on the graph where I should get zero?

• Hint: you can find a lag where there is at least one term without any zeros in its factors. Don't forget that correlation concerns the residuals after means are subtracted. There's probably also a technical issue similar to that discussed at stats.stackexchange.com/questions/81754; namely, there are several slightly different formulas for the ccf in use. – whuber May 27 '19 at 15:07
• Yes, I had a lack of understanding how cross-correlation is computed, I didn't know that it is computed for mean-substracted values. When I computed ccf with vectors c(-1, 0, 0, 1, 0, 0) and c(0, 1, 0, 0, 0, -1) (which have zero means), I got nonzero elements at lags 1, 2, 3 and everywhere else it is zero, how should be. – bastak May 27 '19 at 16:29
• Some people (typically in engineering fields) do not subtract the means in their definition, often because they assume a zero-mean process anyway. That difference has little impact with the typical long time series ("signals") they work with, but it can have a huge difference with tiny example data like these. – whuber May 27 '19 at 21:48