--- title: Naive2 model keywords: fastai sidebar: home_sidebar summary: "Common benchmark model." description: "Common benchmark model." nb_path: "nbs/models_naive2__naive2.ipynb" ---
API details.
The Naive2 benchmark model, as explained in the M4 guide is determined by the following process:- Test the time-series for seasonal patterns by performing a 90% auto-correlation.
import pandas as pd
from nixtlats.data.datasets.epf import EPF, EPFInfo
from nixtlats.data.tsloader import TimeSeriesLoader
import pylab as plt
from pylab import rcParams
plt.style.use('seaborn-whitegrid')
plt.rcParams['font.family'] = 'serif'
FONTSIZE = 17
# Load and plot data
Y_df, X_df, S_df = EPF.load_groups(directory='./data', groups=['NP'])
fig = plt.figure(figsize=(15, 6))
plt.plot(Y_df.ds, Y_df.y.values, color='#628793', linewidth=0.4)
plt.ylabel('Price [EUR/MWh]', fontsize=15)
plt.xlabel('Date', fontsize=15)
# plt.savefig('./results/NP.png', bbox_inches = 'tight')
plt.show()
from itertools import chain
from multiprocessing import Pool
from numpy.lib.stride_tricks import sliding_window_view
from nixtlats.losses.numpy import mae
def fit_predict_naive2(Y, output_size=24, seasonality=24):
y_hat_list = []
for idx in range(len(Y)):
y_id = Y[idx, :]
y_hat_id = Naive2(seasonality).fit(y_id).predict(output_size)
y_hat_list += [y_hat_id]
return y_hat_list
def parallelized_naive2(Y, n_cores=32):
# Y
n_partitions = n_cores**3//4 # number of partitions to split the array
Y_split = np.array_split(Y, n_partitions)
pool = Pool(n_cores)
Y_hat_list = list(chain.from_iterable(pool.map(fit_predict_naive2, Y_split)))
pool.close()
pool.join()
Y_hat = np.vstack(Y_hat_list)
return Y_hat
Y_windows = sliding_window_view(Y_df.y.values, window_shape=24*7)
day_idxs = (np.arange(len(Y_windows)) % 24) == 0
Y_windows = Y_windows[day_idxs, :]
print("Y_windows.shape", Y_windows.shape)
Y_hat = parallelized_naive2(Y=Y_windows)
Y_hat = Y_hat.reshape(-1)
Y_hat.shape
fig = plt.figure(figsize=(15, 6))
plt.plot(Y_df.ds[168:], Y_df.y.values[168:], color='#628793', linewidth=0.4, label='true')
plt.plot(Y_df.ds[168:], Y_hat[:len(Y_hat)-24], color='peru', linewidth=0.4, label='forecast')
plt.ylabel('Price [EUR/MWh]', fontsize=15)
plt.xlabel('Date', fontsize=15)
plt.legend()
# plt.savefig('./results/NP.png', bbox_inches = 'tight')
plt.show()
naive2_mae = mae(y=Y_df.y.values[168:], y_hat=Y_hat[:len(Y_hat)-24])
print(f'MAE: {naive2_mae}')