Search code examples
pythonpandasstatsmodelsrolling-computation

Rolling statsmodels cointegration using pandas


I have a DataFrame with two Series and I know how to get their cointegration using all data points...

import pandas as pd
import numpy as np
import statsmodels.tsa.stattools as ts

A = pd.Series(np.cumsum(np.random.normal(size=100)) + 50)
B = pd.Series(A + 5 + np.random.normal(size=100))

ts.coint(A, B)

However, I'd like to explore how this cointegration has changed over time by using a rolling window (let's say 60 days). How can I achieve this using a combination of statsmodels and pandas?

Thanks in advance!


Solution

  • You can achieve this by first creating a dataframe, assigning an integer-position series, then use pandas rolling function with a lambda function that extracts the first element of ts.coint's return.

    So modifying your code we get:

    import pandas as pd
    import numpy as np
    import statsmodels.tsa.stattools as ts
    
    A = pd.Series(np.cumsum(np.random.normal(size=1000)) + 50, name='A')
    B = pd.Series(A + 5 + np.random.normal(size=1000), name='B')
    
    df = pd.concat([A, B], axis=1)
    df['ii'] = range(len(df))
    
    df['ii'].rolling(100).apply(lambda ii: ts.coint(df.loc[ii, 'A'], df.loc[ii, 'B'])[0])
    

    To illustrate this, I increased the size of the series to 1000 and set the rolling window to 100 (but you can play with the options in rolling).