Table of Contents


Exponential smoothing was proposed in the late 1950s (Brown, 1959; Holt, 1957; Winters, 1960), and has motivated some of the most successful forecasting methods. Forecasts produced using exponential smoothing methods are weighted averages of past observations, with the weights decaying exponentially as the observations get older. In other words, the more recent the observation the higher the associated weight. This framework generates reliable forecasts quickly and for a wide range of time series, which is a great advantage and of major importance to applications in industry.

The simple exponential smoothing model is a method used in time series analysis to predict future values based on historical observations. This model is based on the idea that future values of a time series will be influenced by past values, and that the influence of past values will decrease exponentially as you go back in time.

The simple exponential smoothing model uses a smoothing factor, which is a number between 0 and 1 that indicates the relative importance given to past observations in predicting future values. A value of 1 indicates that all past observations are given equal importance, while a value of 0 indicates that only the latest observation is considered.

The simple exponential smoothing model can be expressed mathematically as:

y^T+1T=αyT+α(1α)yT1+α(1α)2yT2+,\hat{y}_{T+1|T} = \alpha y_T + \alpha(1-\alpha) y_{T-1} + \alpha(1-\alpha)^2 y_{T-2}+ \cdots,

where yTy_T is the observed value in period tt, y^T+1T\hat{y}_{T+1|T} is the predicted value for the next period, y (t1)(t-1) is the observed value in the previous period, and α\alpha is the smoothing factor.

The simple exponential smoothing model is a widely used forecasting model due to its simplicity and ease of use. However, it also has its limitations, as it cannot capture complex patterns in the data and is not suitable for time series with trends or seasonal patterns.

Building of Simple exponential smoothing model

The simplest of the exponentially smoothing methods is naturally called simple exponential smoothing (SES). This method is suitable for forecasting data with no clear trend or seasonal pattern.

Using the naïve method, all forecasts for the future are equal to the last observed value of the series, y^T+hT=yT,\hat{y}_{T+h|T} = y_{T},

for h=1,2,h=1,2,\dots. Hence, the naïve method assumes that the most recent observation is the only important one, and all previous observations provide no information for the future. This can be thought of as a weighted average where all of the weight is given to the last observation.

Using the average method, all future forecasts are equal to a simple average of the observed data, y^T+hT=1Tt=1Tyt,\hat{y}_{T+h|T} = \frac1T \sum_{t=1}^T y_t,

for h=1,2,h=1,2,\dots Hence, the average method assumes that all observations are of equal importance, and gives them equal weights when generating forecasts.

We often want something between these two extremes. For example, it may be sensible to attach larger weights to more recent observations than to observations from the distant past. This is exactly the concept behind simple exponential smoothing. Forecasts are calculated using weighted averages, where the weights decrease exponentially as observations come from further in the past — the smallest weights are associated with the oldest observations:

where 0α10 \le \alpha \le 1 is the smoothing parameter. The one-step-ahead forecast for time T+1T+1 is a weighted average of all of the observations in the series y1,,yTy_1,\dots,y_T. The rate at which the weights decrease is controlled by the parameter α\alpha.

For any α\alpha between 0 and 1, the weights attached to the observations decrease exponentially as we go back in time, hence the name “exponential smoothing”. If α\alpha is small (i.e., close to 0), more weight is given to observations from the more distant past. If α\alpha is large (i.e., close to 1), more weight is given to the more recent observations. For the extreme case where α=1\alpha=1, y^T+1T=yT\hat{y}_{T+1|T}=y_T and the forecasts are equal to the naïve forecasts.

We present two equivalent forms of simple exponential smoothing, each of which leads to the forecast Equation (1).

Weighted average form

The forecast at time T+1T+1 is equal to a weighted average between the most recent observation yTy_T and the previous forecast y^TT1\hat{y}_{T|T-1}:

y^T+1T=αyT+(1α)y^TT1,\hat{y}_{T+1|T} = \alpha y_T + (1-\alpha) \hat{y}_{T|T-1},

where 0α10 \le \alpha \le 1 is the smoothing parameter. Similarly, we can write the fitted values as y^t+1t=αyt+(1α)y^tt1,\hat{y}_{t+1|t} = \alpha y_t + (1-\alpha) \hat{y}_{t|t-1},

for t=1,,Tt=1,\dots,T. (Recall that fitted values are simply one-step forecasts of the training data.)

The process has to start somewhere, so we let the first fitted value at time 1 be denoted by 0\ell_{0} (which we will have to estimate). Then

Substituting each equation into the following equation, we obtain

The last term becomes tiny for large TT. So, the weighted average form leads to the same forecast Equation (1).

Component form

An alternative representation is the component form. For simple exponential smoothing, the only component included is the level, t\ell_{t}. Component form representations of exponential smoothing methods comprise a forecast equation and a smoothing equation for each of the components included in the method. The component form of simple exponential smoothing is given by:

where t\ell_{t} is the level (or the smoothed value) of the series at time tt. Setting h=1h=1 gives the fitted values, while setting t=Tt=T gives the true forecasts beyond the training data.

The forecast equation shows that the forecast value at time t+1t+1 is the estimated level at time tt. The smoothing equation for the level (usually referred to as the level equation) gives the estimated level of the series at each period tt.

If we replace t\ell_{t} with y^t+1t\hat{y}_{t+1|t} and t1\ell_{t-1} with y^tt1\hat{y}_{t|t-1} in the smoothing equation, we will recover the weighted average form of simple exponential smoothing.

The component form of simple exponential smoothing is not particularly useful on its own, but it will be the easiest form to use when we start adding other components.

Flat forecasts

Simple exponential smoothing has a “flat” forecast function:

y^T+hT=y^T+1T=T,h=2,3,.\hat{y}_{T+h|T} = \hat{y}_{T+1|T}=\ell_T, \qquad h=2,3,\dots.

That is, all forecasts take the same value, equal to the last level component. Remember that these forecasts will only be suitable if the time series has no trend or seasonal component.

Loading libraries and data


Statsforecast will be needed. To install, see instructions.

Next, we import plotting libraries and configure the plotting style.

import matplotlib.pyplot as plt
import seaborn as sns
from import plot_acf, plot_pacf'grayscale') # fivethirtyeight  grayscale  classic
plt.rcParams['lines.linewidth'] = 1.5
dark_style = {
    'figure.facecolor': '#008080',  # #212946
    'axes.facecolor': '#008080',
    'savefig.facecolor': '#008080',
    'axes.grid': True,
    'axes.grid.which': 'both',
    'axes.spines.left': False,
    'axes.spines.right': False,
    '': False,
    'axes.spines.bottom': False,
    'grid.color': '#000000',  #2A3459
    'grid.linewidth': '1',
    'text.color': '0.9',
    'axes.labelcolor': '0.9',
    'xtick.color': '0.9',
    'ytick.color': '0.9',
    'font.size': 12 }

from pylab import rcParams
rcParams['figure.figsize'] = (18,7)
import pandas as pd

The input to StatsForecast is always a data frame in long format with three columns: unique_id, ds and y:

  • The unique_id (string, int or category) represents an identifier for the series.

  • The ds (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp.

  • The y (numeric) represents the measurement we wish to forecast.

df.columns=["ds", "y", "unique_id"]
ds           object
y             int64
unique_id    object
dtype: object
df["ds"] = pd.to_datetime(df["ds"])

Explore Data with the plot method

Plot some series using the plot method from the StatsForecast class. This method prints a random series from the dataset and is useful for basic EDA.

from statsforecast import StatsForecast


Autocorrelation plots

fig, axs = plt.subplots(nrows=1, ncols=2)

plot_acf(df["y"],  lags=30, ax=axs[0],color="fuchsia")

# Grafico
plot_pacf(df["y"],  lags=30, ax=axs[1],color="lime")
axs[1].set_title('Partial Autocorrelation')

#plt.savefig("Gráfico de Densidad y qq");

Split the data into training and testing

Let’s divide our data into sets

  1. Data to train our Simple Exponential Smoothing (SES).
  2. Data to test our model

For the test data we will use the last 30 hours to test and evaluate the performance of our model.

train = df[df.ds<='2017-09-20 17:00:00'] 
test = df[df.ds>'2017-09-20 17:00:00']
train.shape, test.shape
((186, 3), (30, 3))

Now let’s plot the training data and the test data.

sns.lineplot(train,x="ds", y="y", label="Train", linestyle="--")
sns.lineplot(test, x="ds", y="y", label="Test")
plt.title("Ads watched (hourly data)");

Implementation of SimpleExponentialSmoothing with StatsForecast

To also know more about the parameters of the functions of the SimpleExponentialSmoothing Model, they are listed below. For more information, visit the documentation.

alpha : float
    Smoothing parameter.
alias : str
    Custom name of the model.
prediction_intervals : Optional[ConformalIntervals]
    Information to compute conformal prediction intervals.
    By default, the model will compute the native prediction

Load libraries

from statsforecast import StatsForecast
from statsforecast.models import SimpleExponentialSmoothing

Instantiating Model

We are going to build different models, for different values of alpha.

horizon = len(test)
# We call the model that we are going to use
models = [SimpleExponentialSmoothing(alpha=0.1, alias="SES01"),

We fit the models by instantiating a new StatsForecast object with the following parameters:

models: a list of models. Select the models you want from models and import them.

  • freq: a string indicating the frequency of the data. (See panda’s available frequencies.)

  • n_jobs: n_jobs: int, number of jobs used in the parallel processing, use -1 for all cores.

  • fallback_model: a model to be used if a model fails.

Any settings are passed into the constructor. Then you call its fit method and pass in the historical data frame.

sf = StatsForecast(df=df,
                   freq='H', # hourly frequency

Fit the Model

Let’s see the results of our Simple Simple Exponential Smoothing model (SES). We can observe it with the following instruction:

{'mean': array([120864.91], dtype=float32),
 'fitted': array([       nan,  80115.   ,  80092.   ,  81015.3  ,  83106.77 ,
         86959.09 ,  89910.69 ,  91569.12 ,  92691.7  ,  94228.03 ,
         96417.73 ,  99878.96 , 104793.06 , 110072.76 , 114136.98 ,
        117652.78 , 120897.5  , 123285.75 , 126026.18 , 129807.56 ,
        133450.3  , 134057.28 , 131241.05 , 127794.945, 123267.445,
        118953.2  , 114591.38 , 111642.74 , 110686.47 , 112131.32 ,
        112721.19 , 112371.57 , 111381.914, 110467.73 , 111004.95 ,
        112958.45 , 116095.11 , 119382.6  , 122359.336, 124927.41 ,
        127315.664, 129567.1  , 131667.39 , 133444.66 , 135152.19 ,
        134549.97 , 131476.47 , 127546.32 , 123068.19 , 118392.875,
        114066.586, 110923.92 , 108711.03 , 109682.93 , 110233.64 ,
        110304.27 , 109159.84 , 108662.36 , 108662.625, 110460.36 ,
        113457.83 , 117359.05 , 120250.64 , 123027.58 , 125498.32 ,
        127523.484, 129699.64 , 132702.17 , 135540.45 , 135538.4  ,
        133279.06 , 129971.164, 125735.55 , 121945.49 , 118635.445,
        116006.9  , 114852.71 , 114961.44 , 116360.3  , 118862.766,
        121420.484, 123603.44 , 124562.09 , 125229.88 , 126954.9  ,
        129996.91 , 132247.22 , 134396.   , 136075.89 , 137532.81 ,
        138523.03 , 139923.22 , 140618.4  , 139081.06 , 136965.45 ,
        132938.9  , 129006.016, 125011.414, 121444.77 , 118357.8  ,
        116351.016, 115972.914, 117322.625, 119730.86 , 123013.77 ,
        125970.4  , 127490.36 , 129496.32 , 132657.69 , 136025.42 ,
        139100.88 , 141504.8  , 143084.81 , 144681.83 , 146215.64 ,
        148428.58 , 150575.72 , 149789.16 , 146105.73 , 141229.66 ,
        135274.2  , 129697.77 , 124563.   , 120911.2  , 118799.08 ,
        119297.17 , 118499.95 , 116593.96 , 114700.06 , 112995.555,
        111952.5  , 112750.25 , 115050.73 , 117557.66 , 119974.89 ,
        122199.4  , 124515.46 , 126597.414, 128978.67 , 132232.81 ,
        134351.03 , 134387.92 , 131655.62 , 127994.57 , 123146.61 ,
        118665.445, 114265.91 , 111038.31 , 109729.484, 110691.03 ,
        110933.43 , 109728.086, 108155.28 , 106705.75 , 106453.68 ,
        107783.305, 110603.98 , 114189.08 , 116686.67 , 119740.51 ,
        122259.95 , 125170.96 , 128261.86 , 131574.17 , 134917.77 ,
        134834.98 , 131909.98 , 128004.484, 123131.04 , 118815.94 ,
        114745.34 , 111849.305, 110665.375, 111986.836, 112421.66 ,
        111608.49 , 110591.64 , 109295.98 , 109192.875, 110398.59 ,
        113443.734, 115954.86 , 118458.375, 120765.04 , 122847.53 ,
        124623.78 , 126112.9  , 128123.11 , 129553.3  , 128992.47 ,
        126229.23 , 122423.3  , 117785.97 , 113040.875, 108951.79 ,
        106076.11 , 104963.   , 106657.695, 107386.93 , 107297.734,
        106296.96 , 105553.266, 105561.44 , 106443.3  , 109032.47 ,
        112792.22 , 115712.5  , 118422.75 , 121182.47 , 124276.23 ,
        127027.6  , 129891.34 , 132491.2  , 131581.6  , 128731.43 ,
        125373.79 ], dtype=float32)}

As we can see, the result obtained above has an output in a dictionary, to extract each element from the dictionary we are going to use the .get() function to extract the element and then we are going to save it in a pd.DataFrame().

fitted=pd.DataFrame(result01.get("fitted"), columns=["fitted01"])
0NaNNaNNaN2017-09-13 00:00:00
180115.00000080115.00000080115.0000002017-09-13 01:00:00
280092.00000080000.00000079931.0000002017-09-13 02:00:00
213131581.593750138842.562500129852.3593752017-09-21 21:00:00
214128731.429688120961.281250108434.4687502017-09-21 22:00:00
215125373.789062108058.14062597810.8906252017-09-21 23:00:00
sns.lineplot(df, x="ds", y="y", label="Actual", linewidth=2)
sns.lineplot(fitted,x="ds", y="fitted01", label="Fitted01", linestyle="--", )
sns.lineplot(fitted, x="ds", y="fitted05", label="Fitted05", color="lime")
sns.lineplot(fitted, x="ds", y="fitted08", label="Fitted08")
plt.title("Ads watched (hourly data)");

Forecast Method

If you want to gain speed in productive settings where you have multiple series or models we recommend using the StatsForecast.forecast method instead of .fit and .predict.

The main difference is that the .forecast doest not store the fitted values and is highly scalable in distributed environments.

The forecast method takes two arguments: forecasts next h (horizon) and level.

  • h (int): represents the forecast h steps into the future. In this case, 30 hours ahead.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals. Depending on your computer, this step should take around 1min.

# Prediction
Y_hat = sf.forecast(h=horizon, fitted=True)
12017-09-22 00:00:00120864.9062594171.57031283790.179688
12017-09-22 01:00:00120864.9062594171.57031283790.179688
12017-09-22 02:00:00120864.9062594171.57031283790.179688
12017-09-22 03:00:00120864.9062594171.57031283790.179688
12017-09-22 04:00:00120864.9062594171.57031283790.179688
12017-09-13 00:00:0080115.0NaNNaNNaN
12017-09-13 01:00:0079885.080115.00000080115.0080115.000000
12017-09-13 02:00:0089325.080092.00000080000.0079931.000000
12017-09-13 03:00:00101930.081015.29687584662.5087446.203125
12017-09-13 04:00:00121630.083106.77343893296.2599033.242188

012017-09-22 00:00:00120864.9062594171.57031283790.179688
112017-09-22 01:00:00120864.9062594171.57031283790.179688
212017-09-22 02:00:00120864.9062594171.57031283790.179688
312017-09-22 03:00:00120864.9062594171.57031283790.179688
412017-09-22 04:00:00120864.9062594171.57031283790.179688

Predict method

To generate forecasts use the predict method.

The predict method takes two arguments: forecasts the next h (for horizon). * h (int): represents the forecast hh steps into the future. In this case, 30 hours ahead.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals.

This step should take less than 1 second.

forecast_df = sf.predict(h=horizon) 
12017-09-22 00:00:00120864.9062594171.57031283790.179688
12017-09-22 01:00:00120864.9062594171.57031283790.179688
12017-09-22 02:00:00120864.9062594171.57031283790.179688
12017-09-23 03:00:00120864.9062594171.57031283790.179688
12017-09-23 04:00:00120864.9062594171.57031283790.179688
12017-09-23 05:00:00120864.9062594171.57031283790.179688

We can join the forecast result with the historical data using the pandas function pd.concat(), and then be able to use this result for graphing.

df_plot=pd.concat([df, forecast_df]).set_index('ds')
2017-09-13 00:00:0080115.01NaNNaNNaN
2017-09-13 01:00:0079885.01NaNNaNNaN
2017-09-13 02:00:0089325.01NaNNaNNaN
2017-09-23 03:00:00NaNNaN120864.9062594171.57031283790.179688
2017-09-23 04:00:00NaNNaN120864.9062594171.57031283790.179688
2017-09-23 05:00:00NaNNaN120864.9062594171.57031283790.179688
# Plot the data and the exponentially smoothed data
plt.plot(df_plot['y'],label="Actual", linewidth=2.5)
plt.plot(df_plot['SES01'], label="Predict-SES01")
plt.plot(df_plot['SES05'], label="Predict-SES05")
plt.plot(df_plot['SES08'], label="Predicti-SES08")
sns.lineplot(fitted,x="ds", y="fitted01", label="Fitted01", linestyle="--", )  # '-', '--', '-.', ':',
sns.lineplot(fitted, x="ds", y="fitted05", label="Fitted05", color="yellow", linestyle="-.",)
sns.lineplot(fitted, x="ds", y="fitted08", label="Fitted08", color="fuchsia" ,linestyle=":")
plt.title("Ads watched (hourly data)");
Text(0, 0.5, '')

Let’s plot the same graph using the plot function that comes in Statsforecast, as shown below.

sf.plot(df, forecast_df)


In previous steps, we’ve taken our historical data to predict the future. However, to asses its accuracy we would also like to know how the model would have performed in the past. To assess the accuracy and robustness of your models on your data perform Cross-Validation.

With time series data, Cross Validation is done by defining a sliding window across the historical data and predicting the period following it. This form of cross-validation allows us to arrive at a better estimation of our model’s predictive abilities across a wider range of temporal instances while also keeping the data in the training set contiguous as is required by our models.

The following graph depicts such a Cross Validation Strategy:

Perform time series cross-validation

Cross-validation of time series models is considered a best practice but most implementations are very slow. The statsforecast library implements cross-validation as a distributed operation, making the process less time-consuming to perform. If you have big datasets you can also perform Cross Validation in a distributed cluster using Ray, Dask or Spark.

In this case, we want to evaluate the performance of each model for the last 30 hourly (n_windows=), forecasting every second months (step_size=30). Depending on your computer, this step should take around 1 min.

The cross_validation method from the StatsForecast class takes the following arguments.

  • df: training data frame

  • h (int): represents h steps into the future that are being forecasted. In this case, 30 hours ahead.

  • step_size (int): step size between each window. In other words: how often do you want to run the forecasting processes.

  • n_windows(int): number of windows used for cross validation. In other words: what number of forecasting processes in the past do you want to evaluate.

crossvalidation_df = sf.cross_validation(df=df,

The crossvaldation_df object is a new data frame that includes the following columns:

  • unique_id: index. If you dont like working with index just run crossvalidation_df.resetindex().
  • ds: datestamp or temporal index
  • cutoff: the last datestamp or temporal index for the n_windows.
  • y: true value
  • model: columns with the model’s name and fitted value.
12017-09-18 06:00:002017-09-18 05:00:0099440.0118499.953125109816.250112747.695312
12017-09-18 07:00:002017-09-18 05:00:0097655.0118499.953125109816.250112747.695312
12017-09-18 08:00:002017-09-18 05:00:0097655.0118499.953125109816.250112747.695312
12017-09-21 21:00:002017-09-20 17:00:00103080.0126112.898438140008.125139770.906250
12017-09-21 22:00:002017-09-20 17:00:0095155.0126112.898438140008.125139770.906250
12017-09-21 23:00:002017-09-20 17:00:0080285.0126112.898438140008.125139770.906250

Evaluate Model

We can now compute the accuracy of the forecast using an appropiate accuracy metric. Here we’ll use the Root Mean Squared Error (RMSE). To do this, we first need to install datasetsforecast, a Python library developed by Nixtla that includes a function to compute the RMSE.

!pip install datasetsforecast
from datasetsforecast.losses import rmse

The function to compute the RMSE takes two arguments:

  1. The actual values.
  2. The forecasts, in this case, Simple Exponential Smoothing Model (SES).
from datasetsforecast.losses import mse, mae, rmse

def evaluate_cross_validation(df, metric):
    models = df.drop(columns=['ds', 'cutoff', 'y']).columns.tolist()
    evals = []
    for model in models:
        eval_ = df.groupby(['unique_id', 'cutoff']).apply(lambda x: metric(x['y'].values, x[model].values)).to_frame() # Calculate loss for every unique_id, model and cutoff.
        eval_.columns = [model]
    evals = pd.concat(evals, axis=1)
    evals = evals.groupby(['unique_id']).mean(numeric_only=True) # Averages the error metrics for all cutoffs for every combination of model and unique_id
    evals['best_model'] = evals.idxmin(axis=1)
    return evals
evaluation_df = evaluate_cross_validation(crossvalidation_df, rmse)


We would like to thank Naren Castellon for writing this tutorial.


  1. Changquan Huang • Alla Petukhina. Springer series (2022). Applied Time Series Analysis and Forecasting with Python.
  2. James D. Hamilton. Time Series Analysis Princeton University Press, Princeton, New Jersey, 1st Edition, 1994.
  3. Nixtla Parameters.
  4. Pandas available frequencies.
  5. Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, Time series cross-validation”..
  6. Seasonal periods- Rob J Hyndman.