I am using keras LSTM to do a time series prediction. It is a regression problem, where I want to predict for example the next 5 values. The data comes from a sensor and looks like this, where x axis is the sample time and ordenates axis is the value to predict:
To provide a better explanation of this, let's imagine that the actual moment is marked by the red line, and I want to predict the next values (green arrow), so in this situation I can anticipate the large drop after the one marked in red, which was stable during a long time:
In other words, I would like to predict situations with large drops in the values, for example the ones marked in red in the following image, so I can counter-attack this large drops:
Until now, I have been able to do single-step prediction. When I do the training+prediction, the results are pretty good (blue is the actuals, orange the prection of the training set and green the prediction of the validation set):
In order to feed the LSTM, I use the following function:
# convert an array of values into a dataset matrix valid for keras LSTM format
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
The LSTM network that I use looks like this:
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
Where look_back is the window of values that the prediction is based (5 values). As my output layer (Dense) size is one, I conclude that this is a single-step prediction, so If I am using this in real time, I will predict with certainty only the next value. The thing is that I would like to predit more than one value at a time. Initially, I have use recursive forecasting like this one. With it I get the following forecasting, where blue is the window values and the orange is the forecasted values:
My questions are:
how do I go from single-step to multi-step? I guess that I need to change the size of the dense output layer? Do I need also to change the format of the input data for the LSTM?
Yes, you are right. No, it is not mandatory to change the input data as well.
Is a multi-step prediction of 5 values better than a single step-forecasting of 5 values?
Define better. If by better you mean w.r.t. accuracy, then 5 single-step forecasts are going to be, likely, more accurate than a 5-step forecast, since the single-step forecasts will take into account more recent historical values (the look_back
values in your case). In general, longer forecasting is harder than shorter forecasting, since in long-term forecasting the error tends to accumulate as you attempt to forecast the future values further into the future.
Last but not least, is an LSTM the best way to anticipate this large drops?
That depends largely on your data, and only by actually evaluating different models you can answer this. You can try:
Amazon's Chronos and Google's TimesFM are other options, but maybe too hot out of the oven to try now (APIs not yet released, etc.).