Seasonality, trend and noise in time series data

1. Seasonality, trend and noise in time series data

Let's continue exploring how to discover structure in time series data. In this lesson, we will discuss some of key properties of times series data, namely, seasonality, trend and noise.

2. Properties of time series

When looking at time series data, you may have noticed some clear patterns that they exhibit. As you can see in the time series shown here, the data displays a clear upward trend as well as a periodic signal.

3. The properties of time series

In general, most time series can be decomposed in three major components. The first is seasonality, which describes the periodic signal in your time series. The second component is trend, which describes whether the time series is decreasing, constant or increasing over time. Finally, the third component is noise, which describes the unexplained variance and volatility of your time series. Let's go through some concrete examples so that you get a better understanding of each of these components.

4. Time series decomposition

When looking at time series, it may seem daunting to extract the trend of a time series, or having to manually identify its seasonality. Fortunately, you don't have to resort to manual work in order to extract the different components of a time series. Instead, you can rely on a method known as time-series decomposition, which will allow you to automatically extract and quantify the structure of time-series data. To preform time series decomposition you can leverage the statsmodels library, only this time we will rely on the statsmodels dot tsa sub-module, which contains functions that are useful for time series analysis. The sm dot tsa dot seasonal_decompose() function can be used to apply time series decomposition out of the box. The example on this slide shows how to perform time-series decomposition on the values in the co2 column of the co2_levels DataFrame. Note that by default, seasonal_decompose() returns a figure of relatively small size, so lines 3 and 4 ensure that the output figure is large enough for us to visualize.

5. A plot of time series decomposition on the CO2 data

The seasonal_decompose() function returns an object that contains the values of all the three components of interest, which are the seasonal, trend and noise components.

6. Extracting components from time series decomposition

Additionally, it is easy to extract each individual component and plot them. As you can see here, you can use the dir() command to print out the attributes associated to the decomposition variable generated earlier, and to print the seasonal component, use decomposition dot seasonal.

7. Seasonality component in time series

This example extracts and plots the values for the seasonal component. A seasonal pattern exists when a time series is influenced by seasonal factors. Seasonality should always be a fixed and known period.

8. Seasonality component in time series

For example, the temperature of the day should display clear daily seasonality, as it is always warmer during the day than at night. Alternatively, it could also display monthly seasonality, as it is always warmer in summer compared to winter.

9. Trend component in time series

Let's repeat the same exercise, but this time extract the trend values of the time series decomposition. The trend component reflects the overall progression of the time series and can be extracted using the decomposition dot trend command shown in line 1.

10. Trend component in time series

You can then use the familiar dot plot() method to plot and annotate the trend values of your time series data.

11. Noise component in time series

Finally, you can also extract the noise, or the residual component of a time series as shown here.

12. Noise component in time series

This describes random, irregular influences that could not be attributed to either trend or seasonality.

13. Let's practice!

Now, it's your turn to work on time series decomposition!