Holt-Winters time series forecasting with statsmodels

后端 未结 2 1722
一向
一向 2020-12-24 15:12

I tried forecasting with holt-winters model as shown below but I keep getting a prediction that is not consistent with what I expect. I also showed a visualizat

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 15:50

    The main reason for the mistake is your start and end values. It forecasts the value for the first observation until the fifteenth. However, even if you correct that, Holt only includes the trend component and your forecasts will not carry the seasonal effects. Instead, use ExponentialSmoothing with seasonal parameters.

    Here's a working example for your dataset:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from statsmodels.tsa.holtwinters import ExponentialSmoothing
    
    df = pd.read_csv('/home/ayhan/international-airline-passengers.csv', 
                     parse_dates=['Month'], 
                     index_col='Month'
    )
    df.index.freq = 'MS'
    train, test = df.iloc[:130, 0], df.iloc[130:, 0]
    model = ExponentialSmoothing(train, seasonal='mul', seasonal_periods=12).fit()
    pred = model.predict(start=test.index[0], end=test.index[-1])
    
    plt.plot(train.index, train, label='Train')
    plt.plot(test.index, test, label='Test')
    plt.plot(pred.index, pred, label='Holt-Winters')
    plt.legend(loc='best')
    

    which yields the following plot:

提交回复
热议问题