class VARAgent(Agent): """Model-based VAR agent, trained offline on a historic dataset.""" _id = 'VAR' def __init__(self, df, max_order=15, policy='softmax'): # initialize VAR model self.model = VAR(df) # fit model self.model = self.model.fit(maxlags=max_order, ic='aic') # memory used to cache observations self.memory = pd.DataFrame(columns=df.columns) # policy self.policy = 'softmax' def observe(self, observation, action, reward, done, next_observation): self.memory.append( [observation['returns'], next_observation['returns']]) def act(self, observation): _returns = observation['returns'] if len(self.memory) == 0: # random sample _values = np.random.uniform(0, 1, self.model.coefs.shape[-1]) else: # forecast one step returns _values = self.model.forecast(self.memory.dropna().values, 1)[0] # softmax policy if self.policy == 'softmax': # to pandas.Series _action = pd.Series(_values, index=_returns.index, name=_returns.name) return softmax(_action) # LONG best stock policy elif self.policy == 'best': # one-hot vector _action = np.zeros_like(_values).ravel() _action[np.argmax(_values)] = 1.0 # to pandas.Series _action = pd.Series(_action, index=_returns.index, name=_returns.name) return _action
def VAR_forecast(df): """Initiates Forecasting using Forecast on the passed dataset Parameters ---------- df The dateset containing historical-observations on day-ahead prices Returns ------- forecasted A list of forecasted electricity prices for the next 24 hours """ column_name = "Day-ahead Price [EUR/MWh]" # Open CSV File and set timestamp column as index df.rename(columns={df.columns[0]: "cet_timestamp"}, inplace=True) df["cet_timestamp"] = pd.to_datetime(df["cet_timestamp"], format="%Y-%m-%d %H:%M") df.set_index("cet_timestamp", inplace=True) # Impute and get only one column df_diff = df.diff().dropna() # Generate new dates dates = list() last_date = df.index[-1:][0] for i in range(1, 25): last_date += timedelta(hours=1) dates.append(last_date.strftime("%Y-%m-%d %H:%M:%S")) var_model = VAR(df_diff).fit(26) var_forecast = var_model.forecast(y = var_model.y, steps=24) var_forecast_df = pd.DataFrame(var_forecast, columns=df.columns, index= dates) var_forecast_df = invert_transformation(df, var_forecast_df) ''' # For mean absolute error last_24hours = last_date - timedelta(hours=24) # History - 24hours history = df_diff[df_diff.index <= last_24hours] var_mae_model = VAR(history).fit(26) var_mae_forecast = var_mae_model.forecast(y = var_mae_model.y, steps=24) mae = mean_absolute_error(history['Day-ahead Price [EUR/MWh]'].values, np.array(var_mae_forecast)) print(mae) ''' return list(var_forecast_df[column_name].values), datetime.now().strftime("%Y-%m-%d")
train_set.shape[0] / batch_size, print_loss, ) ) lstm = lstm.eval() # ar train_arset = ipd_set2arset() armodel = VAR(train_arset) armodel = armodel.fit() px = torch.from_numpy(test_set).transpose(1, 2).float() ry = px pyar = np.zeros((px.shape[0], px.shape[1])) for i in np.arange(px.shape[0]): for t in np.arange(px.shape[1]): pyar[i, t] = ( 1 if armodel.forecast(np.array(px[i, : t + 1]), lag)[0][0] > 0 else 0 ) # lr lrmodel = LogisticRegression(random_state=0, max_iter=1000).fit( train_set_rgx, train_set_rgy ) pylrraw = lrmodel.predict(test_set_rgx) pylr = pylrraw.reshape((1651, 8)) varX = Variable(px) py = lstm(varX).squeeze().data.cpu().numpy() if fold == 0: test_set_full = test_set py_full = py pyar_full = pyar pylr_full = pylr else:
model11.summary() ######## Step 12 ######### from scipy import signal as sg f, Pxx_den = sg.periodogram(bitcoin['bprice'], 10e3) # seasonality should be seen plt.xlabel('frequency [Hz]') plt.ylabel('PSD [V**2/Hz]') plt.semilogy(f, Pxx_den) # Differencing Vairable f, Pxx_den = sg.periodogram( bitcoin['dbprice'], 10e3) # should look like skyscrapers so no seasonality # Still there is no seasonality confirm with professor plt.semilogy(f, Pxx_den) ######## Step 13 ######### from statsmodels.tsa.api import VAR bitcoin.index = bitcoin['Date'] xdata = pd.concat((bitcoin['bprice'], bitcoin['sp'], bitcoin['euro'], bitcoin['gold'], bitcoin['oil']), 1) model13 = VAR(xdata).fit(maxlags=3) model13.summary() ######## Step 14 ######### # Forecasting using VAR model model13.forecast(xdata.values, steps=30) model13.plot()