Reader small image

You're reading from  Time Series Analysis with Python Cookbook

Product typeBook
Published inJun 2022
PublisherPackt
ISBN-139781801075541
Edition1st Edition
Concepts
Right arrow
Author (1)
Tarek A. Atwan
Tarek A. Atwan
author image
Tarek A. Atwan

Tarek A. Atwan is a data analytics expert with over 16 years of international consulting experience, providing subject matter expertise in data science, machine learning operations, data engineering, and business intelligence. He has taught multiple hands-on coding boot camps, courses, and workshops on various topics, including data science, data visualization, Python programming, time series forecasting, and blockchain at various universities in the United States. He is regarded as a data science mentor and advisor, working with executive leaders in numerous industries to solve complex problems using a data-driven approach.
Read more about Tarek A. Atwan

Right arrow

Chapter 10: Building Univariate Time Series Models Using Statistical Methods

In Chapter 9, Exploratory Data Analysis and Diagnosis, you were introduced to several concepts to help you understand the time series process. Such recipes included Decomposing time series data, Detecting time series stationarity, Applying power transformations, and Testing for autocorrelation in time series data. These techniques will come in handy in the statistical modeling approach that will be discussed in this chapter.

When working with time series data, different methods and models can be used, depending on whether the time series is univariate or multivariate, seasonal or non-seasonal, stationary or non-stationary, and linear or nonlinear. If you list the assumptions you need to consider and examine – for example, stationarity and autocorrelation – it will become apparent why time series data is deemed to be complex and challenging. Thus, to model such a complex system, your goal is...

Technical requirements

You can download the Jupyter Notebooks and necessary datasets from this book's GitHub repository:

Before you start working through the recipes in this chapter, please run the following code to load the datasets and functions that will be referenced throughout:

  1. Start by importing the basic libraries that will be shared across all the recipes in this chapter:
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import warnings
    from statsmodels.tsa.api import (kpss, adfuller, 
                                  seasonal_decompose, STL)
    from statsmodels.tools.eval_measures import rmspe, rmse
    from sklearn.metrics import mean_absolute_percentage_error...

Plotting ACF and PACF

When building statistical forecasting models such as AR, MA, ARMA, ARIMA, or SARIMA, you will need to determine the type of time series model that is most suitable for your data and the values for some of the required parameters, called orders. More specifically, these are called the lag orders for the autoregressive (AR) or moving average (MA) components. This will be explored further in the Forecasting univariate time series data with non-seasonal ARIMA recipe of this chapter.

To demonstrate this, for example, an Autoregressive Moving Average (ARMA) model can be written as ARMA(p, q), where p is the autoregressive order or AR(p) component, and q is the moving average order or MA(q) component. Hence, an ARMA model combines an AR(p) and an MA(q) model.

The core idea behind these models is built on the assumption that the current value of a particular variable, , can be estimated from past values of itself. For example, in an autoregressive model of order...

Forecasting univariate time series data with exponential smoothing

In this recipe, you will explore the exponential smoothing technique using the statsmodels library. The ExponentialSmoothing classes in statsmodels resemble popular implementations from the R forecast package, such as ets() and HoltWinters(). In statsmodels, there are three different implementations (classes) of exponential smoothing, depending on the nature of the data you are working with:

  • SimpleExpSmoothing: Simple exponential smoothing is used when the time series process lacks seasonality and trend. This is also referred to as single exponential smoothing.
  • Holt: Holt's exponential smoothing is an enhancement of the simple exponential smoothing and is used when the time series process contains only trend (but no seasonality). It is referred to as double exponential smoothing.
  • ExponentialSmoothing: Holt-Winters' exponential smoothing is an enhancement of Holt's exponential smoothing...

Forecasting univariate time series data with non-seasonal ARIMA

In this recipe, you will explore non-seasonal ARIMA and use the implementation in the statsmodels package. ARIMA stands for Autoregressive Integrated Moving Average, which combines three main components: the autoregressive or AR(p) model, the moving average or MA(q) model, and an integrated (differencing) factor or I(d).

An ARIMA model can be defined by the p, d, and q parameters, so for a non-seasonal time series, it is described as ARIMA(p, d, q). The p and q parameters are called orders; for example, in AR of order p and MA of order q. They can also be called lags since they represent the number of periods we need to lag for. You may also come across another reference for p and q, namely polynomial degree.

ARIMA models can handle non-stationary time series data through differencing, a time series transformation technique, to make a non-stationary time series stationary. The integration or order of differencing...

Forecasting univariate time series data with seasonal ARIMA

In this recipe, you will be introduced to an enhancement to the ARIMA model for handling seasonality, known as the Seasonal Autoregressive Integrated Moving Average or SARIMA. Like an ARIMA(p, d, q), a SARIMA model also requires (p, d, q) to represent non-seasonal orders. Additionally, a SARIMA model requires the orders for the seasonal component, which is denoted as (P, D, Q, s). Combining both components, the model can be written as a SARIMA(p, d, q)(P, D, Q, s). The letters still mean the same, and the letter case indicates which component. For example, the lowercase letters represent the non-seasonal orders, while the uppercase letters represent the seasonal orders. The new parameter, s, is the number of steps per cycle – for example, s=12 for monthly data or s=4 for quarterly data.

In statsmodels, you will use the SARIMAX class to build a SARIMA model.

In this recipe, you will be working with the milk data...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Time Series Analysis with Python Cookbook
Published in: Jun 2022Publisher: PacktISBN-13: 9781801075541
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tarek A. Atwan

Tarek A. Atwan is a data analytics expert with over 16 years of international consulting experience, providing subject matter expertise in data science, machine learning operations, data engineering, and business intelligence. He has taught multiple hands-on coding boot camps, courses, and workshops on various topics, including data science, data visualization, Python programming, time series forecasting, and blockchain at various universities in the United States. He is regarded as a data science mentor and advisor, working with executive leaders in numerous industries to solve complex problems using a data-driven approach.
Read more about Tarek A. Atwan