unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
8.04k stars 874 forks source link

[QUESTION] Future covariates series does not extend far enough into the future #2283

Closed mnoorchenar closed 6 months ago

mnoorchenar commented 7 months ago

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.

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)
madtoinou commented 7 months ago

Hi @mnoorchenar,

Deep learning model supporting future covariates such as TFTModel need the future covariates series to extend output_chunk_length values after the end of the target series that is being forecasted. It is mentioned in the docstring of the argument: "Also, the number of future values from future covariates to use as a model input (if the model supports future covariates). It is not the same as forecast horizon n used in predict(), which is the desired number of prediction points generated using either a one-shot- or autoregressive forecast. Setting n <= output_chunk_length prevents auto-regression. This is useful when the covariates don’t extend far enough into the future, or to prohibit the model from using future values of past and / or future covariates for prediction (depending on the model’s covariate support)".

From the error message, it seems like the two series have the same end, meaning that your future covariates might actually be a past covariate (see this page for more information about the covariates).