Cointegration and the VECM
Today let's understand how two prices that each wander can still be tied together, and how to model that tie.
Let's say you are an analyst at a grain co-op. Every trading day you record the wheat price at two elevators, one inland and one on the river, and you now have 250 days of both.
Plot them and each price does the same thing: it drifts around and never settles at one level. Over the 250 days, each price ranges over more than 2 dollars.
But the two prices are clearly related. The gap between them, called the spread, stays close to the same value all the time.
So here is the question: how do you model two prices that each wander, but stay tied together? The answer has three parts.
That is the whole plan. Everything from here on fills in one of the three boxes, using these two wheat prices.
How to tell a non-stationary price from a stationary one
Cointegration is about series that wander, so let's first be precise about what wandering means.
A series is stationary when its mean and its variance stay the same over time. It keeps coming back to the same level. A non-stationary series does not: it can drift away from where it started and stay away, so its mean is not constant.
Let's build the prices we will use for the whole lesson. The block simulates 250 days of inland and river wheat prices, plus a coffee price that is simulated on its own and has nothing to do with wheat. The simulation builds in a freight gap of 0.40 dollars between the two wheat prices and a daily pull back toward it, so later we can check our estimates against those values.
Each row is one trading day. Now let's plot the two wheat prices and print how far each one ranges.
Both prices drift down over the 250 days, and neither has a fixed mean. Inland moves between 4.81 and 7.04 dollars, and river between 4.41 and 6.67 dollars.
But a plot is only an impression, so let's test it. The augmented Dickey-Fuller (ADF) test asks whether a series has a unit root: today's value is yesterday's value plus a random change, so a shock never fades out. That is its null hypothesis, and a small p-value rejects it. The adf.test() function in the tseries package runs it, and it also allows for a linear trend.
We run it on each price and on its daily changes, meaning today's price minus yesterday's. The prices themselves are called the levels.
On the levels the p-values are 0.275, 0.317 and 0.536, all well above 0.05, so none of the three prices can rule out a unit root. On the daily changes the p-value is 0.01 for all three. That is the smallest value the function reports, so the true p-value is at most 0.01 and the changes are stationary.
A series that becomes stationary after differencing once is called integrated of order 1, written I(1). So inland, river and coffee are all I(1).