def ts_diagnose(self): """Diagnoses the fitted model""" try: assert self.model_fit is not None except AssertionError: self._prophet_logger.exception( "Model has to be fitted first! Please call ts_fit(...)") sys.exit("STOP") self.plot_residuals() if self._diagnose: if input( "Run cross validation y/n? Note, depending on parameters provided " "this can take some time...").strip().lower() == 'y': start = time() self._prophet_logger.info( "Running cross validation using parameters provided....") if self._history is not None: try: self._prophet_cv = cross_validation( self.model_fit, initial=self._history, period=self._step, horizon=self._horizon) except Exception: self._prophet_logger.exception( "Prophet cross validation error: check your " "parameters 'history', 'horizon', 'step'!") else: try: self._prophet_cv = cross_validation( self.model_fit, period=self._step, horizon=self._horizon) except Exception: self._prophet_logger.exception( "Prophet cross validation error: " "check your parameters 'horizon', 'step'!") self._prophet_logger.info("Time elapsed: {}".format(time() - start)) simu_intervals = self._prophet_cv.groupby('cutoff')['ds'].agg([ ('forecast_start', 'min'), ('forecast_till', 'max') ]) self._prophet_logger.info( "Following time windows and cutoffs have been set-up:\n") print(simu_intervals) # plot_cross_validation_metric(self._prophet_cv, metric='mape') # self._prophet_logger.info("Running performance metrics...") self._prophet_p = performance_metrics(self._prophet_cv) else: self._prophet_logger.info("OK") return
def test_prophecy(ticker): df = get_daily_data(ticker, 90) df.rename(columns={'time': 'ds', 'close': 'y'}, inplace=True) m.fit(df) df_cv = cross_validation(m, horizon='10 days') df_p = performance_metrics(df_cv) df_p.head(5) plot_cross_validation_metric(df_cv, metric='mape') plt.show()
def FB_Model(train_x, train_y,train_ratio,predict_period,changepoint_prior_scale,analysisornot,holiday): time_split = int((train_x.shape[0]) * train_ratio) df = pd.DataFrame(columns=['ds', 'y']) df['ds'] = train_x[:time_split];df['y'] = train_y[:time_split] df['y'] = np.log((np.asarray(df['y'], dtype=float))) #log能让预测效果更好 Hoday = pd.DataFrame({ 'holiday': 'weekend', 'ds': pd.to_datetime(holiday), 'lower_window': 0, 'upper_window': 1, }) #训练 model = Prophet(changepoint_prior_scale=changepoint_prior_scale,mcmc_samples=0,holidays=Hoday)#默认为0,增大后将最大后验估计取代为马尔科夫蒙特卡洛取样,但是会极大地延长训练时间 model.fit(df) future = model.make_future_dataframe(freq='D', periods=predict_period) # 建立数据预测框架,数据粒度为天,预测步长为20天 forecast = model.predict(future) model.plot(forecast).show() # 绘制预测效果图 model.plot_components(forecast).show() #分析结果 if analysisornot: df_cv = cross_validation(model, initial='1100 days', period='20 days', horizon='20 days') df_p = performance_metrics(df_cv) print(df_cv.head()) print(df_p.head()) fig = plot_cross_validation_metric(df_cv, metric='mape') fig.show()
def plot_residuals(self): print("CROSS VALIDATION RESULTS") df_cv = cross_validation(self.m, initial='365.25 days', period='365.25 days', horizon='365.25 days') # df_cv = cross_validation(self.m, initial='180 days', period='180 days', horizon='180 days') # self.out_table = df_cv print(pdtabulate(df_cv)) print("PERFORMANCE METRICS") df_p = performance_metrics(df_cv) print(pdtabulate(df_p)) # Mean absolute percentage error fig3 = plot_cross_validation_metric(df_cv, metric='mape', figsize=(11, 6)) ax3 = fig3.gca() blue_patch = mpatches.Patch(color='#5F86BC', label='Accuracy ~= ((1 - MAPE) * 100) %') plt.legend(handles=[blue_patch]) # ax3.legend("Accuracy ~= ((1 - MAPE) * 100) %") ax3.set_title( label="Prophet Forecast - Mean Absolute Percentage Error - " + self.name, fontsize=24) ax3.set_xlabel(xlabel="Forecast Horizon (days)", fontsize=16) ax3.set_ylabel(ylabel="MAPE", fontsize=16) ax3.set_ylim([0.0, 0.8]) plt.show()
def main(): args = initialize_params() pg, ds_key = import_secrets(os.path.expanduser(args.ini_path)) dir_out = os.path.expanduser(args.pdf_dir) test_area = '9.0-48453001100' bin_sizes = ['15T', '1H', '6H', '1D'] log_transforms = [False] holiday_scales = [5.0, 10.0, 20.0] opts = [bin_sizes, log_transforms, holiday_scales] for c in product(*opts): f_out = dir_out + '_'.join(list(map(str, c))) + '_' + str( args.changepoint_prior_scale) + '.pdf' if not os.path.exists(f_out): m = modeler(pg, ds_key, area=test_area, log=c[1], bin_window=c[0], cps=args.changepoint_prior_scale, hs=c[2]) with PdfPages(f_out) as pdf: fig = m.model.plot(m.fcst) pdf.savefig() fig2 = m.model.plot_components(m.fcst) pdf.savefig() fig3 = plot_cross_validation_metric(m.df_cv, metric='rmse') pdf.savefig()
def validation_main(): try: print("Try to read model parameters...") with open( 'C:\\workspace\\ecoproph\\ecoproph_experimentalPvModelBothA100.pckl', 'rb') as fin: model = pickle.load(fin) print("Validation begin...") df_cv = cross_validation(model, initial='460 days', period='30 days', horizon='20 days') fig = plot_cross_validation_metric(df_cv, metric='mape', rolling_window=0.2) plt.savefig('C:\\workspace\\ecoproph\\validation.png', bbox_inches='tight', dpi=500) plt.show() except FileNotFoundError: print("No saved .pckl model found!") print("Create and save a model before validation.")
def process_and_save_results(weather_model_2017, train_e_w, minmax_pipe): #fit model, run cross validation, return results, performance, and mean MAE weather_cv_results, weather_cv_perfomance = prophet_cv_performance( weather_model_2017, train_e_w, minmax_pipe) #plot the crossvalidated error performance plot_cross_validation_metric(weather_cv_results, metric='mae', rolling_window=0) path = './results/prophet/' weather_cv_results.to_csv(path + 'weather_cv_results.csv') weather_cv_perfomance.to_csv(path + 'weather_cv_performance.csv') print('Results saved at {}'.format(path))
def cv_mape_fig(self): ''' 'mse', 'rmse', 'mae', 'mape', 'coverage' rolling_window is the proportion of data included in the rolling window of aggregation. The default value of 0.1 means 10 aggregation for computing the metric. ''' return plot_cross_validation_metric(self.__cv_metrics.df_cv, metric='mape', rolling_window=0.1)
def plot_validation(pm, df_cv, metric): st.write(pm.head()) st.write(pm.tail()) fig = plot_cross_validation_metric(df_cv, metric=metric, rolling_window=0.1) # plt.show() st.pyplot(fig)
def cross_validate(df): prophet = Prophet() prophet.fit(df) df_cv = cross_validation( prophet, initial="30 days", period="4 days", horizon="7 days" ) df_performance = performance_metrics(df_cv) fig_performance = plot_cross_validation_metric(df_cv, metric="mape") return df_performance
def cross_validate(df): prophet = Prophet() prophet.fit(df) df_cv = cross_validation(prophet, initial='30 days', period='4 days', horizon='7 days') df_performance = performance_metrics(df_cv) fig_performance = plot_cross_validation_metric(df_cv, metric='mape') return df_performance #print(predict(df_cases_fb))
def measure_accuracy(input_db = preprocess_db): #get latest 300 points from last 24 hours of data and measure accuracy res = input_db.query('select * from Ax where time > now()- 24h order by time desc limit 300') for measurement in res: print("Measuring prediction for " + measurement) ret = res[measurement] df = pd.DataFrame.from_dict(ret) df['ds'] =df.index.astype(str).str[:-6] df['y'] = df['value'] print(df) m = Prophet(interval_width=0.95) m.fit(df) df_cv = cross_validation(m, period='1 seconds', horizon = '60 seconds') print(df_cv) df_p = performance_metrics(df_cv) df_p.head() #plot mean squared error #refer https://facebook.github.io/prophet/docs/diagnostics.html plot_cross_validation_metric(df_cv, metric='mse') print("Done")
def process_job(conn, job): assert job['type'].lower() in ['cases', 'deaths', 'tests'] print(f"{time.strftime('%H:%M:%S')} Starting job={job}") data = query_data(conn, job) df = prepare_data(job, data) m = create_model(job) m.fit(df) # predict a third into the future of what we looked back future_days = round(job['days_to_look_back'] / 3) future = m.make_future_dataframe(periods=future_days) future['cap'] = df['cap'][0] forecast = m.predict(future) # region debug if os.getenv('DOCKER_ACTIVE') is None: fig = m.plot(forecast) add_changepoints_to_plot(fig.gca(), m, forecast) fig.savefig(f"../job/prediction-{job['id']}.png") # endregion change_points = m.changepoints.dt.date.tolist() store_prediction(conn, job, forecast, change_points) # cross validate and create score if job['with_score']: # compute period to have 5-6 simulated forecasts horizon = pd.Timedelta("14 days") initial = horizon * 3 period = (df.iloc[-1]['ds'] - df.iloc[0]['ds'] - horizon - initial) / 5 df_cv = cross_validation(m, initial=initial, horizon=horizon, period=period, parallel='processes') df_p = performance_metrics(df_cv) # region debug if os.getenv('DOCKER_ACTIVE') is None: fig = plot_cross_validation_metric(df_cv, metric='mape') fig.savefig(f"../job/score-{job['id']}.png") # endregion score = df_p.iloc[-1]['mape'] store_score(conn, job, score)
def cv_cases(state): df = prepdata_cases(state) prophet = Prophet() prophet.fit(df) df_cv = cross_validation( prophet, initial="50 days", period="4 days", horizon="7 days" ) df_performance = performance_metrics(df_cv) fig_performance = plot_cross_validation_metric(df_cv, metric="mape") return plt.show()
def createDiagnosticsLayout(signal, frequency, holidayDropdown, holidayScale, seasonalityScale, changepointScale, seasonalityMode, contents, filename, paramSearch): print(signal, frequency, holidayDropdown, holidayScale, seasonalityScale, changepointScale, seasonalityMode, filename, paramSearch) if signal == 'VOID': return None if signal != 'NOTIFY': return html.Div('Encountered an error : ' + signal, style = {'margin-left' : '1rem'}) df = parseContents(contents, filename) model = generateModel(frequency, holidayDropdown, holidayScale, seasonalityScale, changepointScale, seasonalityMode, df, paramSearch) initial, period, horizon = getParams(frequency, len(df)) df_cv =cross_validation(model, initial = initial, period = period, horizon = horizon) #df_p = performance_metrics(df_cv, rolling_window = 0) #print(df_p.head()) fig = mpl_to_plotly(plot_cross_validation_metric(df_cv, metric = 'mae', rolling_window = 0)) return html.Div(children = [html.H6('Mean Absolute Error', style = {'margin-left': '1rem'}), dcc.Graph(figure = fig)])
def cv_cases(state): df = prepdata_cases(state) prophet = Prophet() prophet.fit(df) df_cv = cross_validation(prophet, initial='50 days', period='4 days', horizon='7 days') df_performance = performance_metrics(df_cv) fig_performance = plot_cross_validation_metric(df_cv, metric='mape') return plt.show()
def sav_figure(name, c_prophet, c_forecast, ca_cv=None): fig = c_prophet.plot(c_forecast, xlabel='Date', ylabel='CPU Secs') a = add_changepoints_to_plot(fig.gca(), c_prophet, c_forecast) plt.title('CPU Seconds (hourly)') plt.savefig(IMG_DIR + name + '_forecast.png') #plt.show() figC = c_prophet.plot_components(c_forecast) plt.savefig(IMG_DIR + name + '_forecastComp.png') #plt.show() if ca_cv: figV = plot_cross_validation_metric(ca_cv, metric='mape') plt.title( 'CPU Seconds Forecast MAPE (mean absolute percent error)') plt.savefig(IMG_DIR + name + '_forecastCV.png')
def diagnostics(m): #prediction performance on a 365 days horizon, starting with 730 days of training data, #and then making predictions every 180 days. #(3x days, x/2, x days) df_cv = cross_validation(m, initial='4380 days', period='365 days', horizon='1460 days') #15-1-4 df_cv.head() #used to compute some useful statistics of the prediction performance df_p = performance_metrics(df_cv) df_p.head() #ploting a graph fig = plot_cross_validation_metric(df_cv, metric='mape') fig.savefig('img/cross-validation metric.png')
def runValidation(self, model): #df_cv = cross_validation(model, initial='60 days', period='1 days', horizon = '14 days') df_cv = cross_validation(model, initial='42 days', period='3 days', horizon='14 days') fig = plot_cross_validation_metric(df_cv, metric='mape') plt.ylabel('Erro Médio Absoluto Percentual (MAPE)', fontsize=18) plt.xlabel('Horizonte (Dias)', fontsize=18) plt.xticks(fontsize=15) plt.yticks(fontsize=15) plt.legend(['Erro Percentual Absoluto', 'MAPE']) figure = plt.gcf() plt.savefig("/home/elton/Pictures/Corona Figuras/Figura 4.svg", dpi=300) plt.show()
def validation(model, data, city, y_predict, number_of_day_initial, period, horizon): ''' period set how frequenly will the prediction be carried out horizon define long each forcast will be forecast will be happening at cutoff+horizon ''' df_cv = cross_validation(model, initial=number_of_day_initial, period=period, horizon=horizon) df_p = performance_metrics(df_cv) fig4 = plot_cross_validation_metric(df_cv, metric='rmse') fig4.canvas.set_window_title("cross validation") #calculating the mean absolute percentage error(MAPE) mape = mean_absolute_percentage_error(df_cv.y, df_cv.yhat) print(f"The mape of train data is {mape}%")
def ts_prophet(series, periods, horizon, cap=None, floor=None, **kwargs): df = pd.DataFrame() df["y"] = series df["ds"] = series.index if cap is not None: df["cap"] = cap if floor is not None: df["floor"] = floor plt.figure(figsize=(16, 6)) plt.title(series.name) sns.lineplot(df.ds, df.y) plt.show() m = Prophet(**kwargs) Prophet() m.fit(df) future = m.make_future_dataframe(periods) if cap is not None: future["cap"] = cap if floor is not None: future["floor"] = floor forecast = m.predict(future) fig1 = m.plot(forecast) plt.show() fig2 = m.plot_components(forecast) plt.show() df_cv = cross_validation(m, horizon) df_p = performance_metrics(df_cv, rolling_window=1) fig3 = plot_cross_validation_metric(df_cv, metric="rmse") plt.show() return forecast, df_cv, df_p
d_df = daily_df.reset_index().dropna() sns.set() plt.plot(d_df['Date'], d_df['AveragePrice']) plt.title('Avocado Prices') plt.xlabel('Date') plt.ylabel('Average Price') # plt.show() d_df.columns = ['ds', 'y'] m = Prophet() m.fit(d_df) future = m.make_future_dataframe(periods=90) # forecast for 90 days forecast = m.predict(future) f = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail() fig1 = m.plot(forecast) fig1.savefig('fig1.png') fig2 = m.plot_components(forecast) fig2.savefig('fig2.png') df_cv = cross_validation(m, horizon='90 days') df_p = performance_metrics(df_cv) # print(df_p.head(5)) fig3 = plot_cross_validation_metric(df_cv, metric='mape') fig3.savefig('fig3.png')
cv_results = cross_validation(prophet, initial='2000 hours', period='1440 minutes', horizon='1 day') # Calculando MAPE mape_baseline = dtexp.mean_absolute_percentage_error(cv_results.y,cv_results.yhat) mape_baseline #%% from fbprophet.diagnostics import performance_metrics performance_results = performance_metrics(cv_results) performance_results.head() #%% from fbprophet.plot import plot_cross_validation_metric fig = plot_cross_validation_metric(cv_results, metric='mape') #%% print(df_274_time_sale.iloc[1]) df_274_time_sale.reset_index(drop=True) feriados = dtclean.get_Holiday() df_teste = df_274_time_sale.iloc[0:28] #%% df_temp = forecast_feriados.copy() df_temp = df_temp.iloc[0:28] df_teste['yhat'] = df_temp['yhat'] df_teste.head()
def model_predict(img_path, model): file = file_path + '/' + "HISTWIDE_LOWES.csv" out_file_path = os.path.join(basepath, 'output') data = pd.read_csv(file) data.columns = data.columns.astype(str).str.replace('PERIOD', '') data['STARTDATE'] = pd.to_datetime(data['STARTDATE']) data.drop(['DMDGROUP', 'EVENT', 'HISTSTREAM', 'DMDCAL', 'TYPE'], axis=1, inplace=True) tData = data.melt(id_vars=['DMDUNIT', 'LOC', 'STARTDATE'], var_name='WEEK', value_name='SALES') tData['WEEK'] = tData['WEEK'].astype(int) temp = tData['WEEK'].apply(lambda x: pd.Timedelta(x, unit='W')) tData['Date'] = tData['STARTDATE'] + (temp) tData['SALES'] = tData['SALES'].str.replace(',', '') tData.dropna(subset=["SALES"], inplace=True) tData['SALES'] = tData['SALES'].astype(float) uniqueStore = tData.LOC.unique() GData = tData.groupby('LOC') tcData = tData.copy() output = [] tData = tcData.copy() for i in range(0, len(uniqueStore)): uStore = tData.loc[tData['LOC'] == uniqueStore[i]] uStore['Normalized'] = (uStore['SALES'] - uStore['SALES'].min()) / ( uStore['SALES'].max() - uStore['SALES'].min()) output.append(uStore) output2 = pd.DataFrame() for i in range(0, len(output)): output2 = output2.append(pd.DataFrame(output[i])) output2.to_csv(out_file_path + '/' + r'SPI_final_data.csv') output3 = output2 output4 = output2 output5 = output2 output6 = output2 output7 = output2 output3 = output3.loc[output3['LOC'] == 'G1010_L0'] output4 = output4.loc[output4['LOC'] == 'G1010_L1'] output5 = output5.loc[output5['LOC'] == 'G1249_L0'] output6 = output6.loc[output6['LOC'] == 'G1249_L1'] for i in range(0, 104): output2["D" + str(i)] = np.where(output2['WEEK'] == (i + 1), 1, 0) output2 = output2.loc[output2['WEEK'] == 1] output2.dropna() output2.to_csv(out_file_path + '/' + r'SPI_final_dummy_data.csv') data1 = output2.iloc[:, 6].values data2 = pd.DataFrame(data1) data2.dropna() data2[0] = data2[0].fillna(0) pred = KMeans(n_clusters=10).fit_predict(data2) Y = pred data2['Profiles'] = Y output2['Profiles'] = Y output2.to_csv(out_file_path + '/' + r'SPI_final_dummy_data_groups.csv') Groups = output2.groupby('Profiles')['LOC'].count() output3 = output3.loc[output3['LOC'] == 'G1010_L0'] output4 = output4.loc[output4['LOC'] == 'G1010_L1'] output5 = output5.loc[output5['LOC'] == 'G1249_L0'] output6 = output6.loc[output6['LOC'] == 'G1249_L1'] output7 = output7.loc[output7['LOC'] == 'G1683_L1'] plt.figure(figsize=(20, 8)) plt.plot(output3['Date'], output3['SALES'], 'b-', label='G1010_L0') plt.plot(output4['Date'], output4['SALES'], 'r-', label='G1010_L1') plt.plot(output5['Date'], output5['SALES'], 'g-', label='G1249_L0') plt.plot(output6['Date'], output6['SALES'], 'y-', label='G1249_L1') plt.xlabel('Date') plt.ylabel('Sales') plt.title('Sales of G1010 Vs G1249') plt.legend() plt.savefig(out_file_path + '/' + 'Sales of G1010 Vs G1249.png') plt.figure(figsize=(20, 8)) plt.plot(output7['Date'], output7['SALES'], 'o-', label='G1683_L2') plt.xlabel('Date') plt.ylabel('Sales') plt.title('G1683_L2') plt.legend() plt.savefig(out_file_path + '/' + 'Sales of G1683_L2.png') output7 = output7.rename(columns={'Date': 'ds', 'SALES': 'y'}) output7['ds'] = pd.to_datetime(output7['ds']) G1683_L2_model = Prophet() G1683_L2_model.fit(output7) G1683_L2_forecast = G1683_L2_model.make_future_dataframe(periods=52, freq='W') G1683_L2_forecast = G1683_L2_model.predict(G1683_L2_forecast) plt.figure(figsize=(20, 8)) G1683_L2_model.plot(G1683_L2_forecast, xlabel='Date', ylabel='Sales') plt.title('G1683 Sales') plt.savefig(out_file_path + '/' + 'G1683 Sales') plt.figure(figsize=(20, 8)) output7['y'].plot() plt.savefig(out_file_path + '/' + 'output7.png') future_dates = G1683_L2_model.make_future_dataframe(periods=52, freq='W') prediction = G1683_L2_model.predict(future_dates) prediction.to_csv(out_file_path + '/' + r'prediction.csv') #### plot the predicted projection G1683_L2_model.plot(prediction) plt.savefig(out_file_path + '/' + 'prediction.png') ##### Visualize Each Components[Trends,Weekly] G1683_L2_model.plot_components(prediction) plt.savefig(out_file_path + '/' + 'Trends Weekly.png') from fbprophet.diagnostics import cross_validation output7_cv = cross_validation(G1683_L2_model, horizon="365 days", period='180 days', initial='60 days') from fbprophet.diagnostics import performance_metrics df_performance = performance_metrics(output7_cv) df_performance.head() from fbprophet.plot import plot_cross_validation_metric fig = plot_cross_validation_metric(output7_cv, metric='mdape') plt.savefig(out_file_path + '/' + 'cross validation metrics.png') return Groups
model.plot(prediction) #Visualize Each Components[Trends,yearly] model.plot_components(prediction) from fbprophet.diagnostics import cross_validation df_cv=cross_validation(model,initial='730 days', period='180 days', horizon='365 days') df_cv.head() #Evaluating MSE,RMSE,MAE etc from fbprophet.diagnostics import performance_metrics df_p=performance_metrics(df_cv) df_p.head() #Plotting RMSE from fbprophet.plot import plot_cross_validation_metric fig=plot_cross_validation_metric(df_cv, metric='rmse')
def prediction(data): pd.plotting.register_matplotlib_converters() fb_forecasting = Prophet(n_changepoints=4) data_frame = copy.deepcopy(data) # Prophet follows sklearn API # The input to Prophet is always a date frame with two columns 'ds' and 'y' # reference: https://facebook.github.io/prophet/ data_frame.columns = ['ds', 'y'] data_frame['y'].replace(' ', '', inplace=True) data_frame.dropna(subset=['y'], inplace=True) fb_forecasting.fit(data_frame) future = fb_forecasting.make_future_dataframe(periods=250) forecast = fb_forecasting.predict(future) fb_forecasting.plot_components(forecast) plt.title("Trend analysis of crude oil prices") plt.savefig('Results/trend.png', bbox_inches='tight') plt.show() print("------------FORECAST----------------") forecast.to_csv('forecast.csv', index=False) print("------------FORECAST----------------") fig = fb_forecasting.plot(forecast, xlabel="Year", ylabel="Crude Oil Price") a = add_changepoints_to_plot(fig.gca(), fb_forecasting, forecast) plt.title("Prophet Forecasting Model") plt.savefig('Results/forecast.png', bbox_inches='tight') plt.show() # Prophet Oil Forecasts forecast2019 = forecast[(forecast['ds'] >= '2019-01-01') & (forecast['ds'] <= '2020-01-21')] figure, ax = plt.subplots() ax.plot(forecast2019['ds'], forecast2019['yhat'], label='Predicted Crude Oil Prices', color="red") prophet_data = data[data['Date'] >= '2019-01-01'] ax.plot(prophet_data['Date'], prophet_data['OilPrice'], label='Original Crude Oil Prices', color="orange") plt.ylim([0, 90]) legend = ax.legend(loc='upper right', shadow=True) plt.title('Crude Oil Prices Forecasting using Prophet') plt.xlabel('Months') plt.ylabel('Crude Oil Prices') plt.savefig('Results/ForecastUsingProphet.png', bbox_inches='tight') plt.show() # Mean Absolute Error mean_abs_error = metrics.mean_absolute_error(prophet_data['OilPrice'], forecast2019['yhat']) # Mean Squared Error mean_square_error = metrics.mean_squared_error(prophet_data['OilPrice'], forecast2019['yhat']) # Root Mean Squared Error root_mean_square_error = math.sqrt(mean_square_error) print("Mean Absolute Error = ", mean_abs_error) print("Mean Squared Error = ", mean_square_error) print("Root Mean Squared Error = ", root_mean_square_error) # Prophet Diagnostics cross_validation_forecast = cross_validation(fb_forecasting, initial='1259 days', period='180 days', horizon='365 days') cutoff = cross_validation_forecast['cutoff'].unique()[0] cross_validation_forecast = cross_validation_forecast[cross_validation_forecast['cutoff'].values == cutoff] performance = performance_metrics(cross_validation_forecast) print(performance.head()) plot_cross_validation_metric(cross_validation_forecast, metric='mape') plt.title("Cross Validation Performance Metrics") plt.savefig('Results/ProphetDiagnostics.png', bbox_inches='tight') plt.show()
def print_rmse_plot(df_cv): fig = plot_cross_validation_metric(df_cv, metric='rmse', rolling_window=.1)
def main(timey, fcst_type): location = "886-limited" timestring = time.strftime("%Y-%m-%d") sql = r"C:\Users\uxkp\sql_queries\inventory\avg_bal_daily.sql" df = daily_time_series(data_prep(read_sql_to_df(sql))) print(df.columns) df.rename(columns={"DATETIME": "ds", "INVENTORY": "y"}, inplace=True) print(df.dtypes) print(df.tail) df.reset_index(drop=True) # m = Prophet() df["y"] = np.log(df["y"].replace(0, np.nan)) df.to_csv(r"c:\users\uxkp\desktop\filetest.csv", index=False) df["cap"] = 24.5 df["floor"] = 0 m = Prophet( mcmc_samples=100, growth="logistic", uncertainty_samples=100, interval_width=0.9, changepoint_range=0.90, changepoint_prior_scale=0.01, weekly_seasonality=False, ) m.fit(df) future = m.make_future_dataframe(periods=60, freq="d") future["cap"] = 24.5 future["floor"] = 0 forecast = m.predict(future) psf = m.predictive_samples(future) # m.plot(psf).show() forecast.to_csv( f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_forecast_{location}_{timestring}.csv", index=False) dcv = cross_validation(m, initial="30 days", period="7 days", horizon="60 days") performance_metrics_results = performance_metrics(dcv) performance_metrics_results.to_csv( f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_performance_metrics_{location}_{timestring}.csv", index=False) fig1 = plot_cross_validation_metric(dcv, metric="mape") plt.savefig( f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_cross validation_{location}_{timestring}.png", format="png") fig2 = m.plot(forecast, xlabel=f"Time Series Forecast") plt.savefig( f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_forecast_{location}_{timestring}.png", format="png") fig3 = m.plot_components(forecast) a = add_changepoints_to_plot(fig3.gca(), m, forecast) plt.savefig( f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_components_{location}_{timestring}.png", format="png") fig1.show() fig2.show() fig3.show()
# In[13]: def plotdaily(dataframe): last6=dataframe fig=plt.figure(figsize=(70,40)) plt.plot(last6['ds'],last6['yhat'],color='red',linewidth=7) plt.plot(last6['ds'],last6['y'],color='blue',linewidth=7) plotdaily(last6) # In[14]: from fbprophet.plot import plot_cross_validation_metric fig = plot_cross_validation_metric(last6, metric='mape') # In[15]: 19/06/2018#weekly def weekly(last6): i=0 k=0 mape=pd.DataFrame(columns=['yhat','y']) while (i<len(last6)): s=last6.iloc[i:i+7] yhat=np.sum(s['yhat']) y=np.sum(s['y']) last6.loc[i:i+7,'weeklyyhat']=yhat
# generate and save figs here cutoff_date = pd.to_datetime('2018-05-15') pred_copy = m pred_copy.model.history = pred_copy.model.history[ pred_copy.model.history['ds'] >= cutoff_date] fig = fb_plot(pred_copy.model, m.fcst[m.fcst['ds'] >= cutoff_date], xlabel='Time', ylabel='N Scooters', figsize=(15, 6)) # fig.show() fig.savefig('/home/bapfeld/scoothome/figures/prophet_example.png') fig2 = m.model.plot_components(m.fcst) fig2.savefig('/home/bapfeld/scoothome/figures/prophet_example_components.png') fig3 = plot_cross_validation_metric(m.df_cv, metric='rmse') fig3.savefig('/home/bapfeld/scoothome/figures/prophet_example_rmse.png') # do the same for the in use m2 = tsModel(pg, ds_key, '15T', include_weather=False) m2.get_area_series(area, series='scooter') m2.transform_area_series(select_var='n') m2.prep_model_data() m2.build_model(scale=100, hourly=True, holidays_scale=50) m2.train_model() n_periods = m2.calculate_periods() m2.build_prediction_df(periods=n_periods) m2.future.dropna(inplace=True) m2.predict() m2.cv(initial='365 days', period='30 days', horizon='30 days', log=False) in_use_preds = m2.fcst