Auto- and Partial Autocorrelation Matrices

Module for computing auto- and partial autocorrelation matrices
X = pd.DataFrame([[2,3,4],[5,6,7],[8,8,6],[9,10,3],[11,4,6]]); X
0 1 2
0 2 3 4
1 5 6 7
2 8 8 6
3 9 10 3
4 11 4 6
lag_uniform(X,1)
Variable 0 1 2 0 1 2
Time t t t t-1 t-1 t-1
0 5 6 7 2 3 4
1 8 8 6 5 6 7
2 9 10 3 8 8 6
3 11 4 6 9 10 3

Here we define the so-called imposter matrix, which will be used for comparing correlation matrices against randomly shuffled ones to determine when a correlation is better than a random one.


source

imposter_matrix

 imposter_matrix (X:pandas.core.frame.DataFrame, random_state=42)
Type Default Details
X DataFrame Matrix to be randomized
random_state int 42 Random state to be used
Returns DataFrame The input matrix with shuffled values for each column
imposter_matrix(X)
0 1 2
0 5 6 7
1 11 4 6
2 8 8 6
3 2 3 4
4 9 10 3

source

correlation_matrix

 correlation_matrix (X:pandas.core.frame.DataFrame,
                     y:pandas.core.frame.DataFrame)
Type Details
X DataFrame Matrix 1 for calculating the correlation matrix
y DataFrame Matrix 2 for calculating the correlation matrix
Returns ndarray Correlation matrix as a Numpy array
correlation_matrix(X,X)
array([[ 1.        ,  0.41978508,  0.0860663 ],
       [ 0.41978508,  1.        , -0.27628324],
       [ 0.0860663 , -0.27628324,  1.        ]])

source

ACM

 ACM (X:pandas.core.frame.DataFrame, lag:int)
Type Details
X DataFrame Dataframe of raw data for which to calculate the ACM
lag int Lag to calculate correlation against
Returns DataFrame Autocorrelation Matrix (ACM)
ACM(X,2)
0 1 2
t t t
0 t-2 0.981981 0.953821 0.500000
1 t-2 -0.654654 -0.563621 0.142857
2 t-2 0.000000 -0.114708 -0.755929

source

partial_correlation_matrix

 partial_correlation_matrix (X:pandas.core.frame.DataFrame)
Type Details
X DataFrame Matrix for calculating partial correlation
Returns ndarray Partial correlation matrix as a Numpy array
partial_correlation_matrix(X)
array([[ 1.        ,  0.46324708,  0.23162552],
       [ 0.46324708,  1.        , -0.34549142],
       [ 0.23162552, -0.34549142,  1.        ]])

source

PACM

 PACM (X:pandas.core.frame.DataFrame, lag:int)
Type Details
X DataFrame Matrix for calculating partial autocorrelation
lag int Lag to calculate partial correlation against
PACM(X,2)
Variable 0 1 2
Time t t t
Variable Time
0 t-2 -0.807661 -0.109185 0.375985
1 t-2 -0.606224 -0.387828 0.624375
2 t-2 0.018772 -0.876408 0.974483