-1

I'm trying to create the autocovariance for several lags on several rows. In this example I got two rows and 11 lags.

I got the following two rows in my example:

enter image description here

in text:

 array([[164, 148, 152, 144, 155, 125, 153, 146, 138, 190, 192, 192],
   [239, 379, 105, 150, 400, 326, 134, 441, 199, 431, 203, 425]], dtype=int64)

I want to get the autocovariance for every row with lag 0,1...11.

For now, I got the following code:

 import statsmodels as sm
 import numpy as np
 import pandas as pd
 df = pd.read_excel("directory\\file.xlsx")

def autocov(row):
    x = sm.tsa.stattools.acovf(df.T[row], unbiased=False, demean=True, 
    fft=None, missing='none')
    autocov_df = pd.DataFrame(x)

for index,row in df.iterrows():
     print(x)

This prints me the following array:

output

in text:

 [ 447.52083333  191.93229167   94.51041667  -26.36979167  -87.58333333
   -97.90104167 -121.86458333  -54.328125    -94.47916667  -31.19270833
   -12.65625      16.171875  ]
 [ 447.52083333  191.93229167   94.51041667  -26.36979167  -87.58333333
   -97.90104167 -121.86458333  -54.328125    -94.47916667  -31.19270833
  -12.65625      16.171875  ]

However, as you can see, it prints the same values for both 2 rows.

What am I doing wrong in my code???

This is my preferred output:

[ 447.52083333  191.93229167   94.51041667  -26.36979167  -87.58333333
  -97.90104167 -121.86458333  -54.328125    -94.47916667  -31.19270833
  -12.65625      16.171875  ]
[ 14887., -7237., 1811.,-198.5,
  2903.08333333,  -3346.41666667,   1140.33333333,  -1207.25      ,
  1141.08333333,  -3307.75      ,   1402.33333333,   -544.41666667]
0

1 Answer 1

1

You are iterating with index, row but you print constant value:

for index,row in df.iterrows():
    print(autocov_df.T)

autocov_df.T does not depend on index or row.

You need to use iteration variable to see difference, like:

for index,row in df.iterrows():
    y = some_function(index,row)
    print(y)

In your example you don't call autocov and there is no return statement in that function:

def autocov(row):
    x = sm.tsa.stattools.acovf(df.T[row], unbiased=False, demean=True, 
    fft=None, missing='none')
    autocov_df = pd.DataFrame(x)
    return(autocov_df)

for index,row in df.iterrows():
     x = autocov(index)
     print(x)

Note that your parameter naming could be misleading.

4
  • What would you like to print?
    – Frane
    Dec 10, 2018 at 11:51
  • I edited my question with the preferred output. This is what I want to achieve (my df will eventually have thousands of rows) Dec 10, 2018 at 11:54
  • I tried your solution and embedded it in my code like this: for index,row in df.iterrows(): print(row) print(autocov_df) It gives me the same results however Dec 10, 2018 at 12:05
  • Answer is updated. Needed to import statsmodels.tsa.stattools to run example.
    – Frane
    Dec 10, 2018 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.