Python | ARIMA Model for Time Series Forecasting

19 Feb, 2020
A Time Series is defined as a series of data points indexed in time order. The time order can be daily, monthly, or even yearly. Given below is an example of a Time Series that illustrates the number of passengers of an airline per month from the year 1949 to 1960.
Time Series ForecastingTime Series forecasting is the process of using a statistical model to predict future values of a time series based on past results.
Some Use Cases
To predict the number of incoming or churning customers.
To explaining seasonal patterns in sales.
To detect unusual events and estimate the magnitude of their effect.
To Estimate the effect of a newly launched product on number of sold units.
Components of a Time Series:
Trend:The trend shows a general direction of the time series data over a long period of time. A trend can be increasing(upward), decreasing(downward), or horizontal(stationary).
Seasonality:The seasonality component exhibits a trend that repeats with respect to timing, direction, and magnitude. Some examples include an increase in water consumption in summer due to hot weather conditions, or an increase in the number of airline passengers during holidays each year.
Cyclical Component: These are the trends with no set repetition over a particular period of time. A cycle refers to the period of ups and downs, booms and slums of a time series, mostly observed in business cycles. These cycles do not exhibit a seasonal variation but generally occur over a time period of 3 to 12 years depending on the nature of the time series.
Irregular Variation: These are the fluctuations in the time series data which become evident when trend and cyclical variations are removed. These variations are unpredictable, erratic, and may or may not be random.
ETS DecompositionETS Decomposition is used to separate different components of a time series. The term ETS stands for Error, Trend, and Seasonality.
Code: ETS Decomposition of Airline Passengers Dataset:
# Importing required librariesimport numpy as npimport pandas as pdimport matplotlib.pylot as pltfrom statsmodels.tsa.seasonal import seasonal_decompose  # Read the AirPassengers datasetairline = pd.read_csv('AirPassengers.csv',                       index_col ='Month',                       parse_dates = True)  # Print the first five rows of the datasetairline.head()  # ETS Decompositionresult = seasonal_decompose(airline['# Passengers'],                             model ='multiplicative')  # ETS plot result.plot()
Output:
ARIMA Model for Time Series ForecastingARIMA stands for autoregressive integrated moving average model and is specified by three order parameters: (p, d, q).
AR(p) Autoregression – a regression model that utilizes the dependent relationship between a current observation and observations over a previous period.An auto regressive (AR(p)) component refers to the use of past values in the regression equation for the time series.
I(d) Integration – uses differencing of observations (subtracting an observation from observation at the previous time step) in order to make the time series stationary. Differencing involves the subtraction of the current values of a series with its previous values d number of times.
MA(q) Moving Average – a model that uses the dependency between an observation and a residual error from a moving average model applied to lagged observations. A moving average component depicts the error of the model as a combination of previous error terms. The order q represents the number of terms to be included in the model.
Types of ARIMA Model
ARIMA:Non-seasonal Autoregressive Integrated Moving Averages
SARIMA:Seasonal ARIMA
SARIMAX:Seasonal ARIMA with exogenous variables
Pyramid Auto-ARIMA
The ‘auto_arima’ function from the ‘pmdarima’ library helps us to identify the most optimal parameters for an ARIMA model and returns a fitted ARIMA model.
Code : Parameter Analysis for the ARIMA model
# To install the librarypip install pmdarima  # Import the libraryfrom pmdarima import auto_arima  # Ignore harmless warningsimport warningswarnings.filterwarnings("ignore")  # Fit auto_arima function to AirPassengers datasetstepwise_fit = auto_arima(airline['# Passengers'], start_p = 1, start_q = 1,                          max_p = 3, max_q = 3, m = 12,                          start_P = 0, seasonal = True,                          d = None, D = 1, trace = True,                          error_action ='ignore',   # we don't want to know if an order does not work                          suppress_warnings = True,  # we don't want convergence warnings                          stepwise = True)           # set to stepwise  # To print the summarystepwise_fit.summary()
Output:
Code : Fit ARIMA Model to AirPassengers dataset
# Split data into train / test setstrain = airline.iloc[:len(airline)-12]test = airline.iloc[len(airline)-12:] # set one year(12 months) for testing  # Fit a SARIMAX(0, 1, 1)x(2, 1, 1, 12) on the training setfrom statsmodels.tsa.statespace.sarimax import SARIMAX  model = SARIMAX(train['# Passengers'],                 order = (0, 1, 1),                 seasonal_order =(2, 1, 1, 12))  result = model.fit()result.summary()
Output:
Code : Predictions of ARIMA Model against the test set
start = len(train)end = len(train) + len(test) - 1  # Predictions for one-year against the test setpredictions = result.predict(start, end,                             typ = 'levels').rename("Predictions")  # plot predictions and actual valuespredictions.plot(legend = True)test['# Passengers'].plot(legend = True)
Output:
Code : Evaluate the model using MSE and RMSE
# Load specific evaluation toolsfrom sklearn.metrics import mean_squared_errorfrom statsmodels.tools.eval_measures import rmse  # Calculate root mean squared errorrmse(test["# Passengers"], predictions)  # Calculate mean squared errormean_squared_error(test["# Passengers"], predictions)
Output:
Code : Forecast using ARIMA Model
# Train the model on the full datasetmodel = model = SARIMAX(airline['# Passengers'],                         order = (0, 1, 1),                         seasonal_order =(2, 1, 1, 12))result = model.fit()  # Forecast for the next 3 yearsforecast = result.predict(start = len(airline),                           end = (len(airline)-1) + 3 * 12,                           typ = 'levels').rename('Forecast')  # Plot the forecast valuesairline['# Passengers'].plot(figsize = (12, 5), legend = True)forecast.plot(legend = True)
Output:
Machine Learning
Python
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
ML | Linear Regression
Search Algorithms in AI
Getting started with Machine Learning
Introduction to Recurrent Neural Network
ML | Monte Carlo Tree Search (MCTS)
Read JSON file using Python
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Different ways to create Pandas Dataframe