Skip to content

Instantly share code, notes, and snippets.

@Knk00
Last active June 26, 2020 11:20
Show Gist options
  • Save Knk00/7554b5cb82428d1673310cdc6d7786b8 to your computer and use it in GitHub Desktop.
Save Knk00/7554b5cb82428d1673310cdc6d7786b8 to your computer and use it in GitHub Desktop.
'''Helper Funciton to check if seasonality is present'''
def seasonality_test(self, series):
self.__seasonal__ = False
idx = np.arange(len(series.index)) % 12
H_statistic, p_value = kruskal(series, idx)
if p_value <= 0.05:
self.__seasonal__ = True
return seasonal
'''Helper Function to check if trend is present'''
def mkTest(series, seasonal):
if seasonal == False:
data_mk = mk.original_test(series)
trend = data_mk[0]
else:
data_mk_seasonal_test = mk.seasonal_test(series, period= 12)
trend = data_mk_seasonal_test[0]
if trend == 'decreasing' or trend == 'increasing':
self.__trend__ = 'present'
trend = 'present'
return trend
self.__trend__ = trend
return trend
def holtWinters_TES(self, train, test, trend, seasonal):
if: trend == 'present' and seasonal == True:
tes_add = ExponentialSmoothing(train, trend = 'add', seasonal = 'add', seasonal_periods= 12).fit().fittedvalues
tes_add_pred = tes_add.forecast(len(test))
rmse_tes_add = rootMeanSquaredError(test, tes_add_pred)
tes_mul = ExponentialSmoothing(train, trend = 'mul', seasonal = 'mul', seasonal_periods= 12).fit().fittedvalues
tes_mul_pred = tes_mul.forecast(len(test))
rmse_tes_mul = rootMeanSquaredError(test, tes_mul_pred)
if rmse_tes_add < rmse_tes_mul:
if rmse_tes_add < self.rmse:
self.rmse = rmse_tes_add
self.__model__ = 'holtWinters_TES_add'
self.__model_type__ = 'add'
else:
if rmse_tes_mul < self.rmse:
self.rmse = rmse_tes_mul
self.__model__ = 'holtWinters_TES_mul'
self.__model_type__ = 'mul'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment