Demand forecasting : Exponential smoothing

Exponential smoothing is another simple forecasting model. This model too assumes that future demand is more or less similar to recent demand. 

The key concept behind exponential smoothing is that in order to determine the forecast the model assigns highest weight to the most recent period. Older periods are assigned lesser weights. This ensures that outliers and noise have a lesser impact than in the case of the Moving Average model. 

The Exponential smoothing model can be mathematically expressed as:$$f_t=\alpha d_{t-1}+(1-\alpha)f_{t-1}$$ $$\ 0<\alpha\le1$$
The Exponential smoothing model is a kind of "learning model" since the previous forecast made by the model, which already included previous demand observations, is used to determine the latest forecast. In other words, this model uses the most recent demand observation as well as the previous forecast it made.

Since the forecast for each period is function of "alpha", a learning rate/ratio as well as the previous demand and forecasts, we can re-write the equation as :

$$f_t=\alpha d_{t-1}+(1-\alpha)f_{t-1}$$ $$f_{t-1}=\alpha d_{t-2}+(1-\alpha) f_{t-2}$$ $$f_t=\alpha d_{t-1}+\propto(1-\alpha)d_{t-2}+(1-\alpha)^2f_{t-2}$$ $$f_t=\alpha d_{t-1}+\propto(1-\alpha)d_{t-2}+\alpha(1-\alpha)^2d_{t-3}+(1-\alpha)^3f_{t-3}$$ and so on...

We see that the weight given to each older demand observation is reduced by a factor of \((1-\alpha) \cdot \) Hence this model is called "Exponential smoothing".

We can also see that higher the value of alpha, the closer the forecast follows the demand in the last period. Alpha =1 will give us a "naive" forecast where the next forecast will be equal to the last period's demand. Hence determining an optimum value of alpha becomes important. This can vary from one organisation to another but it is obvious that for any value of alpha greater than 0.5 the model assigns more weight to latest observations at the expense of earlier observations.

Now moving on to implementation of the Exponential smoothing model.

One way to initialise the forecast is to assign it a value equal to the average of the first "n" demand observations. $$f_0 = \frac{1}{n} \sum_{i=0}^{n} d_{t}$$


def exp_smooth(d,alpha=0.3):
    d=np.append(d,[np.nan])
    f=np.full(len(d),np.nan)
   
   #Initialising forecast as the average of the first three demand observations
    f[1]=(d[0]+d[1]+d[2])/3
    
    for t in range (2,len(d)):
        f[t]=round((alpha*d[t-1]+(1-alpha)*f[t-1]),2)    
       
    df=pd.DataFrame.from_dict({'Demand':d,'Forecast':f})
    
    return df

# For my test, I have used a csv file with demand data for the last 50 periods.
# The contents were generated using the random() function in excel.

import pandas as pd
import numpy as np

df_read=pd.read_csv('Demand50.csv')
d=df_read.values

# Calling the function with a learning rate value of 0.3.
df_exp_smooth= exp_smooth(d,alpha=0.3)

df_exp_smooth.index.name ='Period'
df_exp_smooth[['Demand','Forecast']].plot() 


Comments