In the previous segment, you learnt about the importance of stationarity and a couple of formal tests to detect it. In this segment, we will learn the ways to convert a non-stationary series into a stationary series to build an Auto Regressive model.

The two tools to convert a non-stationary series into stationary series are as follows:

- Differencing
- Transformation

Let’s study these tools in detail.

To remove the trend (to make the mean constant) in a time series you use the technique called **differencing**. As the name suggests, in differencing you compute the differences between consecutive observations. Differencing stabilises the mean of a time series by removing changes in the level of a time series and therefore eliminating (or reducing) trend and seasonality.

For Example:

Original time series | After 1st order differencing | After 2nd order differencing |

50 | ||

180 | 130 | |

420 | 240 | 110 |

770 | 350 | 110 |

1240 | 470 | 120 |

1820 | 580 | 110 |

Here 1st order differencing is calculated by the difference of two consecutive observations of the original time series. 2nd order differencing is calculated by the difference of two consecutive observations of the 1st order differenced series.

At the first difference level, it still has a linear trend but we have successfully removed the higher-order trend observed in the original time series. After the 2nd difference level, the series obtained is mostly a horizontal line. There is no overall visible trend in this series and thus you can say the series obtained is a stationary series.

The other method to introduce the stationarity is making the variance constant. There can be many transformation methods used to make a non-stationary series stationary but here, we are discussing the Box-Cox transformation.

The mathematical formulae that Box-cox transformation is:

where y is the original time series and y(λ) is the transformed series. The procedure for the Box-Cox transformation is to find the optimal value of λ between -5 and 5 that minimizes the variance of the transformed data (*The formula is not important to remember; the main thing you should know is how to implement this using Python which you will learn in the next video.).*

Below is the time series data on which Box-Cox transformation is implemented with λ = 0.2 as shown in the demonstration above.

y(t) | 2 | 5 | 4 | 11 | 15 | 30 | 28 | 60 | 56 | 110 | 60 | 120 | 240 | 200 | 150 |

y(0.2) | 0.74 | 1.89 | 1.59 | 3.07 | 3.59 | 4.87 | 4.73 | 6.33 | 6.18 | 7.80 | 6.33 | 8.02 | 9.96 | 9.42 | 8.62 |

Here we clearly see after the Box-Cox transformation the variance has become more or less constant or stabilised but still has a trend. Again, this trend can be removed by differencing.

Now that you know the methods to convert a non-stationary series into a stationary series, let us convert the airline passenger dataset which is a non-stationary series into a stationary series.

In this segment, you studied the methods to convert a non-stationary series into a stationary series. Now, it is time for you to answer a few questions on these topics.