unit8co / darts

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

[BUG] trying using past_covariants in data with no cyclical features (financial) #2247

Closed flockonus closed 5 months ago

flockonus commented 6 months ago

Describe the bug

Hello,

Very likely not a bug but a question(?)

I'm trying to code a ML model that would be able to make some short term predictions based on financial data, which looks like this for example:

image

The target is 'avg' while other features are supposed to be past_covariates. I scaled the data, split into train / val, but when trying to .fit i get the error: ValueError: The dimensions of the series in the training set and the validation set do not match..

To Reproduce

My goal is trying as many models as possible, i've started with NBEATS. I've looked up covariates docs but couldn't find any example that comes close.

Full notebook + data here

Simplified snippet:


# load df and ffill missing data

series = TimeSeries.from_dataframe(df, freq='B')

target = train_df[['avg']]
covariates = train_df[['vol', 'price_change', 'wick_up', 'wick_down', 'mean_roll_wick_u2b', 'mean_roll_wick_d2b']]

target_series = TimeSeries.from_dataframe(target, fill_missing_dates=True, freq='B').astype(np.float32)
covariates_series = TimeSeries.from_dataframe(covariates, fill_missing_dates=True, freq='B').astype(np.float32)

# scale
target_scaled = scaler.fit_transform(target_series)
covariates_scaled = scaler.fit_transform(covariates_series)

# split
(train_scaled, val_scaled) = target_scaled.split_before(pd.Timestamp(val_boundary))

model = NBEATSModel(
    input_chunk_length=5,
    output_chunk_length=5,
    n_epochs=25,
    dropout=0.05,
)

model.fit(train_scaled, val_series=val_scaled, verbose=True, past_covariates=covariates_scaled)

# ERROR => ValueError: The dimensions of the series in the training set and the validation set do not match.

# IF .fit without past_covariates there is no error - but the quality is very low

Expected behavior

I'd like to understand how to fit those many past_covariants (5 columns) with my given univariate target.

System (please complete the following information):

Additional context Also a more meta question, looking at the examples they seem to rely on periodicity. Given the data i'm trying to fit indicated no periodicity, is it still a possible fit with darts?

dennisbader commented 6 months ago

Hi @flockonus and sorry for the late response. You have to supply val_past_covariates to model.fit() as well.

For your meta question: If your target series has some forecastable characteristics, then the models should be able to learn them.