import numpy as np
from matplotlib import pyplot as plt
from gluonts.dataset.common import ListDataset
from gluonts.model.deepar import DeepAREstimator
from gluonts.distribution.multivariate_gaussian import MultivariateGaussianOutput
from gluonts.trainer import Trainer
N = 20
T = 100
prediction_length = 10
freq = '1H'
custom_datasetx = np.random.normal(size=(N, 2, T))
custom_datasetx[:,1,:] = custom_datasetx[:,1,:]*10
train_ds = ListDataset(
[
{'target': x, 'start': '2019-01-01'}
for x in custom_datasetx[0:17, :, :]
],
freq=freq,
one_dim_target=False,
)
test_ds = ListDataset(
[
{'target': x, 'start': '2019-01-01'}
for x in custom_datasetx[17:, :, :]
],
freq=freq,
one_dim_target=False,
)
estimator = DeepAREstimator(
prediction_length=prediction_length,
freq=freq,
trainer=Trainer(epochs=5),
distr_output=MultivariateGaussianOutput(dim=2),
)
predictor = estimator.train(train_ds)
from gluonts.evaluation.backtest import make_evaluation_predictions
forecast_it, ts_it = make_evaluation_predictions(
dataset=test_ds,
predictor=predictor,
num_samples=100
)
from gluonts.evaluation import MultivariateEvaluator
evaluator = MultivariateEvaluator(quantiles=[0.1, 0.5, 0.9])
agg_metrics, item_metrics = evaluator(ts_it, forecast_it, num_series=len(test_ds))
item_metrics.plot(x='MSIS', y='MASE', kind='scatter', c=item_metrics.index, cmap='Accent')
plt.grid(which="both")
plt.show()