Time Series and Trend Analysis
By: Alex Mitrani
Tags: Machine Learning, Time Series Analysis, Visualization, Data Science, Timeseries

Time-dependent trends are a unique feature of time series analysis. If the sequence of events matters, then you need to analyze possible trends. These trends can ultimately be used for creating models that predict future values. I recently published articles about working with time series data and creating OHLC (open-high-low-close) charts in python; I will be continuing our discussion here.

Trends and Stationarity

Time series models work on the assumption that the series to be analyzed is stationary, or has a mean, variance, and covariance that are not functions of time. It is extremely rare that you will load a time series dataset that fulfills all three principles of stationarity, you will have to remove these trends to achieve this goal. You will still retain the valuable information of time dependent mean, variance, and covariance, they will be applied later on. Once these three factors are satisfied then you can move on to applying time series models to your dataset.

An illustration of the principles of stationarity, Source: BeingDatum

The above image illustrates the three principles of stationarity:

Mean: The mean of the time series should not be a function of time, whether throughout the time series or during specific periods (seasonal). Image B depicts a series where the mean is growing over time and Image A depicts a series where the mean is constant throughout the time frame. A seasonal time dependent mean also counts against stationarity. The observations may oscillate around a mean but they cannot fluctuate as a function of time for the series to be considered stationary. Variance (Homoscedasticity): The variance of the observations must be constant throughout the time series. Image C depicts a series where the variance of the observations is a function of time, Image A depicts a series where the variance is constant throughout the series. Covariance: The covariance between two observations over a consistent time interval is not a function of time. In Image D the covariance between two variables fluctuates over time; it is smaller towards the middle compared to the rest of the series. The covariance is constant throughout the same interval in Image A.

Only Image A depicts a time series that fits the principles of stationarity.

Dickey-Fuller Test

One useful statistical test to check for stationarity is the Dickey-Fuller Test. In this test the null hypothesis is that the given time series is not stationary and the alternative hypothesis is that the series is stationary. A time interval is selected to calculate the series’ rolling mean and rolling standard deviation. If the p-value falls below the critical value then we reject the null hypothesis.

CPB Example

I revisited the data obtained from the Campbell’s Soup Company in my previous articles and used python coding to analyze the series. I utilized the API from unibit.ai to obtain the data and I stored it in a pandas dataframe. The year to date price data is below:

The year to date stock price of CPB, Source: unibit.ai

I selected a time interval of 20 days because that is how many trading days there are in a month. I then utilized panda’s .rolling() method to obtain the rolling mean and variance of the series. I then utilized StatsModel’s implementation of the Dickey-Fuller Test. The results were a p-value of 0.7469 which means that I cannot reject the null hypothesis (the time series is not stationary). A visualization of the rolling mean and variance for Campbell’s price is below:

A visualization of the YTD rolling mean and variance of Campbell’s adjusted closing price. The rolling mean has a downward trend while the rolling standard deviation only has slight anomalies in an otherwise consistent series.

The notebook I created for this article is hosted on my GitHub profile here.

Summary

The principles of stationarity are central to time series analysis. Once we identify and remove specific trends we can then utilize powerful machine learning models that are designed for time series data. Python’s StatsModels library has an easy to implement Dickey-Fuller Test to check for stationarity.