title: Efficient Cointegration Test in Python
body: <p>I am wondering if there is a better way to test if two variables are cointegrated than the following method:</p>
  
  <pre><code>import numpy as np
  import statsmodels.api as sm
  import statsmodels.tsa.stattools as ts
  
  y = np.random.normal(0,1, 250)
  x = np.random.normal(0,1, 250)
  
  def cointegration_test(y, x):
      # Step 1: regress on variable on the other 
      ols_result = sm.OLS(y, x).fit() 
      # Step 2: obtain the residual (ols_resuld.resid)
      # Step 3: apply Augmented Dickey-Fuller test to see whether 
      #        the residual is unit root    
      return ts.adfuller(ols_result.resid)
  </code></pre>
  
  <p>The above method works; however, it is not very efficient. When I run <code>sm.OLS</code>, a lot of things are calculated, not just the residuals, this of course increases the run time. I could of course write my own code that calculates just the residuals, but I don't think this will be very efficient either. </p>
  
  <p>I looking for either a build in test that just tests for cointegration directly. I was thinking <code>Pandas</code>, but don't seem to be able to find anything. Or maybe there is a clever to test for cointegration without running a regression, or some efficient method.</p>
  
  <p>I have to run a lot of cointegration tests, and it would nice to improve on my current method.</p>