def holt(): forecast_steps = 100 fit1 = Holt(origin_series).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) forecast1 = fit1.forecast(forecast_steps).rename("Holt's linear trend") fit2 = Holt(origin_series, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) forecast2 = fit2.forecast(forecast_steps).rename("Exponential trend") fit3 = Holt(origin_series, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2, damping_slope=0.8) forecast3 = fit3.forecast(forecast_steps).rename("Additive damped trend") fit1.fittedvalues.plot(marker="o", color='blue') forecast1.plot(color='blue', marker="o", legend=True) # plt.show() fit2.fittedvalues.plot(marker="o", color='red') forecast2.plot(color='red', marker="o", legend=True) # plt.show() fit3.fittedvalues.plot(marker="o", color='green') forecast3.plot(color='green', marker="o", legend=True) plt.show()
def holt_prediction(data, time_steps): #setting paramets for the linear regression method, etc. fit1 = Holt(data).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) #actaul forecast... what will be printed on the graph fcast1 = fit1.forecast(time_steps).rename("Holt's linear trend") fit2 = Holt(data, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) fcast2 = fit2.forecast(time_steps).rename("Exponential trend") fit3 = Holt(data, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2) fcast3 = fit3.forecast(time_steps).rename("Additive damped trend") return fit1, fit2, fit3, fcast1, fcast2, fcast3
def holts_linear_trend(input_df, kunag, matnr, smoothing_level=0.3, smoothing_slope=0.1): df = input_df.copy() df = remove_negative_rows(df) df_series = individual_series(df, kunag, matnr) df_series = data_transformation.get_weekly_aggregate(df_series) df_series["date"] = df_series["dt_week"].map(str) df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", "")) df_series["prediction"] = df_series["quantity"] df_series_train, df_series_test = splitter(df_series) k = 0 for index, row in df_series_test.iterrows(): df_series_train["quantity"] = df_series_train["quantity"].map(float) fit1 = Holt(np.asarray(df_series_train["quantity"])).fit( smoothing_level=smoothing_level, smoothing_slope=smoothing_slope) predicted = fit1.forecast(1) row["prediction"] = predicted[0] df_series_train = pd.concat([df_series_train, pd.DataFrame(row).T ]).reset_index(drop=True) if k == 0: test_index = df_series_train.shape[0] - 1 k = 1 output_df = df_series_train test_df = df_series_train.iloc[test_index:] # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"])) return output_df, mean_squared_error(test_df["quantity"], test_df["prediction"])
def holts_linear(input_df, kunag, matnr, n, sl, ss): i = 0 lst = [] test1 = train_test_split(df, kunag, matnr, n)[1] y_hat_avg = test1.copy() for i in range(n, 0, -1): train, test = train_test_split(df, kunag, matnr, i) dd = np.asarray(train["quantity"]) fit1 = Holt(np.asarray(train['quantity'])).fit(smoothing_level=sl, smoothing_slope=ss) y_hat_avg['Holt_linear'] = fit1.forecast(len(test1)) pred = y_hat_avg['Holt_linear'] lst.append(pred.iloc[-1]) pd.DataFrame(lst) y_hat_avg['pred_column'] = lst plt.figure(figsize=(12, 8)) plt.plot(train.set_index("date")['quantity'], label='Train', marker='.') plt.plot(test1.set_index("date")['quantity'], label='Test', marker='.') plt.plot(y_hat_avg.set_index("date")['pred_column'], label='Holts linear', marker='.') plt.legend(loc='best') plt.title("Holts linear") plt.show() rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column)) mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column) del y_hat_avg['Holt_linear'] return y_hat_avg, rms, mae
def HOLT(df): train = df[0:20] test = df[20:] rms = 0.0 num = 0 for id_idx in range(1, 25, 1): if train['id' + str(id_idx)].sum() > 30: fit = Holt(train['id' + str(id_idx)]).fit(smoothing_level=0.3, smoothing_slope=0.1, optimized=False) fcast = fit.forecast(len(test)) plt.plot(train['id' + str(id_idx)], marker='o', label='train_id' + str(id_idx)) plt.plot(test['id' + str(id_idx)], marker='o', label='test_id' + str(id_idx)) plt.plot(fcast, marker='o', label='holt_linear' + str(id_idx)) plt.legend(loc='best') rms = rms + math.sqrt( mean_squared_error(test['id' + str(id_idx)], fcast)) num = num + 1 plt.show() mean_error = rms / num print(mean_error)
def date_forecast(date, min_list): ddate = date["y_year"].values.tolist() list_value = date["list_value"].values.tolist() data = date.set_index('y_year') data['list_value'] = data['list_value'].astype('float64') ddate_diff = 20 - len(ddate) + 1 test = date # if len(date) <= 12 and len(date) >5: # test = data[12-len(data):12] # elif len(data) <= 3: # test = date.copy() # else: # test = data[len(data)-12:len(data)] # print(test) # 这里是设置特征函数,规划出一条一元一次方程,出生情况直接按照所有数据做出一条直线 fit = Holt(np.asarray(test['list_value'])).fit(smoothing_level=0.3, smoothing_slope=0.1) # 这里预测截止到2020年12月的数据 # num_list = fit.forecast(len(test)+ddate_diff+1)[:len(test)+1] num_list = fit.forecast(ddate_diff) # 这里存在一个问题,他不是所有的数据都是从02年开始的,要么就是补全,要么就是全部设为0 for i in num_list: list_value.append(i) # 这里是按照02年开始取数,补全前面的删除后面的 if min_list != None: list_value = get_zero_list(min_list) + list_value list_value = list_value[:21] return list_value
def WalkForwardCV_HOLT(param, X): n_train = len(X) // 2 n_records = len(X) error_list = [] aic_list = [] for i in range(n_train, n_records): # Split train and test train, test = X[0:i], X[i:i + 1] # Fit Holt's linear model fit1 = Holt(train).fit(smoothing_level=param[0], smoothing_slope=param[1]) # predict next day fcast1 = fit1.forecast(1) # calculate error error = MAPE(fcast1, test) error_list.append(error) # obtain AIC aic_list.append(fit1.aic) return np.mean(error_list), np.mean(aic_list), fit1
def hello_world(): dataset = pd.read_csv('count_people.csv') train = dataset import matplotlib.pyplot as plt import statsmodels.api as sm from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt data = [] for i in dataset['col']: data.append(int(i)) test = dataset[-5:] y_hat_avg = test.copy() fit1 = Holt(np.asarray(train['col'])).fit(smoothing_level=0.3, smoothing_slope=0.1) y_hat_avg['Holt_linear'] = fit1.forecast(len(test)) for i in y_hat_avg['Holt_linear']: data.append(int(i)) setGraf12(data) #кОЛИЧЕСВО ЧЕЛОВЕК dataset = pd.read_csv('money.csv') train = dataset data = [] for i in dataset['col']: data.append(int(i)) test = dataset[-5:] y_hat_avg = test.copy() fit1 = Holt(np.asarray(train['col'])).fit(smoothing_level=0.3, smoothing_slope=0.1) y_hat_avg['Holt_linear'] = fit1.forecast(len(test)) for i in y_hat_avg['Holt_linear']: data.append(int(i)) setGraf13(data) #Деньги dataset = pd.read_csv('passagers.csv') train = dataset data = [] for i in dataset['col']: data.append(int(i)) test = dataset[-5:] y_hat_avg = test.copy() fit1 = Holt(np.asarray(train['col'])).fit(smoothing_level=0.3, smoothing_slope=0.1) y_hat_avg['Holt_linear'] = fit1.forecast(len(test)) for i in y_hat_avg['Holt_linear']: data.append(int(i)) print(data) setGraf14(data) return render_template('index.html',first_graf_link = "/",second_graf_link ="/second",title = "Holt")
def HOLT(): fit1 = Holt(df.sales_result).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) fcast1 = fit1.forecast(12).rename("Holt's linear trend") fit2 = Holt(df.sales_result, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) fcast2 = fit2.forecast(12).rename("Exponential trend") fit3 = Holt(df.sales_result, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2) fcast3 = fit3.forecast(12).rename("Additive damped trend") fit1.fittedvalues.plot(marker="o", color='blue') fcast1.plot(color='blue', marker="o", legend=True) fit2.fittedvalues.plot(marker="o", color='red') fcast2.plot(color='red', marker="o", legend=True) fit3.fittedvalues.plot(marker="o", color='green') fcast3.plot(color='green', marker="o", legend=True) plt.show()
def holt(y,y_to_train,y_to_test,smoothing_level,smoothing_slope, predict_date): y.plot(marker='.', color='black', legend=True, figsize=(14, 7)) fit1 = Holt(y_to_train).fit(smoothing_level, smoothing_slope, optimized=False) fcast1 = fit1.forecast(predict_date).rename("Holt's linear trend") mse1 = ((fcast1 - y_to_test) ** 2).mean() print('The Root Mean Squared Error of Holt''s Linear trend {}'.format(round(np.sqrt(mse1), 2))) fit2 = Holt(y_to_train, exponential=True).fit(smoothing_level, smoothing_slope, optimized=False) fcast2 = fit2.forecast(predict_date).rename("Exponential trend") mse2 = ((fcast2 - y_to_test) ** 2).mean() print('The Root Mean Squared Error of Holt''s Exponential trend {}'.format(round(np.sqrt(mse2), 2))) fit1.fittedvalues.plot(marker=".", color='blue') fcast1.plot(color='blue', marker=".", legend=True) fit2.fittedvalues.plot(marker=".", color='red') fcast2.plot(color='red', marker=".", legend=True) plt.show()
def date_forecast(date): ddate = date["ddate"].values.tolist() data = date.drop('inum_person', 1).set_index('ddate').drop('ccusname', 1) data['inum_person_true'] = data['inum_person_true'].astype('float64') ddate_diff = months('2020-12-01', str(ddate[len(ddate) - 1])) mon_list = y_mon(ddate[len(ddate) - 1]) mon_list = list(reversed(mon_list)) # print(mon_list) test = [] test = data[len(data) - 12:len(data)] # test = data y_hat_avg = test.copy() # 这里是设置特征函数,规划出一条一元一次方程 # fit = Holt(np.asarray(test['inum_person_true'])).fit(smoothing_level=0.4, smoothing_slope=0.1) print(test) if len(test) <= 12: # fit = ExponentialSmoothing(np.asarray(test['inum_person_true']), seasonal_periods=3, trend='add', seasonal='add', ).fit() fit = Holt(np.asarray(test['inum_person_true'])).fit( smoothing_level=0.1, smoothing_slope=0.1) else: fit = Holt(np.asarray(test['inum_person_true'])).fit( smoothing_level=0.6, smoothing_slope=0.1) # fit = ExponentialSmoothing(np.asarray(test['inum_person_true']), seasonal_periods=12, trend='add', seasonal='add', ).fit() # 这里预测截止到2020年12月的数据 num_list = fit.forecast(len(test) + ddate_diff + 1)[len(test) + 1:] # print(num_list) y_hat_avg['Holt_linear'] = fit.forecast(len(test)) # print(y_hat_avg) # plt.figure(figsize=(8, 6)) # plt.plot(data['inum_person_true'], label='Train') # plt.plot(test['inum_person_true'], label='Test') # plt.plot(y_hat_avg['Holt_linear'], label='Holt_linear') # plt.legend(loc='best') # # plt.show() # 训练集数据和预测数据放到一起 ddate_fin = [str(i) for i in ddate] + mon_list inum_person = date["inum_person_true"].values.tolist() for i in num_list: inum_person.append(i) c = {'ddate': ddate_fin, 'inum_person': inum_person} return DataFrame(c)
def Train(train_data, real_data, prediction_days): fit1 = Holt(train_data).fit() fit2 = Holt(train_data, exponential=True, damped=True).fit() fcast1 = fit1.forecast(prediction_days) fcast2 = fit2.forecast(prediction_days) plot_time_series( { r'Real Data': [real_data, None], r'4.Additive, $\alpha=%0.2f$ & $\beta=%0.2f$' % (fit1.params['smoothing_level'], fit1.params['smoothing_slope']): [fcast1, None], r'5.Multiplicative, $\theta=%0.2f$ ' % fit2.params['damping_slope']: [fcast2, None] }, 'Prediction of COVID-19 Cases in Kurdistan-Region,Iraq\n Using Holt’s Methods on Cumulative Data\n', 'holt_models_default') return [fit1, fit2]
def next_two_weeks_Holt(df): import matplotlib.pyplot as plt from statsmodels.tsa.api import Holt df = df.set_index('Date') final = pd.DataFrame() for var in df.columns: model = Holt(df[var]).fit(smoothing_level=.3, smoothing_slope=.1, optimized=False) final[var] = pd.Series(model.forecast(14)) return final
def forecast_holt(ticker, data): ticker = str(ticker).upper() dataset = data[ticker] X = dataset.values year = 365 diff = difference(X, year) model = Holt(diff).fit(smoothing_level=0.3, smoothing_slope=0.1) fc_holt = model.forecast() fc_holt = inverse_difference(X, fc_holt, year) return fc_holt[0]
def holttm(i): df = i train = np.asarray(df.iloc[:(round(len(df) * .85)), 0]) hell = df.iloc[(round(len(df) * .85)):, 0] fit1p = Holt(train).fit(smoothing_level=0.3, smoothing_slope=0.13, optimized=False) fcastp = fit1p.forecast(len(hell)) sreal = (sum(hell)) spred = (sum(fcastp)) mape = calculomape(sreal, spred) return (mape)
def ts_holt(train, test, **kwargs): yhat = pd.DataFrame(dict(actual=test)) sm.tsa.seasonal_decompose(train).plot() result = sm.tsa.stattools.adfuller(train) plt.show() holt = Holt(train).fit(**kwargs) yhat["holt_linear"] = holt.forecast(test.shape[0]) plot_and_eval(train, test, yhat.holt_linear, test) return holt
class HoltLearner(BaseEstimator): def __init__(self, dayone, dayout, smoothing_level=0.5, smoothing_slope=0.05, date_string='%m-%d-%Y', date_string_output='%a, %d %b %Y %H:%m:%S'): self.smoothing_level = smoothing_level self.smoothing_slope = smoothing_slope self.dayone = dayone self.dayout = dayout self.date_string = date_string self.date_string_output = date_string_output def fit(self, df): self.df = ajust_df(df) self.model = Holt(self.df['y'], exponential=True).fit(smoothing_level=0.5, smoothing_slope=0.05, optimized=False) return self def predict(self, forecast=1): series_out = self.model.forecast(forecast) # Format date: # Count the number of days up now days_before = days_until_now(dayone=self.dayone, dayout=self.dayout, date_string=self.date_string) # Count the days ahead from now days_future = [ x for x in range(days_before + 1, forecast + days_before + 2) ] # base = datetime.today() date_list = [ datetime.strftime(self.dayout + timedelta(days=x), '%m-%d-%y') for x in range(forecast + 1) ] # df_out = pd.DataFrame(index=series_out.index.strftime('%d/%m/%Y'), columns=['yhat'], # data=series_out.values) # df_out.index.name = 'ds' df_out = pd.DataFrame(columns=['yhat']) df_out['ds'] = format_date(date_list[1:], date_string_output=self.date_string_output) df_out['yhat'] = series_out.values return df_out
def holtt(i, fc_periods, df): df = df hell = np.asarray(df.iloc[0:, 0]) train = df.iloc[394:, 0] modelo = SimpleExpSmoothing(hell).fit(smoothing_level=.2, optimized=False) resultado = modelo.fittedvalues df['pronostico'] = resultado nombre = list(df.columns.values.tolist()) fit1 = Holt(hell).fit(smoothing_level=0.35, smoothing_slope=0.1, optimized=True) print(fit1.summary()) fcast1 = fit1.forecast(fc_periods) return (fcast1)
def holts_linear(train, test, column, smoothing, slope): #holt linear technique with smoothing and slope as parameters y_hat_avg = test.copy() start = time.time() model_hl = Holt(np.asarray(train[column])).fit(smoothing_level = smoothing, smoothing_slope = slope) y_hat_avg['holt_linear']= model_hl.forecast(len(test)) y_hat_avg['transformed_values'] = inverse_transform(y_hat_avg,'holt_linear') rmse_hl = sqrt(mean_squared_error(y_hat_avg[column], y_hat_avg['transformed_values'])) end = time.time() time_hl = end - start print('\n Total RMSE of the holt linear model : %.3f ' % rmse_hl) result.loc[len(result)] = [column, 'Holts Linear', rmse_hl, time_hl] #test_plt((20,10), test_df, predictions, column, 'Holts Linear') result_plots['Holts_linear'] = y_hat_avg['transformed_values']
def HLM_model(train,test): #alpha=smoothing_level and beta=smoothing slope fit1 = Holt(train).fit(smoothing_level = 0.3,smoothing_slope = 0.1) fcast=fit1.forecast(len(test)) plt.figure(figsize=(18,8)) plt.plot(train,label='train data',color='black') plt.plot(test,label='test data',color='green') plt.plot(fcast,label='forecast',color='red') plt.legend(loc='best') plt.title('Load Forecast using HLM trend Method',fontsize=15) plt.xlabel('day----->') plt.ylabel('Consumption in Mwh') plt.show() print("Verification of HLM(trend) Forecasting Model") modelverification(fit1,fcast,test) return(fit1)
def holt(): N, t = 200, 160 realisations = pd.Series(list(sample_random_walk(0, 0.1, N)), range(N)) mod = Holt(realisations[:t + 1]).fit(optimized=True) params = [ 'smoothing_level', 'smoothing_slope', 'initial_level', 'initial_slope' ] results = pd.DataFrame(index=["alpha", "beta", "l_0", "b_0", "SSE"], columns=["Holt's"]) results["Holt's"] = [mod.params[p] for p in params] + [mod.sse] print(results) forecasts = mod.forecast(N - (t + 1)).rename(r'$\alpha=0.5$ and $\beta=0.5$') plot(realisations, pd.Series(np.nan, range(t + 1)).append(forecasts)) plot_components(mod) py.show()
def act(self, timeseries): optimal_weights = [] for asset in timeseries.columns: ts = timeseries[asset] fit1 = Holt(ts).fit() forecast = fit1.forecast(self.forecast_horizon) prediction = forecast.values[-1] - forecast.values[0] optimal_weights.append(prediction) if self.allow_short: optimal_weights /= sum(np.abs(optimal_weights)) else: optimal_weights += np.abs(np.min(optimal_weights)) optimal_weights /= sum(optimal_weights) return optimal_weights
def holt(df, chunk=100, smoothing=0.03, slope=0.1): cnt = int(len(df.index) / chunk) for i in range(cnt): if i == cnt: print('final') else: train = df.iloc[i * chunk:(i + 1) * chunk] test = df.iloc[(i + 1) * chunk:(i + 2) * chunk] fit = Holt(asarray(train)).fit(smoothing_level=smoothing, smoothing_slope=slope) forecast = fit.forecast(len(test)) try: rms = sqrt(mean_squared_error(test, forecast)) print('RMS %s' % rms) except: pass plt.plot(train, color='b') plt.plot(test.index, forecast, lw=3, color='r') plt.show()
def holts_linear(input_df, kunag, matnr, n, sl, ss): i = 0 lst = [] test1 = train_test_split(df, kunag, matnr, n)[1] y_hat_avg = test1.copy() for i in range(n, 0, -1): train, test = train_test_split(df, kunag, matnr, i) dd = np.asarray(train["quantity"]) fit1 = Holt(np.asarray(train['quantity'])).fit(smoothing_level=sl, smoothing_slope=ss) y_hat_avg['Holt_linear'] = fit1.forecast(len(test1)) pred = y_hat_avg['Holt_linear'] lst.append(pred.iloc[-1]) pd.DataFrame(lst) y_hat_avg['pred_column'] = lst rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column)) mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column) del y_hat_avg['Holt_linear'] return y_hat_avg, rms, mae
async def get_holt_finish_getnumber(first_list, date_list): try: data_series = pd.Series(first_list, date_list) fit = Holt(data_series).fit(smoothing_level=0.8, smoothing_trend=0.2, optimized=False) test_forecast=[] forecast_message="" for i in range(1,1000): test_forecast = fit.forecast(i) res = all(i < j for i, j in zip(test_forecast, test_forecast[1:])) if(test_forecast[-1]<=0 and str(res)): forecast_message="Tahmini Bitiş Tarihi" fcast = i break else: fcast = 100 forecast_message = "100 Gün Sonrasının Tahmini" except UnboundLocalError as e: raise e return fcast, forecast_message
def holt_(df,x,y,n): df["ym"] = pd.to_datetime(df.ym) if len(df) <= 7: df_tsa = df.copy() df_tsa["mark_forecast"] = "ori" else: df_1 = df.copy() df_1["mark_forecast"] = "ori" fit = Holt(np.asarray(df['inum_person_ad'])).fit(smoothing_level=x, smoothing_slope=y) df_2 = pd.DataFrame(columns = df_1.columns) dt_list = [] dt_last = max(df["ym"]) for i in range(1,n+1): dt_list.append(add_month(dt_last,i)) df_2['ym'] = dt_list df_2['inum_person_ad'] = fit.forecast(n) df_2["mark_forecast"] = "forecast" df_2['ccuscode'] = max(df_1['ccuscode']) df_2['item_code'] = max(df_1['item_code']) df_tsa = pd.concat([df_1,df_2],ignore_index=True) return df_tsa
def predict(self, country, tau, r_hubei, stringency): forcast_cnt = len(stringency) if country == 'israel': # fill hubei if 0 < forcast_cnt + len( self.r_adj) - len(r_hubei): ## TODO Shold use time series r_hubei = r_hubei.append(r_hubei[-1] * (forcast_cnt - len(r_hubei))) # StringencyIndex self.df_tmp['StringencyIndex'].fillna(method='ffill', inplace=True) tmp = self.df_tmp.copy() # normalize to day_0 and then shift forward 7 days so dont need to lag in regression tmp['StringencyIndex'].shift(periods=-(self.day_0 - 7)).fillna( method='ffill', inplace=True) cur_stringency = tmp['StringencyIndex'].values stringency = stringency['StringencyIndex'].values stringency = np.append(cur_stringency, stringency) self.r_predicted = [0] for t in range(1, len(self.r0d) + forcast_cnt + 7): if t <= 2: projected_r = self.r0d[t] else: projected_r = self.crystal_ball_regression( self.r0d[t - 1], r_hubei[t - 1], r_hubei[t - 2], stringency[min(t, len(stringency) - 1)]) if t >= len(self.r0d): self.r0d = np.append(self.r0d, projected_r) self.r_predicted = np.append(self.r_predicted, projected_r) else: holt_model = Holt(self.r_adj[-tau:], exponential=True).fit(smoothing_level=0.1, smoothing_slope=0.9) self.r0d = np.append(self.r_adj, holt_model.forecast(forcast_cnt + 1)) self.r0d = np.clip(self.r0d, 0, 100)
def holts_linear(input_df, kunag, matnr, n, sl, ss): path = "/home/rahul/Downloads/bharat/time_series1/model_graphs/" folder = path + '/holts_linear/' + "sl" + str(sl) + '_' + "ss" + str(ss) if not os.path.exists(folder): os.makedirs(folder) index = str(kunag) + "-" + str(matnr) i = 0 lst = [] test1 = train_test_split(df, kunag, matnr, n)[1] y_hat_avg = test1.copy() for i in range(n, 0, -1): train, test = train_test_split(df, kunag, matnr, i) dd = np.asarray(train["quantity"]) fit1 = Holt(np.asarray(train['quantity'])).fit(smoothing_level=sl, smoothing_slope=ss) y_hat_avg['Holt_linear'] = fit1.forecast(len(test1)) pred = y_hat_avg['Holt_linear'] lst.append(pred.iloc[-1]) pd.DataFrame(lst) y_hat_avg['pred_column'] = lst plt.figure(figsize=(12, 8)) plt.plot(train.set_index("date")['quantity'], label='Train', marker='.') plt.plot(test1.set_index("date")['quantity'], label='Test', marker='.') plt.plot(y_hat_avg.set_index("date")['pred_column'], label='Holts linear', marker='.') plt.legend(loc='best') plt.title("Holts linear") plt.savefig(folder + "/" + 'Graph_{}.png'.format(index), format="PNG") plt.show() rms = sqrt(mean_squared_error(test1.quantity, y_hat_avg.pred_column)) mae = mean_absolute_error(test1.quantity, y_hat_avg.pred_column) del y_hat_avg['Holt_linear'] return y_hat_avg, rms, mae
def linear_holt(train, test): lin_h = Holt(train).fit() yhat = lin_h.forecast(test.shape[0]) mse = mean_squared_error(test, yhat) rmse = mse**.5 return {'model_type': 'holt', 'mse': mse, 'rmse': rmse}, yhat # def prophet(train,test): # mod_train = pd.DataFrame(train) # mod_test = pd.DataFrame(test) # mod_train['y'] = train # mod_test['y'] = test mod_train['ds'] = train.index mod_test['ds'] = test.index model = Prophet() model.fit(mod_train) yhat = model.predict(mod_test).yhat mse = mean_squared_error(test, yhat) rmse = mse**.5 return {'prophet': 'moving_avg', 'mse': mse, 'rmse': rmse}
def forecasting(real_time_data): # Import library from statsmodels.tsa.api import Holt # Extract out the Thresholds from the Dictionary tag = ''.join((pd.DataFrame(real_time_data).columns).tolist()) lower_thr = Mythresholds[tag]['val_lower'] print('val_lower: ' + str(lower_thr)) upper_thr = Mythresholds[tag]['val_upper'] print('val_upper: ' + str(upper_thr)) print('') # dataset for training exponential smoothing if len(real_time_data) > 1: fit = Holt(real_time_data, damped=True).fit(smoothing_level=0.2, smoothing_slope=0.04) fcast = fit.forecast(8).rename("Forcasted value") print('Forcasted value: ' + str(fcast.tolist())) print( '------------------------------------------------------------------------------------------------------------------' ) plt.figure(figsize=[20, 5]) fit.fittedvalues.plot(color='green') fcast.plot(color='indigo', legend=True) plt.plot(real_time_data, color="black") plt.axhline(upper_thr, color='red', linestyle='--') plt.axhline(lower_thr, color='red', linestyle='--') plt.grid() # Behaviour of data in real time current_value = real_time_data[len(real_time_data) - 1] print('Actual current_value: ' + str(current_value)) # calculate & storing change after every iteration if len(real_time_data) > 1: current_value = real_time_data[len(real_time_data) - 1] previous_value = real_time_data[len(real_time_data) - 2] change = current_value - previous_value # percentage change after every iteration if len(real_time_data) > 1: current_perc = round((change / (upper_thr - lower_thr)) * 100, 2) storing_percentage_value.append(current_perc) if (current_perc > 10): print( 'The change in % has been seen more than 10% in last hours :' + str(current_perc) + ' %') else: print('The Change in % has been seen in last hours is :' + str(current_perc) + ' %') storing_percentage_value_last5 = np.array( storing_percentage_value[-5:None]) print("last five change in percentage every hour: " + str(storing_percentage_value_last5) + ' %') #def forcasted_perc_change(last,df): if len(real_time_data) > 1: current_value = real_time_data[len(real_time_data) - 1] forcast_perc_change = ((fcast.tolist()[7] - current_value) / (upper_thr - lower_thr)) * 100 print('') print('The % change has been seen in forcasted value : ' + str(round(forcast_perc_change, 2)) + ' %') # Behaviour of forcasted data in real time if ((fcast.tolist()[7] > lower_thr) and (fcast.tolist()[7] < upper_thr)): print('The forcasted value is within range: ' + str(round(fcast.tolist()[7], 2))) else: print("Warning! warning! warning! .....") print('-------------------------------------------------') # Calculate after what time variable will cross the thresholds. for i in fcast: if i >= upper_thr: max = (fcast.tolist().index(i) + 1) min = (fcast.tolist().index(i)) if current_value < upper_thr: print("The actual value will cross upper threshold in " + str(min) + '-' + str(max) + ' hours') else: print( 'The actual value has already been crossed upper threshold' ) print('Please take action immediately ') break if i <= lower_thr: max = (fcast.tolist().index(i) + 1) min = (fcast.tolist().index(i)) if current_value > llower_thrr_thr: print("The actual value will cross lower threshold in " + str(min) + '-' + str(max) + ' hours') else: print( 'The actual value has already been crossed lower threshold' ) print('Please take action immediately ') break
'Figure 7.4: Level and slope components for Holt’s linear trend method and the additive damped trend method.' ) # ## Comparison # Here we plot a comparison Simple Exponential Smoothing and Holt's # Methods for various additive, exponential and damped combinations. All of # the models parameters will be optimized by statsmodels. fit1 = SimpleExpSmoothing(livestock2).fit() fcast1 = fit1.forecast(9).rename("SES") fit2 = Holt(livestock2).fit() fcast2 = fit2.forecast(9).rename("Holt's") fit3 = Holt(livestock2, exponential=True).fit() fcast3 = fit3.forecast(9).rename("Exponential") fit4 = Holt(livestock2, damped=True).fit(damping_slope=0.98) fcast4 = fit4.forecast(9).rename("Additive Damped") fit5 = Holt(livestock2, exponential=True, damped=True).fit() fcast5 = fit5.forecast(9).rename("Multiplicative Damped") ax = livestock2.plot(color="black", marker="o", figsize=(12, 8)) livestock3.plot(ax=ax, color="black", marker="o", legend=False) fcast1.plot(ax=ax, color='red', legend=True) fcast2.plot(ax=ax, color='green', legend=True) fcast3.plot(ax=ax, color='blue', legend=True) fcast4.plot(ax=ax, color='cyan', legend=True) fcast5.plot(ax=ax, color='magenta', legend=True) ax.set_ylabel('Livestock, sheep in Asia (millions)') plt.show() print( 'Figure 7.5: Forecasting livestock, sheep in Asia: comparing forecasting performance of non-seasonal methods.' )