""" Name : c8_36_Durbin_WatSon_.py Book : Python for Finance (2nd ed.) Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 6/6/2017 email : yany@canisius.edu paulyxy@hotmail.com """ import pandas as pd from scipy import stats import statsmodels.formula.api as sm import statsmodels.stats.stattools as tools from matplotlib.finance import quotes_historical_yahoo_ochl as getData # begdate=(2012,1,1) enddate=(2016,12,31) # def dailyRet(ticker,begdate,enddate): p =getData(ticker, begdate, enddate,asobject=True,adjusted=True) return p.aclose[1:]/p.aclose[:-1]-1 retIBM=dailyRet('IBM',begdate,enddate) retMkt=dailyRet('^GSPC',begdate,enddate) df = pd.DataFrame({"Y":retIBM, "X": retMkt}) result = sm.ols(formula="Y ~X", data=df).fit() print(result.params) residuals=result.resid print("Durbin Watson") print(tools.durbin_watson(residuals))