awslabs / gluonts

Probabilistic time series modeling in Python
https://ts.gluon.ai
Apache License 2.0
4.61k stars 752 forks source link

How to aggregate a forecast by hours to days ? #1036

Open Harry-N opened 4 years ago

Harry-N commented 4 years ago

Hello everyone, I am new to GluonTS.

I saw in the documentation that an object "gluonts.model.forecast" has a function (copy_aggregate(agg_fun:Callable)) which aggregates and returns a new Forecast object with a time series aggregated over the dimension axis.

I tried to implement this code :

    for target, forecast in islice(zip(tss, forecasts), num_plots):

        target=target[-past_length:].resample("1D").sum()

        ax = target.plot(figsize=(12, 5), linewidth=2)

        forecast_copy=forecast.copy_aggregate(agg_fun=sum)

        forecast_copy.plot(color='g')
        plt.show()

Did I something wrong?

In advance, thank you !

PascalIversen commented 4 years ago

Hey @Harry-N,

the copy_aggregate() method does not aggregate over the time-axis, but over the target axis for multi-dimensional targets in multi-variate forecasting. That is probably not what you are after. I assume you have a uni-variate target and want to aggregate over the time axis. I think there is no function for that in GluonTS currently, but you could do it like this:

for target, forecast in islice(zip(tss, forecasts), num_plots):

    target=target[-past_length:].resample("1D").sum()

    ax = target.plot(figsize=(12, 5), linewidth=2)

    samples = []
    for sample in forecast.samples:
        samples.append(pd.Series(sample, index=f.index).resample("1D").sum().values)
    forecast.samples = np.array(samples)

    forecast.plot(color='g')
    plt.show()