From sgiannerini at gmail.com  Mon Oct  2 13:37:07 2006
From: sgiannerini at gmail.com (Simone Giannerini)
Date: Mon, 2 Oct 2006 13:37:07 +0200
Subject: [Rd] CCF and ACF
Message-ID: <3c12769c0610020437m408fba35q438ffdd147212f49@mail.gmail.com>

Dear all,

 given two numeric vectors x and y, the ACF(x) at lag k is
cor(x(t),x(t+k)) while the CCF(x,y) at lag k is cor(x(t),y(t-k)). See
below for a simple example.

 > set.seed(1)
 > x <- rnorm(10)
 > y <- rnorm(10)
> x
 [1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 -0.8204684
 0.4874291  0.7383247  0.5757814 -0.3053884
> y
 [1]  1.51178117  0.38984324 -0.62124058 -2.21469989  1.12493092
-0.04493361 -0.01619026  0.94383621  0.82122120  0.59390132
> x <- x - mean(x);
> y <- y -mean(y);

   ## ACF OF x(t)
> print(acf(x,lag=2,plot=F),digits=5))

 Autocorrelations of series 'x', by lag

        0        1        2
 1.00000 -0.26486 -0.25352

  ## For Instance ACF(x) at lag 1 is Corr(x(t),x(t+1)):

  > sum(x[1:9]*x[2:10])/sum(x^2)
[1] -0.2648633

  ## whereas CCF(x,y) at lag 1 is Corr(x(t),y(t-1))
 > ccf(x,y,lag=2,plot=F)

Autocorrelations of series 'X', by lag

    -2     -1      0      1      2
   -0.139  0.593 -0.377 -0.382  0.116

 > sum(x[1:9]*y[2:10])/sqrt(sum(x^2)*sum(y^2))

[1] 0.5930216

> sum(x[2:10]*y[1:9])/sqrt(sum(x^2)*sum(y^2))
 [1] -0.3822885

from a quick survey on textbooks (Brockwell and Davis, Chatfield,
Fuller) it looks like different authors use different conventions so
that I think that it would be nice to clarify this in the
documentation.

Ciao

Simone

> R.version
               _
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
status         beta
major          2
minor          4.0
year           2006
month          09
day            20
svn rev        39421
language       R
version.string R version 2.4.0 beta (2006-09-20 r39421)

> Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United  States.1252"


______________________________________________________

Simone Giannerini
 Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
 Tel: +39 051 2098262  Fax: +39 051 232153




[Rd] CCF and ACF
Simone Giannerini