GluonTSの「to_pandas」(from gluonts.dataset.util)は多変量に対応していない!

対応させてみた。

def to_pandas_multi(instance: dict, dim: int) -> pd.Series:    
    target = instance["target"]
    start = instance["start"]
    freq = start.freqstr
    index = pd.date_range(start=start, periods=target.shape[1], freq=freq)
    return pd.Series(target[dim,:], index=index)

以下の様な使い方を想定している。

pred = predictor.predict(test_data)

plot_length = 48
prediction_intervals = (50.0, 90.0)
legend = ["observations", "median prediction"] + [f"{k}% prediction interval" for k in prediction_intervals][::-1]

for x, y in zip(test_data, pred):
    for i in range(target_num):
        plt.subplot(target_num,1,i+1)
        to_pandas_multi(x, i)[-plot_length:].dropna().plot()
        y.copy_dim(i).plot(color='g', prediction_intervals=prediction_intervals)
        plt.grid(which='both')
        plt.legend(legend, loc = 'upper left')
    
plt.show()