It’s also important to decide how we define when two stocks are diverging from there spread. For this, we want to choose a threshold. If the spread exceeds the threshold, then we can assume that the spread is temporary and will revert back to its historical mean. We can have two kinds of thresholds. One threshold, tells us that the spread is much wider than its historical average. Another threshold, can tell us that the spread is much narrower than its historical average. When the spread is unusually wide, the action we take is called shorting the spread. We say we’re shorting the spread, because we expect the spread to get smaller in the future. Conversely, when the spread is unusually narrow, the action we take is called going long the spread. We say we’re going long the spread or buying the spread because we expect the spread to get larger in the future. Let’s discuss what it means to go long or short spread. When the spread widens, we decide to short the spread. This means, that we short the asset that has increased relative to the spread. We also buy the asset that has declined relative to the spread. Some of our previous examples involved shorting the spread. Conversely, when we see this spread narrow, we decided to go long the spread. This means that we buy the asset that has declined relative to the spread. We also short the asset that has increased relative to the spread. If this concept sounds a bit confusing, just to remember to buy low and sell high. In other words, buy when it’s on sale, and sell when it’s overpriced. The way we define these thresholds, is by calculating how many standard deviations the spread is from its historical average. This is called the Z-score of spread. A Z-score of positive one, means that a value is one standard deviation greater than its historical mean. To calculate the Z-score, we subtracted the mean and divide by the standard deviation. As with all modeling, is necessary to perform backtesting. By backtesting, I mean that we want to test our model on historical data. We want to test in a way that simulates the model making predictions in real time. We don’t want to expose the model to data that occurs in the future. To perform backtesting, we divide our historical data into a training set, a validation set, and a test set. The training set has earlier data up to a cutoff date, the validation set has data that occurs after the training set up to and even later cutoff date, the test set contains data that occurs after the validation set. We train our model on the training set, then do intermediate checks using the validation set. This allows us to adjust our model to help it perform more accurate estimates. When we’ve adjusted our model to the point that we think it’s ready to use, we then check it against the test set to see if it really works. Note that, it’s important not to use the test set until we’re done optimizing the model. If we were to adjust the model after using the final test set, this would be like letting our model cheat by showing it information about the future.