Search code examples
pythonmatplotlibstatisticsstatsmodels

How to plot confidence intervals for stattools ccf function?


I am computing the cross-correlation function using ccf from statsmodels. It works fine except I can't see how to also plot the confidence intervals. I notice that acf seems to have much more functionality. Here is a toy example just to have something to see:

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.tsa.stattools as stattools

def create(n):
    x = np.zeros(n)
    for i in range(1, n):
        if np.random.rand() < 0.9:
            if np.random.rand() < 0.5:
                x[i] = x[i-1] + 1
        else:
            x[i] = np.random.randint(0,100)
    return x
x = create(4000)
y = create(4000)
plt.plot(stattools.ccf(x, y)[:100])

This gives:

enter image description here


Solution

  • Unfortunately, the confidence interval is not provided by the statsmodels cross-correlation function (ccf). In R the ccf() would also print the confidence interval.

    Here, we need to calculate the confidence interval by ourself and plot it out afterwards. The confidence interval is here computed as 2 / np.sqrt(lags). For basic info on confidence intervals for cross-correlation refer to:

    import numpy as np
    import matplotlib.pyplot as plt
    import statsmodels.tsa.stattools as stattools
    
    def create(n):
        x = np.zeros(n)
        for i in range(1, n):
            if np.random.rand() < 0.9:
                if np.random.rand() < 0.5:
                    x[i] = x[i-1] + 1
            else:
                x[i] = np.random.randint(0,100)
        return x
    x = create(4000)
    y = create(4000)
    
    lags= 4000
    sl = 2 / np.sqrt(lags)
    
    plt.plot(x, list(np.ones(lags) * sl), color='r')
    plt.plot(x, list(np.ones(lags) * -sl), color='r')
    
    plt.plot(stattools.ccf(x, y)[:100])
    

    This leads to the following plot with the additional red lines: CI Plot