简体   繁体   中英

How to run a script in python on multiple time series?

I am trying to run the following script using the StatsModels library :

cadf = ts.adfuller(df1.spread)
print 'Augmented Dickey Fuller test statistic =',cadf[0]
print 'Augmented Dickey Fuller p-value =',cadf[1]
print 'Augmented Dickey Fuller 1%, 5% and 10% test statistics =',cadf[4]

on my Data frame , my data-frame currently comprises of multiple time series (200+) , and I would like to run the above script on every time series . Below is an example of the dataframe :

Sn.No   A-B      B-C         A-C
  1       10       11.15       13.22
  2       11       12.36       14.75
  3       10.75    11.54       14.21

So , I would like to run the ADF test script on AB,BC and AC and get the test statistic/output for each of them .

To access columns individually, instead of the entire data frame, basically:

df1['A-B']

will get you the first column. Round them up in a list:

columns = ['A-B', 'B-C', 'A-C']

If you have 200+ columns you should access them differently. Let's say you have 200. Something like this should be possible:

columns = range(200)

In that case you don't need the xrange function below, but like so:

df1.iloc(column)

The reason I know this is because I looked up the source code for dataframe in the statmodels library , which apparently is a Pandas dataframe . Assuming you have to enter the spread as in the above example, gather the output statistics in a list using a list comprehension expression.

cadfs = [ts.adfuller(df1[column].spread) for column in columns]

Instead, if you are accessing by numerical index:

cadfs = [ts.adfuller(df1.iloc(column).spread) for column in columns]

To make the code more readable use local constants.

TEST = 0    
P = 1
PERCENTILES = 4

values = [cadfs[column][TEST] for column in xrange(columns)]
ps     = [cadfs[column][P] for column in xrange(columns)]
pct    = [cadfs[column][PERCENTILES] for column in xrange(columns)]

print 'Augmented Dickey Fuller Test'
print 'ADF Test Statistics - p - Percentiles 1%, 5% and 10%'
print values, ps, pct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM