How to calculate the autocorrelation function

224 Views Asked by At

I tried to do it but I obtain parts like: $$\int _{-\infty }^{\infty }cos\left[2\left(nw_0+\theta \right)\right]dn$$, that I supposed it has no sense because $$cos[2(nw_0+\theta)]$$ is oscillant...

1

There are 1 best solutions below

0
On

Here is R code to simulate the first $m = 10\,000$ steps of a 2-state Markov chain for weather $W$ $(0=$sun, $1=$rain$)$ in a place where weather on any one day is heavily influenced by weather the previous day. Over the long run, it rains $b/(a+b) = 1/3$ of the time, (poorly) estimated by $0.28$ over the simulated $m$ days. [Runs of rainy and sunny days are so long that estimation is difficult.]

set.seed(2020)
m=10000; n=1:m; a=.03; b=.06
w = numeric(m);  w[1]=0
for(i in 2:m) {
  if (w[i-1]==0) {w[i] = rbinom(1, 1, a)}
  else           {w[i] = rbinom(1, 1, 1-b)}
}
mean(w)
[1] 0.2837

par(mfrow=c(1,2))
plot(w[1:500], type="l")  # 'ell' not 'one'
acf(w)
par(mfrow=c(1,1))

The panel at left below shows successive values of $W$ the panel at right shows the ACF function of the $W$'s.

Roughly speaking the autocorrelation of lag $1$ is the correlation of the sequences $(W_1, W_2, \dots, W_{m-1})$ and $(W_2, W_3, \dots, W_m).$ The exception is that sample mean and variance use the entire sequence, not just the $m-1$ overlapping values. Autocorrelations of other lags $\ell$ are defined analogously:

$$r_{\ell} =\frac{ \sum_{i=1}^{m-\ell}(W_i - \bar W)(W_{i+\ell} - \bar W)}{(n-1)S^2}.$$

The ACF of a observed values $(W_1, \dots W_m)$ gives the list of autocorrelations for lags $\ell = 0, 1, 2, \dots.$

enter image description here

The autocorrelation of lag $0$ is $1.$ Autocorrelations decrease with $\ell.$ Autocorrelations within the dotted horizontal bands are considered not significantly different from $0.$

So by the end of a month the weather on the first day of the month no longer affects the current weather. (In an ergodic Markov Chain, Markovian one-step dependence 'wears off' over time.)