Introduction to Time Series — Trend Decomposition with Python

Multipart series on time series analysis with Python applied to financial datasets

Amir Nejad
QuantJam
Published in
6 min readNov 2, 2020

--

Photo by Pixabay from Pexels

Source code of this article can be downloaded from Github: Link

Time Series

A time series is a series of data points indexed in time order. Time series resolution is the frequency that data is recorded. For example, heart rate measurements (in units of beats per minute) occur at 0.5 second intervals, so that the length of each series is exactly 15 minutes. Stock data resolution can have different resolution depending on frequency of which data is recorded like: second, minute, hour etc.

Four examples of time series data

Time Series Patterns

Time series generally can have different shapes and forms but in general time series have 3 distinct patterns or components:

  1. Trend exists when there is a long-term increase or decrease in the data. The trend can be linear or non-linear.
  2. Seasonality patterns occur when a time series is affected by seasonal factors such as the time of the year or the day of the week. Seasonality is always of a fixed and known frequency. The monthly sales of antidiabetic drugs above show seasonality which is induced partly by the change in the cost of the drugs at the end of the calendar year.
  3. Cycle patterns occur when the data exhibit rises and falls that are not of a fixed frequency. These fluctuations are usually due to economic conditions and are often related to the “business cycle”. The duration of these fluctuations is usually at least 2 years.
  4. Random/Unexplained is the rest of the time series after trend, seasonality and cyclic trends which cannot be explained in a consistent fashion.

In this article we are going to go over seasonality trends in the time series data. The cyclic behavior of the time series will be discussed in the further articles of this publication series.

Pattern Decomposition using Python

In this section we attempt to decompose a time series to trend, seasonality, cycle and random components using Python. statsmodels is a premier Python module for the estimation of many different statistical models, as well as for conducting statistical tests, and statistical data exploration [more info here]. The time series analysis module statsmodels.tsa contains model classes and functions that are useful for time series analysis.

There are two main methods among other methods to decompose seasonality: linear models such as statsmodels.tsa.seasonal.seasonal_decompose with additive or multiplicative models, and season-trend decomposition using LOESS using statsmodels.tsa.seasonal.STL.

The seasonal_decompose model uses moving averages to decompose seasonality trends. The additive models has following format:

The multiplicative time series model has the following format:

The additive model assumes linear trend behavior whereas multiplicative model increasing or decreasing amplitude and/or frequency over time. The advantage of the seaonal_decompose model is explainability and the constant pattern extraction. This method is a naïve decomposition and often more sophisticated method such as STL is preferred to decompose time series. The STL methods takes advantage of LOESS (Local regression) a nonparametric technique that uses local weighted regression to fit a smooth curve through points in a scatter plot. The seasonal component is allowed to change over time, and the rate of change can be controlled by the user. The following examples demonstrates both aforementioned discussed models.

Example 1: Airline Passenger Data

The Airline Passengers dataset describes a total number of airline passengers over time. As it can be seen, the number of airline passengers in general is increasing over time. However, it is evident that there is a rise and fall pattern within the number of passengers traveled each year.

Airline passenger data

Using seaonal_decompose in this case is appropriate. The model type used in this case is additive with period of 12 since the dataset is monthly data.

The resulting plot of seasonality can be seen from the following two graphs. Overall trend of time series as expected is linearly increasing. The seasonal patterns demonstrate increase in passengers from January to August followed by sharp decline from August to November. The peak of travel each year according to this dataset is summertime.

Airline passenger data seasonality decomposition (left) and typical monthly seasonal changes (right)

Example 2: Natural Gas Price Data

The time series that is selected for this section is the monthly historical price of natural gas as it can be seen from the chart below (data source: EIA). As it can be seen, this time series do not appear to have constant seasonality trend therefore using seaonal_decompose is not appropriate.

US Natural gas price per million BTU ($/BTU) — Data source: EIA.gov

The STL function uses LOESS (locally estimated scatterplot smoothing) to extract smooths estimates of the three components. The key inputs into STL are:

  • season : The length of the seasonal smoother. Must be odd number.
  • period: Periodicity of the sequence.

The seasonal decomposed trend of natural gas prices using STL can be seen from the following figure. As it can be seen, the magnitude of the seasonality is changing and not constant as opposed to seasonal_decompose method. Natural gas prices seem to be affected by seasonal trends during the years of 2000–2010 and lowest 2012–2016. Several fundamental factors can contribute to the change of seasonality of natural gas prices such as increase in gas production from shale reservoirs.

Seasonal trend of natural gas prices using STL

The seasonal changes per year and per month can be seen from the following figures. The seasonal effects have nonlinear patterns with peaks in the month of June and December. Seasonal effects have several historical periods of contractions and expansion.

Seasonal effects on natural gas prices by month (left) by year (right)

For more in-depth information about exploring seasonality of stock market data, please refer to following QuantJam article:

Key Takeaways

Analyzing chart seasonality can provide insights in time series analysis. The main package for decomposing a time series to seasonal and trends is Statsmodels. seasonal_deompose assumes a model with particular formula (either additive or multiplicative). The STL methods takes advantage of LOESS (Local regression) and it is preferred method of choice for most time series seasonality analysis tasks. Stay tuned and follow us to get notified on the future episodes.

Thanks for reading! My name is Amir Nejad,PhD. I’m a data scientist and editor of QuantJam , and I love share my ideas and to collaborate with other fellow data scientists. You can connect with me on Github, Twitter, and LinkedIn.

QuantJam:

You can see my other writings at:

References

About QuantJam

Quantjam is a medium publishing company aims at sharing reproducible research focused on finance and stock market data.

Follow us to stay up-to-date on all articles and activities: https://medium.com/quantjam

Disclaimer

QuantJam writings do not include any investment advice. Past performance is no guarantee of future results. Please consult with your financial advisor before making any investment decision. Investing involves risk and you may incur a profit or loss regardless of strategy selected, including diversification and asset allocation. Investments mentioned may not be suitable for all investors.

All images have been produced by the author, except where stated otherwise.

--

--

Amir Nejad
QuantJam

PhD. Engineer | Data Scientist | Problem Solver | Solution Oriented (twitter: @Dr_Nejad)