Future covariates in Darts in Python Error

48 views Asked by At
mydata_train = X_train.copy()
mydata_train['y_train'] = y_train

mydata_test = X_test.copy()
mydata_test['y_test'] = y_test

# Convert your DataFrame to TimeSeries objects for the target variable and covariates
train_series = TimeSeries.from_dataframe(mydata_train, value_cols=['y_train'], fill_missing_dates=True, freq='h')
test_series = TimeSeries.from_dataframe(mydata_test, value_cols=['y_test'], fill_missing_dates=True, freq='h')

train_covariates = TimeSeries.from_dataframe(mydata_train, value_cols=[col for col in mydata_train.columns if col != 'y_train'], fill_missing_dates=True, freq='h')
test_covariates = TimeSeries.from_dataframe(mydata_test, value_cols=[col for col in mydata_test.columns if col != 'y_test'], fill_missing_dates=True, freq='h')

# Initialize the TFT model
model = TFTModel(
    input_chunk_length=24,
    output_chunk_length=12,
    hidden_size=16,  # Example size, adjust based on dataset size and complexity
    lstm_layers=2,  # Number of LSTM layers
    num_attention_heads=4,  # Number of attention heads
    dropout=0.1,  # Dropout rate
    batch_size=16,  # Batch size for training
    n_epochs=1,  # Number of epochs to train
    add_relative_index=False,  # Whether to add a relative index as a feature
    add_encoders=None,  # Additional encoder settings can be specified here
    likelihood=None,  # Can specify a likelihood for probabilistic forecasting
    random_state=42  # Seed for reproducibility
)

# Fit the model
model.fit(series=train_series, past_covariates=None, future_covariates=train_covariates, verbose=True)

# Predict using the model and future covariates
# n = 160 #len(mydata_test)  # Set the forecast horizon
predicted = model.predict(n=12, series=test_series, future_covariates=test_covariates)

Can anyone help me with this error? ValueError: For the given forecasting horizon n=12, the provided future covariates at dataset index 0 do not extend far enough into the future. As n <= output_chunk_length the future covariates must end at time step 2022-10-01 16:00:00, whereas now they end at time step 2022-10-01 04:00:00.

I only want to predict the next 12 days, and I provided a lot of information from the future covariates—nearly 10 months—and my data was collected hourly.

I don't know what exactly this error means, and I'm completely confused about that. Based on this error, it means that I need to have data information until this 2022-10-01 16:00:00 time. But why do I need it until this time?

I also changed input_chunk_length and output_chunk_length and tried many different numbers, but nothing happened, and I also got the same error.

1

There are 1 answers

0
melvinthedev On

The input_chunk_length and output_chunk_length parameters determine how much historical and future context the model will use for making a prediction. The input_chunk_length is the number of time steps used as input history, and the output_chunk_length is the number of time steps the model will predict into the future.

The error message you see suggests that the model is expecting the future covariates to extend at least up to the end of the output_chunk_length (12 hours) after the last training data point. In your case, the last training data point is at 2022-10-01 04:00:00, so the model is expecting future covariates to be available until 2022-10-01 16:00:00.

One way to solve this issue is to adjust the input_chunk_length and output_chunk_length parameters to match the time range of your data.

For instance, if your data is collected hourly, and you want to predict the next 12 hours, you can set:

input_chunk_length = 12 * 24  # 12 days of historical data
output_chunk_length = 12  # 12 hours of forecasting horizon

However, if you don't have enough future covariates to cover the required time range, you might need to reconsider your data collection strategy or adjust the forecasting horizon.

Keep in mind that this error is related to the context window the model expects to make predictions. The model needs enough historical and future context to make a prediction, and the error message is pointing out that the context window is not being met.