<jupyter_start><jupyter_text>(pdq) (PDQ)m stepwise search with pmdarima API<jupyter_code># pip install pmdarima      ...reinstall module if needed
import pmdarima
print(pmdarima.__version__)
# import auto_arima function (from pmdarima.arima.auto_arima) to select stepwise best SARIMAX parmaters
from pmdarima.arima import auto_arima
model1 = auto_arima(endog_train, exogenous=exog_train, trace=True, seasonal=True, m=24)
# hyperparmaters SARIMAX before memory error:  ARIMA(4,1,2)(2,0,1)[24] no intercept   : AIC=-5371.297, Time=95.35 sec
# use such model for SARIMAX below
#model1.summary()
#Out-of-Sample fitting - model1 - from .auto_arima [ARIMA(4,1,2)(2,0,1)[24] : AIC=-5371.297, Time=95.35 sec]
# fit the complete model
model = SARIMAX(endog_train, exog_train, trend=None, order=(4,1,2), seasonal_order=(2,0,1,24)) # no intercept??
fit_res = model.fit(disp=False)              # .fit(disp=False, maxiter=250) not used
print(fit_res.summary())<jupyter_output>C:\Users\GLP\anaconda3\envs\SARIMAX_py38_test_auto_arima\lib\site-packages\statsmodels\base\model.py:566: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
  warnings.warn("Maximum Likelihood optimization failed to "<jupyter_text>TRAIN SET performance model1 (forecast MW with exogenous m3/h instant flow hot water)  In-sample performance<jupyter_code># In-sample forecast (baseline approach on train set used as backtesting)
yhat = fit_res.predict()        # example of Out-of-sample forecast with exo run over the In-sample training set 
yhat = pd.DataFrame(yhat)
yhat                                                 # predict() can do In- or Out-of-sample forecast
ts1_train['Forecast_SARIMAX']=yhat                                                # appending forecast values to ts1 dataframe
ts1_train
# evaluate generalization performance of SARIMAX model above (In-sample forecast)
obs=endog_train['MW']
pred=ts1_train['Forecast_SARIMAX']

# RMSE
rmse = sqrt(mean_squared_error(obs, pred))                                                # algo for RMSE
print('Test RMSE: %.3f' % rmse)

# MAPE - from 'https://vedexcel.com/how-to-calculate-mape-in-python/'
def mape(obs, pred):
    return np.mean(np.abs((obs - pred) / (obs)))*100                                      # algo for MAPE
result = mape(obs, pred)                                                  
print('Test MAPE: %.3f' % result)                            

#SMAPE - from  'https://vedexcel.com/how-to-calculate-smape-in-python/'
def smape(obs, pred):
    return 100/len(obs) * np.sum(2 * np.abs(pred - obs) / (np.abs(obs) + np.abs(pred)))   # algo for SMAPE

result = smape(obs,pred)
print('Test SMAPE: %.3f' % result)

# Pearson Correlation
corr = np.corrcoef(obs, pred)[0,1]
print('Correlation: %.3f' % corr)

# R2 
r2_result = corr**2                                                                       # algo for R-squared
print('R-squared: %.3f' % r2_result)


# plot forecasts against actual outcomes
f = plt.figure()
f.set_figwidth(16)
f.set_figheight(9)
plt.plot(obs, label = 'MW observed')
plt.plot(pred, color='red', label = 'MW forecast')
plt.legend()
plt.show()
# end<jupyter_output><empty_output><jupyter_text>TEST SET performance model (forecast MW with exogenous m3/h instant flow hot water) backtesting with train-test split method (from '2020-11-07' to '2020-11-09') out-of-sample forecast on test set + performace¶<jupyter_code># Out-of-sample forecast        (baseline approach on test split as TRUE backtesting)
yhat_fore = fit_res.forecast(72, exog = exog_test)      # example of Out-of-sample forecast with exo on Test set
yhat_fore = pd.DataFrame(yhat_fore)
yhat_fore.index = df_base['2020-11-07':].index
yhat_fore
ts1_test['Forecast_SARIMAX']=yhat_fore         # appending forecast values to ts1 dataframe
ts1_test
# evaluate generalization performance of SARIMAX model above (Out-of-sample forecast)
obs=endog_test['MW']
pred=ts1_test['Forecast_SARIMAX']

# RMSE
rmse = sqrt(mean_squared_error(obs, pred))                                                # algo for RMSE
print('Test RMSE: %.3f' % rmse)

# MAPE - from 'https://vedexcel.com/how-to-calculate-mape-in-python/'
def mape(obs, pred):
    return np.mean(np.abs((obs - pred) / (obs)))*100                                      # algo for MAPE
result = mape(obs, pred)                                                  
print('Test MAPE: %.3f' % result)                            

#SMAPE - from  'https://vedexcel.com/how-to-calculate-smape-in-python/'
def smape(obs, pred):
    return 100/len(obs) * np.sum(2 * np.abs(pred - obs) / (np.abs(obs) + np.abs(pred)))   # algo for SMAPE

result = smape(obs,pred)
print('Test SMAPE: %.3f' % result)

# Pearson Correlation
corr = np.corrcoef(obs, pred)[0,1]
print('Correlation: %.3f' % corr)

# R2 
r2_result = corr**2                                                                       # algo for R-squared
print('R-squared: %.3f' % r2_result)


# plot forecasts against actual outcomes
f = plt.figure()
f.set_figwidth(16)
f.set_figheight(9)
plt.plot(obs, label = 'backtest: MW observed')
plt.plot(pred, color='red', label = 'backtest: MW forecast')
plt.legend()
plt.show()
# end<jupyter_output><empty_output><jupyter_text>new plots for integrating train+test set<jupyter_code># evaluate generalization performance of SARIMAX model above (In-sample forecast)
obs=endog_train['MW']
obs_bktst=endog_test['MW']
pred=ts1_train['Forecast_SARIMAX']
forcst=ts1_test['Forecast_SARIMAX']

# plot forecasts against actual outcomes
f = plt.figure()
f.set_figwidth(16)
f.set_figheight(9)
plt.plot(obs, color='violet', label = 'MW observed-train')
plt.plot(obs_bktst, color='violet', label = 'MW observed_test')
# plt.plot(pred, color='red', label = 'Train Set MW forecast - model fit')
plt.plot(forcst, color='green', label = 'Test Set MW forecast -backtest')
plt.legend()
plt.show()
# end<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12557116263327128, 'fraction_numerical': 0.024369605686241327, 'mean_word_length': 3.1914893617021276, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '661533', 'license': 'MIT', 'n_tokens_mistral': 2117, 'n_tokens_neox': 1975, 'n_words': 481, 'path': 'code/1st generation Forecast model (SARIMAX) - Statistical Learning/RENergetic_BARANZINI_Model_003a_additional_plots.ipynb', 'repo_name': 'DanieleBaranzini/RENergetic-AI-solve', 'size': '5909'}