views:

167

answers:

1

Hello everybody!

Thank you in advance for your help!

I have two lists with some stocks prices, example:

a = [10.23, 11.65, 12.36, 12.96]

b = [5.23, 6.10, 8.3, 4.98]

I can calculate the correlation of these two lists, with:

import scipy.stats

scipy.stats.pearsonr(a, b)[0]

But, I didn't found a method to calculate the co-integration of two lists.

Could you give me some advices?

Thank you very much!

+2  A: 

I don't believe a cointegration test has been implemented for scipy. You may be better off using rpy2 to interface Python with R. R provides cointegration tests in the urca package.

For example:

import rpy2.robjects as ro
r=ro.r

a = [10.23, 11.65, 12.36, 12.96]
b = [5.23, 6.10, 8.3, 4.98]

Define a and b in R:

ro.globalEnv['a']=ro.FloatVector(a)
ro.globalEnv['b']=ro.FloatVector(b)

Call the R cor (correlation) function:

print(r('cor(a,b,method="pearson")'))
# [1] 0.2438518

Call the R ca.po (Phillips & Ouliaris Cointegration Test)

r('library(urca)')
print(r('ca.po(cbind(a,b))'))
# ######################################################## 
# # Phillips and Ouliaris Unit Root / Cointegration Test # 
# ######################################################## 

# The value of the test statistic is: 0 

I'm not familiar with cointegration, however, so apologies if my use of ca.po is totally inept.

Also note that R is programming language unto itself, with (at least currently) a richer library of statistical functions than scipy. It's possible to run R directly (without Python). The calls would look a little simpler:

> a = c(10.23, 11.65, 12.36, 12.96)
> b = c(5.23, 6.10, 8.3, 4.98)
> z = cbind(a,b)
> z
         a    b
[1,] 10.23 5.23
[2,] 11.65 6.10
[3,] 12.36 8.30
[4,] 12.96 4.98
> ca.po(z)

######################################################## 
# Phillips and Ouliaris Unit Root / Cointegration Test # 
######################################################## 

The value of the test statistic is: 0 
unutbu
Please, could you give me an example?
Damiano