# Forecasting Methods : Part I

Recently, I was asked to teach a class on forecasting using Python. I thought my notes would be a good source of information for every one interested in this area and I might learn from my reader’s feedback as well. In this part 1 of the article we will talk about basic forecasting methods — Naive, Average, Moving Average and Weighted Average. In the next parts we will discuss measures of scoring, exponential smoothing techniques, Holt, Holt winters, ARIMA, ARMA and deep learning methods using LSTM.

Incidentally Kaggle also released a competition on forecasting which plans to forecast for 145K time series. However, this article is based on M-competitions data-set. Kaggle’s competition is very similar to M3 competitions held in the past led by forecasting researcher Spyros Makridakis. The goal of the M3 competition was to be able to forecast across multiple (3003) time series. The results and learnings from the M3 competition are summarized in the paper.

In this article set I will review forecasting methods using python. The goal is to develop an intuitive understanding of forecasting for beginners. Finally, I felt there are not many mature libaries for forecasting in python, although statsmod provides ARIMA and ARMA at the time of this writing. Facebook’s prophet also seems to be at an early stage.

We will cover the following forecasting methods as shown below -

### Installing libraries

We will use the following libraries. Majority of these could be installed from pip3.

### Pre-processing Data

For this article, I have downloaded data from M3 competition website. Then we massage the data to get to one series and play with it.

We see for the series N 1 there are a total of 1–47 observations. Out of which many might be empty values, represented in python as NaN. We then melt the data to get to our 1st series N 1. We see the series N 1 is an annual series and has a start date of 1975.

In the code shown below, we get a list of variables that we need to melt(pandas melt function), then we create a date series using pandas date_range function and finally create a time-series Series object series1 with index as the year. We print the series.

Next we split the date into 1/3rds, for training and testing.

Lets plot the test and the training data-sets using matplot lib.

### Forecasting Methods

Now that we have the data in the right form, we begin with our journey of forecasting with humble beginnings of Naive Method.

### Method 1 :Naive Method

This is the most primitive forecasting method. The premise of the naive method is that the expected point is equal to the last observed point:

Naive methods although look so simple (“naive”) are useful to create a baseline and also to compute some metrics such as MdRAE. We will cover metrics in detail in the next section of this series.

Lets see how it looks, when we plot the Naive method along side our data and predict for few (7) years ahead.

### Method 2 (a): Average Forecasting Single Next Period

A little more sophisticated method to forecasting beyond “naive” is average method. Here we believe that a good forecast will be an average of all data points.

### Method 2 (b): Average Forecasting Multiple Periods

Average method applied to the multiple periods, would give a flat line. The idea is to develop a visual representation as we move in our journey of learning forecasting.

### Method 3: Moving Average Forecasting

The idea of using last k periods as a good window to predict the next period is moving average. There are two main types of moving average forecasts: Centered Moving Average and Trailing Moving Average.

#### Centered Moving Average (CMA)

The value at time (t) is calculated as the average of raw observations at, before, and after time (t).

For example, a center moving average with a window of 3 would be calculated as:

centered_mv_avg(t) = mean(observed(t-1), observed(t), observed(t+1))

This method requires knowledge of future values, and as such is used on time series analysis to better understand the dataset.

A center moving average can be used as a general method to remove trend and seasonal components from a time series, a method that we often cannot use when forecasting.

#### Trailing Moving Average (TMA)

The value at time (t) is calculated as the average of the raw observations at and before the time (t).

For example, a trailing moving average with a window of 3 would be calculated as:

traling_mv_average(t) = mean(observed(t-2), observed(t-1), observed(t))

Trailing moving average only uses historical observations and is used on time series forecasting. We will use this for our discussion ahead.

### Method 4 : Weighted Moving Average

The next improvement beyond the moving average, is weighted moving average. Here we assign weights to the last k-periods. The most important thing here to remember is that sum of all weights should be equal to 1.

If you are still with me, then we have looked at the very basic methods of forecasting. I had to create a custom library for this, since none of the major libraries in python had these basic time series methods available. I will make this available through pip soon.

References

Thank you for reading this. Here are the list of references that will be very helpful.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.