def get_trend(tr_dict, return_fitted=False): """ method of trend calculation, which fit holt which returns also g_trend (i.e. the distance between y_holt - y_trajectory) :param tr_dict: :param return_fitted: :return: """ trend_dict = OrderedDict() res_dict = None if return_fitted: res_dict = OrderedDict() for patient, values in tr_dict.items(): fitted = Holt(values, damped_trend=True).fit(optimized=True, smoothing_level=0.7) # type: HoltWintersResults trend_dict[patient] = fitted.predict(start=0, end=len(values)-1) if return_fitted: res_dict[patient] = fitted.summary() return trend_dict, res_dict
### Simple Exponential Method ses_model = SimpleExpSmoothing(Train["Sales"]).fit(smoothing_level=0.9) pred_ses = ses_model.predict(start=Test.index[0], end=Test.index[-1]) MAPE(pred_ses, Test.Sales) ### Holt method # Holt method hw_model = Holt(Train["Sales"]).fit(smoothing_level=0.8, smoothing_trend=0.2) pred_hw = hw_model.predict(start=Test.index[0], end=Test.index[-1]) MAPE(pred_hw, Test.Sales) hw_model.summary() ### Holts winter exponential smoothing with additive seasonality and additive trend hwe_model_add_add = ExponentialSmoothing( Train["Sales"], seasonal="add", trend="add", seasonal_periods=4).fit() #add the trend to the model pred_hwe_add_add = hwe_model_add_add.predict(start=Test.index[0], end=Test.index[-1]) MAPE(pred_hwe_add_add, Test.Sales) hwe_model_add_add.summary() ### Holts winter exponential smoothing with multiplicative seasonality and additive trend hwe_model_mul_add = ExponentialSmoothing(Train["Sales"], seasonal="mul", trend="add", seasonal_periods=4).fit()
#forecasting/ predicting next 19 periods births_pred = births_ses.forecast(steps=19) print(births_pred) #Plot actual and forecast plt.plot(births) plt.plot(births_pred) plt.legend(['Actual', 'Forecast - SimpleExpSmoothing'], bbox_to_anchor=(1, 1), loc=2) plt.show() #Model with double exponential smoothing from statsmodels.tsa.holtwinters import Holt births_holt = Holt(births).fit() births_holt.summary() ''' Holt Model Results ============================================================================== Dep. Variable: births No. Observations: 168 Model: Holt SSE 280.134 Optimized: True AIC 93.899 Trend: Additive BIC 106.395 Seasonal: None AICC 94.421 Seasonal Periods: None Date: Fri, 26 Mar 2021 Box-Cox: False Time: 00:23:58 Box-Cox Coeff.: None ============================================================================== coeff code optimized ------------------------------------------------------------------------------ smoothing_level 0.4354824 alpha True smoothing_trend 1.8914e-12 beta True