My cross-correlation co-efficient is exceed the limit of -1 and 1.

I am trying to cross-correlation two time series (monthly observations), but it often give cross-correlation co-efficient greater or less then 1 or -1.

Here is an example of 3 year data record and its mean cross-correlation plot.

[There are many answer available but none of them help me out. I just want to know either this error s belong to data set or approach i adopted]

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from dateutil.relativedelta import relativedelta
from scipy import signal


np.random.seed(1)
tidx = pd.date_range('1975-01-01', periods=36,  freq='MS') 
tim=pd.to_datetime(tidx)
df_t=pd.DataFrame(tim, columns=['time']) # time series 
p1=pd.DataFrame([13, 17, 13, 15, 9, 9, 16, 11, 11, 16, 11, 10, 23, 19, 23, 9, 3, 6, 4, 27, 8, 16, 
                 26, 17, 16, 15, 16,  8, 36,  9,  9, 11, 11, 16,  7,  5], columns=['a'])
p2=pd.DataFrame([1182.7,1191.5,1206.1,1259.6,1351.2,1473.2,1531.6,1531.2,1531.4,1530.7,1527.8,1526.9,
                 1526.9, 1520.3, 1511.9, 1501.7, 1491.7, 1516.8, 1526.1, 1541.7, 1541.7, 1530.7,
                 1518.4, 1490.6, 1479.1, 1465.4, 1409.1, 1329.5, 1302.4, 1379.2, 1520.9, 1545.6, 
                 1550.0,  1539.1, 1498.7, 1468.4],columns=['b'])

df_final=pd.concat([df_t, p1, p2], axis=1)

fig, ax1  = plt.subplots(figsize=(10,4), sharey=False, dpi=300)
t1=df_final['time'].iloc[0]
cr=[]
lag=[]

for i in range(0, 36, 12):
    t2=t1+relativedelta(months=i)
    t3=t1+ relativedelta(months=i+12)
    sub=df_final[(df_final.time>=t2) & (df_final.time<t3)]
    w_sub=sub[['time', 'a']]
    e_sub=sub[['time', 'b']]
    wl=w_sub['a'] - np.mean(w_sub['a'])
    eq=e_sub['b']- np.mean(e_sub['b'])
    wl=wl/np.max(wl)
    eq=eq/np.max(eq)
    corr = scipy.signal.correlate(wl-np.mean(wl), eq-np.mean(eq), mode='same', method='direct')
    lags = signal.correlation_lags(len(wl), len(eq), mode="same")
    cr.append(corr)
cc=pd.DataFrame(cr)
ccc=np.mean(cc)
lll=np.linspace(0, 11, 12)
ax1.plot(lll, ccc, 'red')

Here is the result (cc is lower than -1)

enter image description here

0

There are 0 best solutions below