By Jacques Joubert

About two weeks ago I decided to attempt to write a blog series on Pairs trading and statistical arbitrage. What I found is that everyone tends to reference the ADF test but I really don’t see a lot of posts that explain the test in full.

As you read about building a pairs trading strategy there is talk of testing a pair for co-integration and then you learn that they use ADF to do this. However, when I looked up tests for co-integration, the ADF test was not on the short list. Rather, an ADF test features under the topic: Testing for a Unit Root.

Fortunately for me, my wife studied actuarial sciences and my house is littered with books. I really thought it would be as easy as picking up a time series book and learning about ADF but that idea was shot down like the slow duck in the reeds. In short, I would have to read about 6 chapters of time series analysis before getting to a point where I can understand the different ways that you can use the ADF to test for a unit root and to use the ADF test in the context of stat arb.

If there are any readers that know of a resource that could explain the test in full, an article that tries to express to the reader in the most basic of terms, pretty please attach a link to the comments section. (Later on in the week I found this URL http://robotwealth.com/exploring-mean-reversion-and-cointegration-part-2/)

This week’s mantra: Mathematicians that can communicate concepts to the everyday man will go very far in life.

I eventually resorted to using the method explained in my QuantInsti EPAT lectures, this serves as a supplementary to their already written blog series, explained below:

### Step 1: Get data for two securities that you want to perform an ADF test on

In this example, I use a South African Listed company called Mondi that has two listings on the Johannesburg Stock Exchange.

### Step 2: Perform a linear regression on the two securities using a set number of observations

I will use 60 observations. Make sure you also request the residuals to be outputted. Note: When running this in a pairs trading strategy you will have to run the ADF test every day to make sure the Null hypothesis is rejected (Null hypothesis = there is a unit root).

Make sure you check Residuals

The X Variable Coefficient of 0.78255 is what we will be using for the hedge ratio.

## Step 3: Calculate the difference between the residuals

## Step 4: Calculate the t-1 residual in the next column

## Step 5: Perform a linear regression on the Delta and t-1 column

## Step 6: Compare the t-test statistic and the critical value

In order to reject the null hypothesis that there is a unit root present, the t-statistic in this case must be smaller than the critical value. The critical values for an ADF test have their own distribution and here is a snapshot of some of the critical values:

Source: Wikipedia

- We will be using the Critical value of -2.89, we have less than 100 observations
- The t stat is – 3.369
- Therefore the null hypothesis is rejected and we can say that the data are co-integrated

## Conclusion:

This test needs to be performed for every single data point and I would argue that this will not be done in excel. Therefore if you want to run a pairs trading strategy using a technique that includes testing for co-integration using an ADF test then I would recommend you to do so in say Python, R, C++, etc.

Note: Since writing this, one of the mentors from the EPAT program showed me how to run an ADF test in excel on every data point using the LINEST function, I would encourage others to check it out if your adamant on sticking with excel.

January 8, 2016

Eduardo GonzattiI used to run a macro every night after the markets closed in order to get a new list of cointegrated equity pairs.. The matrix was roughly 100 names X 250 data points, it took ~ 4h or so.. Eventually I had the time to search, learn and code parts of the whole workbook into matlab, the speedup was something like 4h – > 4s… Not exaggerating here.

All In all its perfectly fine to run it in VBA only, as long as you automate the process.

Very nice reading.. I had to discover/test /learn/implement all of this by myself some 5 yrs ago, it’s actually very (was?) hard to find a place where you can actually understand what the original poster/writer/coder is trying to communicate, as you say, mathematicians that can communicate will probably go very far in their careers..

Best

January 13, 2017

OlivierI have written a fast unit root tests library in C++ with wrappers for R and Python if it can help.

https://github.com/olmallet81/URT