Home > Blockchain >  cross correlation plot in statsmodels
cross correlation plot in statsmodels

Time:11-15

Consider the simple example below, borrowed from enter image description here

However, this seems to be a lot of code for something that that conceptually is very simple (just appending two lists). Can this code be streamlined?

Thanks!

CodePudding user response:

Well, you can try "just appending to lists":

# also
# cc = list(backards)   list(forwards[1:])
cc = np.concatenate([backwards, forwards[1:]])
full = pd.DataFrame({'lag':np.arange(len(cc))-len(backwards), 
                     'value':cc})
full.plot(x='lag')

Also:

full = (pd.DataFrame({'value':np.concatenate([backwards, forwards[1:]])})
          .assign(lag=lambda x: x.index - len(backwards) )
       )

Output:

enter image description here

Note if all you want is to plot the two arrays, then this would do

plt.plot(-np.arange(len(backwards)), backwards, c='C0')
plt.plot(forwards, c='C0')
  • Related