0

I'm trying to understand the autocorrelation better and wrote an auto correlation function based on the algorithm suggested in the correlogram wiki-page (https://en.wikipedia.org/wiki/Correlogram), I have the following questions;

1- if I use the $$c_h = \frac{1}{N-h} \sum_{t=1}^{N-h} (Y_t-\bar{Y})(Y_{t+h}-\bar{Y})$$ algorithm I get the following image which has an unexpected feature at the end, why do I get such behaviour at the end?Auto correlation as a functopn of delay h

2- if I use the other suggested algorithm $$c_h = \frac{1}{N} \sum_{t=1}^{N-h} (Y_t-\bar{Y})(Y_{t+h}-\bar{Y})$$ I get another unexpected behaviour, which is expected do to the decrease of sum and it's decision by a constant number $N$, is this behaviour reliable?, as this one is recommended in the WIKIPEDIA article.Auto correlation as a functopn of delay h

The python code is the following for the first one;

import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0.05,30,0.05)
y1 = np.sin(x*(2*np.pi))
def c(delt):
    c = 0
    for i in range(len(y1)-delt):
        try:
            c = c + (y1[i]-np.mean(y1))*(y1[i+delt]-np.mean(y1))
        except IndexError:
            break
    c = c/(len(y1)-delt)
    return c

c0=0
for i in range(len(y1)):
    c0 = c0 + (y1[i]-np.mean(y1))*(y1[i]-np.mean(y1))
c0 = c0/len(y1)


R = [(c(i)/c0) for i in range(len(y1))]
plt.plot(R)

and for the second is;

import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0.05,30,0.05)
y1 = np.sin(x*(2*np.pi))
def c(delt):
    c = 0
    for i in range(len(y1)-delt):
        try:
            c = c + (y1[i]-np.mean(y1))*(y1[i+delt]-np.mean(y1))
        except IndexError:
            break
    c = c/(len(y1))
    return c

c0=0
for i in range(len(y1)):
    c0 = c0 + (y1[i]-np.mean(y1))*(y1[i]-np.mean(y1))
c0 = c0/len(y1)


R = [(c(i)/c0) for i in range(len(y1))]
Amir
  • 1

1 Answers1

0

The first problem was due to the shape of the $x$ vector and the second one is also a proper way of defining the Autocorrelation as I fit an $exponential$ function to the $\exp(-t/\tau) \times \sin(x)$ and was able to observe the properties of the decay and validate the decay timeconstant.

Amir
  • 1