All about it : Time Series Analysis — Exponential smoothing example

Sreeram Kashyap
Analytics Vidhya
Published in
5 min readFeb 2, 2021

--

Hi everyone! In this article I am going to explain time series forecast using the exponential smoothing method in python. If you are reading this I am pretty sure you know about basics of the exponential smoothing method. If you don’t, you can read it here.

In this example we will be using yfinance API for downloading stock market data and understand how simple exponential smoothing works. I am using Microsoft data as an example. You can change it to your favorite stock.

Now let’s dive in.

Exponential smoothing in brief: It used a smoothing factor on the historical data and assigns weights to the data. It is then used to make forecasts into the future.

Data: We will use stock market data for 1 month using yahoo finance API.

Language: Python

Libraries: statmodels, pandas, matplotlib, datetime.

API: yfinance

First install the libraries.

!pip install pandas, matplotlib
!pip install statmodels

Now we are ready to start. We need to import the following libraries

import pandas as pd
import yfinance as yf
import datetime
import matplotlib.pyplot as plt
from statsmodels.tsa.api import SimpleExpSmoothing

Now we need to download the data from yfinance. The yfinance API updates stock market data for everyday. It takes three main arguments namely tickers, period and interval. Period is the total period of data we want to download an interval is the gap between two recordings or the size of each timestep. Tickers takes the symbol of the stock we want as listed in the stock market. You can check the symbols of your favorite stock from NYSE or s&p 500 list.

The following command downloads the data.

data = yf.download(tickers=’MSFT’, period=’1mo’, interval=’1d’)

you can see the downloaded data by using the head() command

data.head(3)

Let’s first plot the downloaded data and see how it looks

today = datetime.date.today()
plt.figure()
plt.title(‘ MICROSOFT STOCK FOR LAST 1 MONTH AS ON ‘+ str(today))
plt.plot(data[‘Close’])

In the plotted figure you can observe a trend of increase over time and some variations at each timestep. That’s how a typical stock chart would look like.

Now let’s get to forecasts. We will use the statsmodels library’s built in SimpleExpSmoothing function for the forecasts. For this, we need to specify the smoothing coefficient(alpha).

SimpleExpSmoothing(data”).fit(smoothing_level=0.1)

Learn about the function and the parameters in detail here

There are other parameters that the function takes but this will be enough for us to understand.

Let’s first define the inputs. The function needs input as a series consisting of values and index. In our case the values will be closing price of the stock at each timestep and index will be the dates.

Here another decision to be made is the timestep for forecast. This defines how many timesteps into the future we want to predict the values. In this example we will select a time step of 2 days. Remember that the time step should be of same unit as the interval defined while downloading the data. In our case both are in terms of days.

data = data[‘Close’].tolist()
index= pd.date_range(start=’2021–01–05', end=’2021–02–01', freq=’B’)
stock_data = pd.Series(data, index)
forecast_timestep = 2
#CHANGE DATA to period of 30 DAYS FROM THE DATE WHEN YOU ARE RUNNING THIS CODE

Now we will use SimpleExpSmoothing function to fit the curve first and then make forecasts for each timestep.

Here we need to specify the alpha value. We discussed in the previous article that alpha value is set on a trial and error basis. So we will experiment with three different values of alpha first. Then we will use in-built function to select the best value for alpha.

fit_1 = SimpleExpSmoothing(stock_data, initialization_method=”heuristic”).fit(smoothing_level=0.1,optimized=False)
forecast1 = fit_1.forecast(forecast_timestep).rename(r’$\alpha=0.1$’)
plt.plot(stock_data, color=’black’)
plt.plot(fit_1.fittedvalues, color=’cyan’)
line1, = plt.plot(forecast1, color=’cyan’)

You can easily see that the forecasts are quite far away from the actual values. So we know that we have to change the value.

Let’s try 0.4 now.

fit_2 = SimpleExpSmoothing(stock_data, initialization_method=”heuristic”).fit(smoothing_level=0.4,optimized=False)
forecast2 = fit_2.forecast(forecast_timestep).rename(r’$\alpha=0.4$’)
plt.plot(fit_2.fittedvalues, color=’red’)
line2, = plt.plot(forecast2, color=’red’)

you can observe that the forecast values raise closer to the actual values. So now we know that we need to increase the alpha value further.

Let’s see all the values and also the inbuilt function for selecting alpha value.

plt.figure(figsize=(16,10))
plt.plot(stock_data, color='black')
plt.plot(fit_1.fittedvalues, color='cyan')
line1, = plt.plot(forecast1, color='cyan')
plt.plot(fit_2.fittedvalues, color='red')
line2, = plt.plot(forecast2, color='red')
plt.plot(fit_3.fittedvalues, color='green')
line3, = plt.plot(forecast3, color='green')
plt.plot(fit_4.fittedvalues, color='blue')
line4, = plt.plot(forecast4, color='blue')
plt.legend([line1, line2, line3,line4], [forecast1.name, forecast2.name, forecast3.name,forecast4.name])
plt.show()

You can see the forecasts clearly now. The in-built function selected a value of 0.810 for alpha which as you can observe, gave good forecasts compared to others. But as discussed before, the simple exponential smoothing is not a very accurate measure. You can see theat the best selected value of alpha a=has some deviation as well.

Full code

import numpy as np
import pandas as pd
import yfinance as yf
import datetime
import matplotlib.pyplot as plt
from statsmodels.tsa.api import SimpleExpSmoothing
data = yf.download(tickers=’MSFT’, period=’1mo’, interval=’1d’)data.head(3)today = datetime.date.today()
plt.figure(figsize=(10,6))
plt.title(‘ MICROSOFT STOCK FOR LAST 1 MONTH AS ON ‘+ str(today))
plt.plot(data[‘Close’])
plt.show()
data = data[‘Close’].tolist()
index= pd.date_range(start=’2021–01–05', end=’2021–02–01', freq=’B’)#CHANGE DATA FOR 30 DAYS FROM THE DATE WHEN YOU ARE RUNNING THIS CODE
stock_data = pd.Series(data, index)
forecast_timestep = 2
fit_1 = SimpleExpSmoothing(stock_data, initialization_method=”heuristic”).fit(smoothing_level=0.1,optimized=False)
forecast1 = fit_1.forecast(forecast_timestep).rename(r’$\alpha=0.1$’)
fit_2 = SimpleExpSmoothing(stock_data, initialization_method=”heuristic”).fit(smoothing_level=0.4,optimized=False)
forecast2 = fit_2.forecast(forecast_timestep).rename(r’$\alpha=0.4$’)
fit_3 = SimpleExpSmoothing(stock_data, initialization_method=”heuristic”).fit(smoothing_level=0.6,optimized=False)
forecast3 = fit_3.forecast(forecast_timestep).rename(r’$\alpha=0.8$’)
fit_4 = SimpleExpSmoothing(stock_data, initialization_method=”estimated”).fit()
forecast4 = fit_4.forecast(forecast_timestep).rename(r’$\alpha=%s$’%fit_4.model.params[‘smoothing_level’])
plt.figure(figsize=(16,10))
plt.plot(stock_data, color=’black’)
plt.plot(fit_1.fittedvalues, color=’cyan’)
line1, = plt.plot(forecast1, color=’cyan’)
plt.plot(fit_2.fittedvalues, color=’red’)
line2, = plt.plot(forecast2, color=’red’)
plt.plot(fit_3.fittedvalues, color=’green’)
line3, = plt.plot(forecast3, color=’green’)
plt.plot(fit_4.fittedvalues, color=’blue’)
line4, = plt.plot(forecast4, color=’blue’)
plt.legend([line1, line2, line3,line4], [forecast1.name, forecast2.name, forecast3.name,forecast4.name])
plt.show()

We will discuss other methods in future articles.

…………Stay Tuned……..

--

--