-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcointegration.py
More file actions
44 lines (29 loc) · 1.25 KB
/
cointegration.py
File metadata and controls
44 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
from stock import Stock
class CointegratedAssets(object):
# The "CointegratedAssets" class implements the Engle-Granger approach
# to cointegrated time series.
def __init__(self,assets):
self.price_series = np.atleast_2d([asset.asset_closing_prices() for asset in assets]).T
self.dependent = self.price_series[:,0].T
self.independent = self.price_series[:,1:]
self.engle_granger = {}
self.engle_granger["step_one"] = self.engle_granger_step_one()
if self.engle_granger_cointegration_test():
print "The Engle-Granger test reports that cointegration exists between the time-series."
else:
print "The Engle-Granger test reports that cointegration does not exist between the time-series."
def __str__(self):
return "Cointegration assets"
def engle_granger_step_one(self):
constant = np.ones((self.independent.shape[0],1))
covariates = np.concatenate((constant,self.independent),axis = 1)
theta = np.linalg.lstsq(covariates,self.dependent)[0]
residuals = self.independent - np.dot(covariates,theta)
return {"theta" : theta,"residuals" : residuals}
def engle_granger_step_two(self):
pass
def engle_granger_cointegration_test(self):
pass
assets = [Stock("MSFT"),Stock("GOOG")]
ca = CointegratedAssets(assets)