I am trying to fit an AR-EGARCH(1,1) model to electricity prices and forecast the day ahead. I am modelling every hour sepereatly, so I forecast the price for hour 0-1 only with the time series which consists of the prices in this hour from the past 1000 days. To get all 24 hours I am doing this 23 times for each hour. The forecast seems good most of the times, but sometimes it leads to large prices like -8000 (normally it's around 40), and every time it does this, the message :
"arch/univariate/base.py:750: ConvergenceWarning: The optimizer returned code 4. The message is: Inequality constraints incompatible See scipy.optimize.fmin_slsqp for code meaning"
shows up (not on a regular basis and for different hours). How can I fix this?
def forecast_ar_garch_egarch_hourly(first_forecast_start,seasonality,distribution,length):
df_ar_egarch=pd.DataFrame()
for i in range(0,24): # for each hour
#get electricity prices for hour i in one dataframe
electricity_prices_hour_i= pd.DataFrame(electricity_prices[electricity_prices['hour']==i]['Price'],index=electricity_prices[electricity_prices['hour']==i].index)
#forecast start
forecast_day=first_forecast_day+d.timedelta(hours=i)
#how much past data should be included
start_model=forecast_day-d.timedelta(days=length)
end_model=forecast_day-d.timedelta(days=1)
df_hour_i=electricity_prices_hour_i[start_model:end_model]
#use pmdarima.auto_arima to get the optimal parameter combination (but only the AR(P)-part)
diff=ndiffs(df_hour_i,test='adf')
ar_fit=pm.auto_arima(df_hour_i,max_p=6,start_q=0,max_q=0,d=diff,m=seasonality,start_Q=0,max_Q=0,max_P=16,test='adf',stepwise=True,trace=True)
p, diff, q= ar_fit.order
P, Diff, Q, m= ar_fit.seasonal_order
#get prediction from ar_egarch_pred function
prediction_egarch=ar_egarch_pred(df_stundei,M,seasonality,p,P)
df_ar_egarch=df_ar_egarch.append({'Electricity price EGARCH':prediction_egarch},ignore_index=True)
i+1
df_ar_egarch.index=pd.date_range(start=first_forecast_start,end=first_forecast_start+d.timedelta(hours=23),freq='60min')
return(df_ar_garch,df_ar_egarch)
def ar_egarch_pred(data,seasonality,distribution,p,P):
#distribution : 't','normal' or 'skew'
#seasonality= frequency of seasonality (=7) since I assume weekly seasonality
#get the AR(p) lags
lags_ar=[]
for i in range(1,p+1):
lags_ar=lags_ar+[i]
for j in range(0,P):
lags_ar=lags_ar+[(np.linspace(M,P*M,num=P)[j])]
lags_ar=[int(x) for x in lags_ar]
#fit and forecast with egarch model one step ahead and return this value
ar_egarch=arch_model(modelldaten,mean='AR',lags=lags_ar,vol='EGARCH',p=1,o=1,q=1,dist=distribution)
ar_egarch_fit=ar_egarch.fit(first_obs=modelldaten.iloc[0,0],last_obs=modelldaten.iloc[-1,0],options={'maxiter':4000},disp='off')
forecast=ar_egarch_fit.forecast().mean.iloc[-1,0]
return(forecast)