def test_direct_holt_add(): mod = SimpleExpSmoothing(housing_data) res = mod.fit() x = np.squeeze(np.asarray(mod.endog)) alpha = res.params['smoothing_level'] l, b, f, err, xhat = _simple_dbl_exp_smoother(x, alpha, beta=0.0, l0=res.params['initial_level'], b0=0.0, nforecast=5) assert_allclose(l, res.level) assert_allclose(f, res.level.iloc[-1] * np.ones(5)) assert_allclose(f, res.forecast(5)) mod = ExponentialSmoothing(housing_data, trend='add') res = mod.fit() x = np.squeeze(np.asarray(mod.endog)) alpha = res.params['smoothing_level'] beta = res.params['smoothing_slope'] l, b, f, err, xhat = _simple_dbl_exp_smoother(x, alpha, beta=beta, l0=res.params['initial_level'], b0=res.params['initial_slope'], nforecast=5) assert_allclose(xhat, res.fittedvalues) assert_allclose(l + b, res.level + res.slope) assert_allclose(l, res.level) assert_allclose(b, res.slope) assert_allclose(f, res.level.iloc[-1] + res.slope.iloc[-1] * np.array([1, 2, 3, 4, 5])) assert_allclose(f, res.forecast(5))
def test_direct_holt_add(): mod = SimpleExpSmoothing(housing_data) res = mod.fit() x = np.squeeze(np.asarray(mod.endog)) alpha = res.params['smoothing_level'] l, b, f, err, xhat = _simple_dbl_exp_smoother( x, alpha, beta=0.0, l0=res.params['initial_level'], b0=0.0, nforecast=5) assert_allclose(l, res.level) assert_allclose(f, res.level.iloc[-1] * np.ones(5)) assert_allclose(f, res.forecast(5)) mod = ExponentialSmoothing(housing_data, trend='add') res = mod.fit() x = np.squeeze(np.asarray(mod.endog)) alpha = res.params['smoothing_level'] beta = res.params['smoothing_slope'] l, b, f, err, xhat = _simple_dbl_exp_smoother( x, alpha, beta=beta, l0=res.params['initial_level'], b0=res.params['initial_slope'], nforecast=5) assert_allclose(xhat, res.fittedvalues) assert_allclose(l + b, res.level + res.slope) assert_allclose(l, res.level) assert_allclose(b, res.slope) assert_allclose( f, res.level.iloc[-1] + res.slope.iloc[-1] * np.array([1, 2, 3, 4, 5])) assert_allclose(f, res.forecast(5))
def SES(data, Pre_day): model = SimpleExpSmoothing(data) model_fit = model.fit() yhat = model_fit.predict(row_num - Pre_day, row_num -1) # yhat = model_fit.predict(0, row_num - Pre_day-1) # print("======================SES========================") ''' x = np.arange(data.shape[0]) plt.scatter(x, data, marker='x') plt.plot(x, model_fit.predict(0, row_num - Pre_day-1)) plt.title('Actual test samples vs. forecasts') plt.show() ''' result = yhat return result
def w02_mm_pp_ses_w_statsmodels(): # NOTE: the ability to provide an initial value isn't available # in the currently released version; it looks like 0.10 (unreleased) # will support that from statsmodels.tsa.holtwinters import SimpleExpSmoothing s = build_w02_mm_pp_ma_ses_data() # fit model model = SimpleExpSmoothing(s.values) model_fit = model.fit(smoothing_level=0.33) # make prediction forecasts = model_fit.predict(3, 13) print(forecasts)
def plot(self, alpha): savg = [self.df['x'][0]] savgm = SimpleExpSmoothing(self.df) savgfit = savgm.fit(alpha, optimized=False) savg = savgfit.fittedvalues #print(savg) fig = plt.figure(figsize=(16, 8)) plt.plot(self.df['x'], label='Original Values', color="red") plt.plot(savg, label='Predicted Values Using Exponential Smoothing', color="black") plt.ylim(-10, 0) plt.title('Exponential Smoothing Model') plt.legend(loc='best', fancybox=True, ncol=2) plt.savefig('./sm_avg_plot') plt.clf()
def forecast(data, train_hours, test_hours): # Train on the first 6 days to predict the 7th day # In SES, Holt, etc that rely on the double-differenced data, we cannot # use the first 25 values. train = data.iloc[25:train_hours] # Create the SES Model model = SimpleExpSmoothing(np.asarray(train['seasonally differenced'])) # model._index = pd.to_datetime(train.index) # Fit the model, and forecast fit = model.fit() pred = fit.forecast(test_hours) data['ses'] = 0 data['ses'][25:] = list(fit.fittedvalues) + list(pred)
def cal_exp_smooth(self): # (b) Simple exponential smoothing self.get_single_y_ts() # fit model -- libraby bug: division by zero try: model = SimpleExpSmoothing(self.y_ts) model_fit = model.fit() # make prediction yhat = model_fit.predict(0, len(self.y_ts)) yhat[:self.lag] = np.nan self.train[f'es_unit_cost_{self.standard}'] = yhat.values except Exception as e: # print(s, p, c) # print('exp smoothing occurs error: divide by zero') print(e) self.train[f'es_unit_cost_{self.standard}'] = np.nan
def exponential_smoothing(ts, label=None): """Exponential smoothing methods assign exponentially decreasing weights for past observations. """ from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt locs, labels = plt.xticks() model = SimpleExpSmoothing(np.asarray(ts)) # auto optimization fit1 = model.fit() pred1 = fit1.forecast(2) fit2 = model.fit(smoothing_level=.25) pred2 = fit2.forecast(2) fit3 = model.fit(smoothing_level=.5) pred3 = fit3.forecast(2) plot_exponential_smoothing(ts, fit1, fit2, fit3, pred1, pred2, pred3, label, "Simple Exponential Smoothing",figures_dir) model = ExponentialSmoothing(np.asarray(ts), trend='add') # auto optimization fit1 = model.fit() pred1 = fit1.forecast(2) fit2 = model.fit(smoothing_level=.9) pred2 = fit2.forecast(2) fit3 = model.fit(smoothing_level=.5) pred3 = fit3.forecast(2) plot_exponential_smoothing(ts, fit1, fit2, fit3, pred1, pred2, pred3, label, "Exponential Smoothing",figures_dir) model = Holt(np.asarray(ts)) # auto optimization fit1 = model.fit(smoothing_level=.3, smoothing_slope=.05) pred1 = fit1.forecast(2) fit2 = model.fit(optimized=True) pred2 = fit2.forecast(2) fit3 = model.fit(smoothing_level=.3, smoothing_slope=.2) pred3 = fit3.forecast(2) plot_exponential_smoothing(ts, fit1, fit2, fit3, pred1, pred2, pred3, label, "Holt Exponential Smoothing",figures_dir) return fit3
def predict(timeseries): model = SimpleExpSmoothing(timeseries) model_fit = model.fit() yhat = model_fit.predict(len(timeseries), len(timeseries)) ses_result = yhat MA_16 = timeseries.rolling(window=16, center=True).mean() MA_40 = timeseries.rolling(window=40, center=True).mean() MA_81 = timeseries.rolling(window=81, center=True).mean() MA_162 = timeseries.rolling(window=162, center=True).mean() plt.plot(MA_16, color='yellow', label='MA - Tenth of Season') plt.plot(MA_40, color='blue', label='MA - Quarter of Season') plt.plot(MA_81, color='red', label='MA - Half Season') plt.plot(MA_162, color='black', label='MA - Full Season') plt.legend(loc=0) print('SES Result: ' + str(ses_result)) return
def OD_PR_LPAVG(Dataframe, HNAME_List, Raceday): """ Average Log Odds implied Probability Parameter --------- Matchday : Matchday Dataframe HNAME_List : String of List of Horse Names Raceday : Date of Race Return ------ Dataframe [HNAME, OD_PR_LPAVG] """ Feature_DF = Dataframe.loc[:, ['HNAME', 'RARID']] Extraction = Extraction_Database(""" Select HNAME, RARID, RESFO from RaceDb where RADAT < {Raceday} and HNAME in {HNAME_List} """.format(Raceday=Raceday, HNAME_List=HNAME_List)) Odds = [] for name, group in Extraction.groupby('HNAME'): Probi = group.loc[:, 'RESFO'].map(lambda x: np.log( (1 - 0.175) / x)).dropna().values if len(Probi) > 1: model = SimpleExpSmoothing(Probi) model = model.fit() Odds.append([name, model.forecast()[0]]) elif len(Probi) == 1: Odds.append([name, Probi[0]]) else: Odds.append([name, 0]) Odds = pd.DataFrame(Odds, columns=['HNAME', 'OD_PR_LPAVG']) Feature_DF = Feature_DF.merge(Odds, how='left') Feature_DF.loc[:, 'OD_PR_LPAVG'].fillna(Feature_DF.loc[:, 'OD_PR_LPAVG'].min(), inplace=True) Feature_DF.loc[:, 'OD_PR_LPAVG'].fillna(0, inplace=True) Feature_DF = Feature_DF.loc[:, ['HNAME', 'OD_PR_LPAVG']] return Feature_DF
def forecasting(model_name, resultsDict, predictionsDict, df, df_training, df_testcase): #index = len(df_training) yhat = list() for t in tqdm(range(len(df_testcase.Ambient_Temp))): temp_train = df[:len(df_training) + t] if model_name == "SES": model = SimpleExpSmoothing(temp_train.Ambient_Temp) elif model_name == "HWES": model = ExponentialSmoothing(temp_train.Ambient_Temp) elif model_name == "AR": model = AR(temp_train.Ambient_Temp) # elif model_name == "MA": # model = ARMA(temp_train.Ambient_Temp, order=(0, 1)) # elif model_name == "ARMA": # model = ARMA(temp_train.Ambient_Temp, order=(1, 1)) elif model_name == "ARIMA": model = ARIMA(temp_train.Ambient_Temp, order=(1, 0, 0)) elif model_name == "SARIMAX": model = SARIMAX(temp_train.Ambient_Temp, order=(1, 0, 0), seasonal_order=(0, 0, 0, 3)) model_fit = model.fit() if model_name == "SES" or "HWES": predictions = model_fit.predict(start=len(temp_train), end=len(temp_train)) elif model_name == "AR" or "ARIMA" or "SARIMAX": predictions = model_fit.predict(start=len(temp_train), end=len(temp_train), dynamic=False) yhat = yhat + [predictions] yhat = pd.concat(yhat) resultsDict[model_name] = metrics.evaluate(df_testcase.Ambient_Temp, yhat.values) predictionsDict[model_name] = yhat.values plt.plot(df_testcase.Ambient_Temp.values, label='Original') plt.plot(yhat.values, color='red', label=model_name + ' predicted') plt.legend() plt.show()
def plotsmooth(self,alpha): savg = [self.df['x'][0]] savgm = SimpleExpSmoothing(self.df) savgfit = savgm.fit(alpha,optimized=False) savg = savgfit.forecast(len(self.df1)) ## Test accuracy by taking RMSE mse = mean_squared_error(self.df1['x'], savg) rmse = sqrt(mse) print("RMSE for Simple Exponential Smoothing Model Forecasted Values:",rmse) #print(savg) fig = plt.figure(figsize=(16,8)) plt.plot(self.df['x'],label='Trained Data',color="red") plt.plot(self.df1['x'],label='Test Data',color="orange") plt.plot(savg,label='Forecasted Values Using Moving Average',color="black") plt.ylim(-10,0) plt.title('Exponential Smoothing Model') plt.legend(loc='best',fancybox=True,ncol=2) plt.savefig('./sm_avg_plot') plt.clf()
def smoothing(self): alist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] #alist = [0.1] rmse_list = [] '''for alpha in alist: savg = [self.df['x'][0]] for n in range(1, len(self.df)): savg.append(alpha * self.df['x'][n-1] + (1 - alpha) * savg[n-1]) mse = mean_squared_error(self.df['x'], savg) rmse = sqrt(mse) rmse_list.append(rmse) print(rmse_list) best_a = alist[np.argmin(rmse_list)] ##get index of the minimum mean to obtain best alpha value min_rmse = np.min(rmse_list) ##get minimum of root mean square value print("Best Min RMSE,Alpha:",min_rmse,best_a)''' ##another method here for alpha in alist: savg = [self.df['x'][0]] savgm = SimpleExpSmoothing(self.df) savgfit = savgm.fit(alpha, optimized=False) savg = savgfit.fittedvalues #print(savg) mse = mean_squared_error(self.df['x'], savg) rmse = sqrt(mse) #print("RMSE for alpha=0.1:",rmse) rmse_list.append(rmse) print(rmse_list) best_a = alist[np.argmin( rmse_list )] ##get index of the minimum mean to obtain best alpha value min_rmse = np.min(rmse_list) ##get minimum of root mean square value print("Best Min RMSE,Alpha using fit:", min_rmse, best_a) ##Ploting RMSE against K values plt.plot(alist, rmse_list) plt.title('RMSE vs alpha') plt.xlabel('a') plt.ylabel('RMSE') plt.savefig('./rmse_a') plt.clf() return best_a, min_rmse
def update(self): self.variance = round( self.beta * self.variance + (1 - self.beta) * abs(self.prediction - self.last_arrival_time), 2) # self.prediction = round(self.alpha * self.prediction + (1 - self.alpha) * self.last_arrival_time, 2) begin = max(0, self.index - self.window) data = self.arrivals[begin:self.index] # fit model model = SimpleExpSmoothing(data) model_fit = model.fit() # make prediction self.prediction = model_fit.predict(len(data), len(data))[0] # print('JACOB: ',self.prediction, ' ', self.variance) for key in self.constrains: new_timeout = round( self.prediction + self.constraints_to_K[key] * self.variance, 3) self.timeouts[key].append(new_timeout)
def CC_BWEI_D(Dataframe, HNAME_List, Raceday): """ Change in Bodyweight of Horse Parameter --------- Matchday : Matchday Dataframe HNAME_List : String of List of Horse Names Raceday : Date of Race Return ------ Dataframe [HNAME, CC_BWEI_D] """ Feature_DF = Dataframe.loc[:,['HNAME','RARID']] Extraction = Extraction_Database(""" Select HNAME, RARID, HBWEI from RaceDb where RADAT < {Raceday} and HNAME in {HNAME_List} """.format(Raceday = Raceday, HNAME_List = HNAME_List)) HBWEI = [] for name, group in Extraction.groupby('HNAME'): Weight = (group.loc[:,'HBWEI'].diff() / group.loc[:,'HBWEI']).dropna().values if len(Weight) >1: model = SimpleExpSmoothing(Weight) model = model.fit() HBWEI.append([name, model.forecast()[0]]) elif len(Weight) == 1: HBWEI.append([name,Weight[0]]) else : HBWEI.append([name,0]) HBWEI = pd.DataFrame(HBWEI, columns=['HNAME','CC_BWEI_D']) Feature_DF = Feature_DF.merge(HBWEI, how='left') Feature_DF.loc[:,'CC_BWEI_D'] = Feature_DF.loc[:,'CC_BWEI_D'].abs() Feature_DF.loc[:,'CC_BWEI_D'].fillna(Feature_DF.loc[:,'CC_BWEI_D'].max(), inplace = True) Feature_DF.loc[:,'CC_BWEI_D'].fillna(0, inplace = True) Feature_DF = Feature_DF.loc[:,['HNAME','CC_BWEI_D']] return Feature_DF
def CC_CLS_D(Dataframe, HNAME_List, Raceday): """ Change in HKJC Rating Parameter --------- Matchday : Matchday Dataframe HNAME_List : String of List of Horse Names Raceday : Date of Race Return ------ Dataframe [HNAME, CC_CLS_D] """ Feature_DF = Dataframe.loc[:,['HNAME','RARID']] Extraction = Extraction_Database(""" Select HNAME, RARID, HJRAT from RaceDb where RADAT < {Raceday} and HNAME in {HNAME_List} """.format(Raceday = Raceday, HNAME_List = HNAME_List)) JRat = [] for name, group in Extraction.groupby('HNAME'): Rating = (group.loc[:,'HJRAT'].diff() / group.loc[:,'HJRAT']).dropna().values if len(Rating) >1: model = SimpleExpSmoothing(Rating) model = model.fit() JRat.append([name, model.forecast()[0]]) elif len(Rating) == 1: JRat.append([name,Rating[0]]) else : JRat.append([name,0]) JRat = pd.DataFrame(JRat, columns=['HNAME','CC_CLS_D']) Feature_DF = Feature_DF.merge(JRat, how='left') Feature_DF.loc[:,'CC_CLS_D'].fillna(Feature_DF.loc[:,'CC_CLS_D'].min(), inplace = True) Feature_DF.loc[:,'CC_CLS_D'].fillna(0, inplace = True) Feature_DF = Feature_DF.loc[:,['HNAME','CC_CLS_D']] return Feature_DF
# yhatARIMA = modelARIMA_fit.predict(len(data), len(data), typ='levels') # print(yhatARIMA) # # End Autoregressive Integrated Moving Average # Seasonal Autoregressive Integrated Moving-Average (SARIMA) example # fit model modelSARIMA = SARIMAX(data, order=(1, 1, 1), seasonal_order=(1, 1, 1, 1)) modelSARIMA_fit = modelSARIMA.fit(disp=False) # make prediction yhatSARIMA = modelSARIMA_fit.predict(len(data), len(data)) print(yhatSARIMA) # End Seasonal Autoregressive Integrated Moving-Average # Simple Exponential Smoothing (SES) example # fit model modelSES = SimpleExpSmoothing(data) modelSES_fit = modelSES.fit() # make prediction yhatSES = modelSES_fit.predict(len(data), len(data)) print(yhatSES) # End Simple Exponential Smoothing (SES) # Holt Winter’s Exponential Smoothing (HWES) example # fit model modelHWES = ExponentialSmoothing(data) modelHWES_fit = modelHWES.fit() # make prediction yhatHWES = modelHWES_fit.predict(len(data), len(data)) print(yhatHWES) # End Holt Winter’s Exponential Smoothing (HWES)
# # Simple Forecasting # %% from statsmodels.tsa.holtwinters import SimpleExpSmoothing airpassengers_train = airpassengers_series[:-24] airpassengers_test = airpassengers_series[-24:] airpassengers_log_train = airpassengers_log[:-24] airpassengers_log_test = airpassengers_log[-24:] airpassengers_diff_train = airpassengers_diff[:-24] airpassengers_diff_test = airpassengers_diff[-24:] ses = SimpleExpSmoothing(airpassengers_diff_train[1:]) ses = ses.fit() ses_forecast = ses.forecast(24) # %% plt.plot(airpassengers_diff_train) plt.plot(ses_forecast) plt.title('forecast for next 24 month') # %% [markdown] # Inverse differencing # %% ses_forecast[0] = ses_forecast[0] + airpassengers_log_train[-1] ses_forecast_inv_diff = ses_forecast.cumsum()
def expos(self, hi, lo, input_period, train_period, output_period, pct_require): input_time = self.extract_time_type(input_period) train_time = self.extract_time_type(train_period) # extract column from data using column_no data = self.data.iloc[:, self.column_no - 1].dropna() data = pd.DataFrame(data) # convert from month to year if self.frequency[input_time[0]] >= self.frequency[train_time[0]]: data_check = data.copy() if train_period == 'week': previous_period = 'week' if train_period == 'month': previous_period = 'day' if train_period == 'quarter': previous_period = 'quarter' if train_period == 'year': previous_period = 'month' last_time = getattr(data.index[len(data) - 1], previous_period) # cutting data if it's less than 6 if last_time > self.frequency[input_period] / 2: data = self.arima_to_fill(data, last_time, self.frequency[previous_period], input_period) else: data = self.remove(data, last_time) data_info = self.data_info(data) # get portion if train_period == 'month' or train_period == 'year': percent_portion = self.percentage_type1( data_info, input_time[0], train_time[0]).reset_index() else: percent_portion = self.percentage_type2( data_info, input_time[0], train_time[0]).reset_index() else: print(input_time, ' cannot be converted to ', train_time, ' as it is smaller') # if(train_period=='week'): if train_period == 'week': data = self.resemble_week(data_info[self.name]) data = data[self.name].resample(self.time[train_period]).sum() if pct_require: data = self.percent_change(data, train_period).dropna() train = self.outlier(data, lower=lo, upper=hi) train = train.interpolate(method='cubic', order=3, s=0).ffill().bfill() model = SimpleExpSmoothing(train) model_fit = model.fit() future_forecast = model_fit.predict( len(train), len(train) + self.no_of_forecast_month - 1).reset_index() future_forecast = future_forecast.rename(columns={ 'index': 'Date', 0: self.name }) train = train.reset_index() train = train.append(future_forecast) # train = self.percent_change(train).reset_index() # train = pd.melt(train, id_vars=[train.columns[0]], value_vars=[train.columns[1]]) train = train[len(train) - self.no_of_forecast_month:len(train)].reset_index( drop=True) time_add = train_period + 's' if time_add == 'quarters': last_date = train['Date'][len(train) - 1] + relativedelta(months=4) else: last_date = train['Date'][len(train) - 1] + relativedelta(**{time_add: 1}) dummy_data = pd.DataFrame(columns=['Date', self.name]) dummy_data.loc[0] = [last_date, 0] train = train.append(dummy_data) train = train.set_index('Date') train_converted = train[self.name].resample(self.time[input_period], fill_method='ffill') train_converted = train_converted[:-1].reset_index() portion = percent_portion if self.frequency[input_time[0]] == self.frequency[train_time[0]]: portion[self.name] = 1 train_converted['Date'] = pd.to_datetime(train_converted['Date']) train_converted['month'] = pd.DatetimeIndex( train_converted['Date']).month join_data = train_converted.merge(percent_portion, left_on=input_period, right_on=input_period) join_data[self.name] = join_data[self.name + '_x'] * join_data[self.name + '_y'] join_data = join_data.sort_values(by=['Date']).set_index('Date') forecast_value = pd.Series(join_data[self.name], index=join_data.index) final = forecast_value.resample( self.time[output_period]).sum().reset_index() final = pd.melt(final, id_vars=[final.columns[0]], value_vars=[final.columns[1]]) print(final)
###################################################################### # SimpleExpSmoothing(endog) # SimpleExpSmoothing.fit(smoothing_level=None, # optimized=True, 默认优化未指定的参数 # start_params=None, # initial_level=None, 指定预测的初始值 # use_brute=True) 默认使用暴力寻找初始值 from statsmodels.tsa.holtwinters import SimpleExpSmoothing # 3.训练模型 # #######指定固定平滑系数 alpha = 0.8 mdl = SimpleExpSmoothing(ts) results = mdl.fit(smoothing_level=alpha, optimized=False) print(results.summary()) #打印模型信息 print('模型参数:\n', results.params) #返回参数字典 # 4.模型评估 # 1)查看评估指数 print('AIC=', results.aic) print('AICC=', results.aicc) print('BIC=', results.bic) # print('SSE=', results.sse) # resid = results.resid # 残差resid = ts - y_pred y_pred = results.fittedvalues # 预测历史值 y_true = ts[y_pred.index] displayRegressionMetrics(y_true, y_pred)
sd.resid sd.plot() #%% -------------------------------------------- # Train-test-split & Scaling # -------------------------------------------- split = round(len(df) * 0.8) train_df = df.iloc[:split] test_df = df.iloc[split:] # %%-------------------------------------------- # Simple Exponential Smoothing # -------------------------------------------- model = SimpleExpSmoothing(train_df[reservedWater]) fit_model = model.fit(optimized=True, use_brute=True) # predictions=fit_model.forecast(len(test_df)) predictions = fit_model.predict(start=test_df.index.values[0], end=test_df.index.values[-1]) def plot_results(df, col, fittedValues, title): ''' Objective: plots actual and fittedvalues Input: df: data as DataFrame col: column name, string fittedValues: fitted values ''' plt.figure(figsize=(12, 6))
def SES(base_df, steps): data = base_df['Number'].tolist() model = SimpleExpSmoothing(data) model_fit = model.fit() yhat = model_fit.predict(len(data), len(data) + steps - 1) return np.round(yhat)
train_store198_y_ets.index).Sales.sum() # Set Datetime Index to Frequency = Day (Frequency = 7) train_store198_y_ets.index.freq = 'D' # Fit Models # These 4 hyperparameters will be automatically tuned if optimized=True: # smoothing_level (alpha): the smoothing coefficient for the level. # smoothing_slope (beta): the smoothing coefficient for the trend. # smoothing_seasonal (gamma): the smoothing coefficient for the seasonal component. # damping_slope (phi): the coefficient for the damped trend. # Simple Exponential Smoothing ses_model_198 = SimpleExpSmoothing(train_store198_y_ets, initialization_method='estimated') ses_model_198 = ses_model_198.fit(optimized=True) # Holt (double) holt_model_198 = Holt(train_store198_y_ets, initialization_method='estimated') holt_model_198 = holt_model_198.fit(optimized=True) # Holt-Winters (triple) holt_winters_model_198 = ExponentialSmoothing( train_store198_y_ets, trend="add", seasonal="add", seasonal_periods=7, initialization_method='estimated') holt_winters_model_198 = holt_winters_model_198.fit(optimized=True) # Save and store training data
dir_1 = "C:/Users/priya/Desktop/IOT/project3/" filename = "psdiwaka.csv" path = os.path.join(dir_1, filename) data = pd.read_table(path, header=None, sep=" ") data.columns = ["Values"] train_size = int(len(data) * 0.75) train, test = data[0:train_size], data[train_size:len(data)] alpha = np.asarray(range(1, 10)) / 10 Y_SES = train.copy() rmselist = [] #Run the model for different alpha values for a in alpha: output = "SES" + str(a) model = SimpleExpSmoothing(train.values) model_fit = model.fit(a, optimized=True) Y_SES[output] = model_fit.fittedvalues #RMSE rmse = sqrt(mean_squared_error(Y_SES["Values"], Y_SES[output])) rmselist.append(rmse) #Determing alpha (smoothing factor) for which RMSE value is lowest minrmse = np.argmin(np.asarray(rmselist)) a = alpha[minrmse] print( "The best alpha value for the Exponential Smoothing Model based on lowest RMSE of {} is : {}" .format(round(rmselist[minrmse], 4), a)) #Plot RMSE vs K plt.plot(alpha, rmselist)
def SES(data1): from statsmodels.tsa.holtwinters import SimpleExpSmoothing model = SimpleExpSmoothing(data1) model_fit = model.fit() SESresult = model_fit.predict(len(data1), len(data1)) print('The period and the prediction from the SES Model is:', SESresult)
# 3. if not stationary, take lag-1, lag-2, etc. difference to see if smoothing occurs # 4. now ready for Stationary ARIMA modeling # look at link: https://towardsdatascience.com/the-complete-guide-to-time-series-analysis-and-forecasting-70d476bfe775 company = "AAPL" data = yf.Ticker(company).history('10y').reset_index() tsdata = data["Close"] ts_ma = tsdata.rolling(30).mean() # plt.plot(tsdata) # plt.plot(ts_ma) # plt.show() simp_model = SimpleExpSmoothing(tsdata) simp_model_fit = simp_model.fit() # print(simp_model_fit.summary()) double_model = ExponentialSmoothing(tsdata, trend='add') double_model_fit = double_model.fit(use_boxcox=True) # print(double_model_fit.summary()) # print(double_model_fit.forecast(10)) # print(double_model_fit.predict(1250,1300)) plt.plot(tsdata) plt.plot(double_model_fit.fittedvalues) plt.show() print(double_model_fit.fcastvalues)
plt.plot(train, label="training set", color=TSC) plt.plot(valid, label="validation set", color =VSC, linestyle = '--') plt.plot(rolmean, color=OLC, label='Rolling Mean', linewidth=3) plt.plot(rolstd, color=OLC, label='Rolling Std', linestyle = '--', linewidth=3) plt.legend(loc='best') plt.show(block=False) plt.plot() """ #%% #SIMPLE EXPONENTIAL SMOOTHING... # Creiamo il modello usando la classe SimpleExpSmoothing modelv1 = SimpleExpSmoothing(train) # Facciamo l'adattamento del modello ai dati fitted = modelv1.fit() # Creiamo le previsioni sullo stesso periodo del validation set forecasted = fitted.predict(start="2018-06-11", end="2019-09-29") # Calcolo gli intervalli di predizione per Simple Exponential Smoothing. # Sommiamo/Sottraiamo alle previsioni all'istante t-esimo il valore di # c*sigma dove questo valore rappresenta il 95% della guassiana. predint_xminus = ts[pd.date_range(start=ts.index[int(len(ts) * 0.8) + 1], end=ts.index[int(len(ts)) - 1], freq='D')] predint_xplus = ts[pd.date_range(start=ts.index[int(len(ts) * 0.8) + 1], end=ts.index[int(len(ts)) - 1], freq='D')]
def model_ES(key, train, test_shape = 0, train_flag = 0, test = []): predictions = [] rmse_val=[] try: train = train.values except: train = train history = [np.asscalar(x) for x in train] # TRAIN if train_flag==1: itr=5 data=pd.DataFrame(history) for i in range(3): pred_temp = [] v_train=[np.asscalar(x) for x in data[:-itr].values] v_expected=data.tail(itr).head(3).reset_index(drop = True) try: for t in range(3): if key=='SES': model = SimpleExpSmoothing(history) elif key=='HWES': model = ExponentialSmoothing(history) model_fit = model.fit() yhat= model_fit.predict(len(history), len(history)) if yhat < 0: yhat= mu.weighted_moving_average(history,1,3) yhat=yhat[0] pred_temp.append(yhat) v_train.append(yhat) except: pred_temp.extend(mu.moving_average(v_train, 3 - len(pred_temp), 3)) mu.plotting(key, pred_temp, v_expected) rmse_val.append(mu.calculate_rmse(key, v_expected, pred_temp)) if i == 2: predictions.extend(pred_temp) else: predictions.append(pred_temp[0]) itr=itr-1 # FORECAST else: try: for t in range(test_shape): if key=='SES': model = SimpleExpSmoothing(history) elif key=='HWES': model = ExponentialSmoothing(history) model_fit = model.fit() yhat= model_fit.predict(len(history), len(history)) if yhat < 0: yhat= mu.weighted_moving_average(history,1,3) yhat=yhat[0] predictions.append(yhat) history.append(yhat) except: predictions.extend(mu.moving_average(history, test_shape - len(predictions), 3)) predictions = [int(i) for i in predictions] return predictions,rmse_val
def train_ses(train, test): y_hat = test.copy() model = SimpleExpSmoothing(train[predict_label]) model_fit = model.fit() return model_fit, y_hat
# -*- coding: utf-8 -*- # @Time : 2020/7/27 16:47 # @Author : sen # SES example from statsmodels.tsa.holtwinters import SimpleExpSmoothing from random import random # contrived dataset data = [x + random() for x in range(1, 100)] print(data) # fit model model = SimpleExpSmoothing(data) model_fit = model.fit() # make prediction yhat = model_fit.predict(len(data), len(data)) print(yhat) # [99.23471016] data = [53, 54, 55, 56, 56, 57, 59, 60] print(data) # fit model model = SimpleExpSmoothing(data) model_fit = model.fit() # make prediction yhat = model_fit.predict(len(data), len(data)) print(yhat) #
import Constants import Logging airlinePassengerVolumeDF = pd.read_html ( Constants.readhtml ( Constants.AIRLINES ).text, index_col='Month', parse_dates=True ) airlinePassengerVolumeDF = airlinePassengerVolumeDF[0] airlinePassengerVolumeDF = airlinePassengerVolumeDF.drop ( 'Unnamed: 0', axis=1 ) airlinePassengerVolumeDF = airlinePassengerVolumeDF.dropna () span = 12 alpha = 2 / (span + 1) airlinePassengerVolumeDF['EWMA'] = airlinePassengerVolumeDF['Thousands of Passengers'].ewm ( alpha=alpha, adjust=False ).mean () model = SimpleExpSmoothing ( airlinePassengerVolumeDF['Thousands of Passengers'] ) fitted_model = model.fit ( optimized=False, smoothing_level=alpha ) airlinePassengerVolumeDF['SES12'] = fitted_model.fittedvalues.shift ( -1 ) toJSON = airlinePassengerVolumeDF.head ( 5 ).to_json () print ( toJSON ) parsed = json.loads ( toJSON ) print ( parsed ) Logging.test_logger.info ( 'After adding the SES: {} '.format ( parsed ) ) ax = airlinePassengerVolumeDF['EWMA'].plot ( label='EWMA' ) airlinePassengerVolumeDF['SES12'].plot ( label='SES12' ) ax = ax.set ( xlabel=Constants.TIME, ylabel='PassengerVolume ( Thousands)' ) plt.show () airlinePassengerVolumeDF['DES12'] = ExponentialSmoothing ( airlinePassengerVolumeDF['Thousands of Passengers'], trend='add' ).fit ().fittedvalues.shift ( -1 ).plot ( label='Normal exponentialSmoothing' )