Nixtla / neuralforecast

Scalable and user friendly neural :brain: forecasting algorithms.
https://nixtlaverse.nixtla.io/neuralforecast
Apache License 2.0
2.69k stars 312 forks source link

Error in using Poisson DistributionLoss for LSTM forecasting #1012

Closed RajamannarAanjaram closed 1 week ago

RajamannarAanjaram commented 1 month ago

I'm trying to build a mutivariate time series forecasting model using LSTM,

I'm using this model to forecast for 52 weeks from the current week, since I got new data till last week for this year, I'm retrained the model again from 2020 till last week when I do that I'm getting the below error. Previously I wasn't getting this error.

I have 10 features which I have passed as an historical exogenous variable, while forecasting for 52 weeks, I'm including the code I have come-up with

prediction_df = {}
for cluster_id in correlation_df.Cluster.unique():
    print(f"Modelling for Cluster :{cluster_id}")
    models = [
        LSTM(
            h=h,
            hist_exog_list=cluster_regressor_cols[cluster_id],
            loss=DistributionLoss(distribution='Poisson', level=[80, 90]),
            # loss=DistributionLoss(distribution='StudentT', level=[80, 90]),
            learning_rate=0.001,
            max_steps=100,
            scaler_type='robust',
        ),
    ]
    model_lstm = NeuralForecast(models=models, freq='W-Sat')
    model_lstm.fit(cluster_df_dict[cluster_id])

    p =  model_lstm.predict(futr_df=cluster_test_df_dict[cluster_id]).reset_index() # prediction based on the future dataframe
    p = p.merge(test_df[['ds','unique_id', 'y']], on=['ds', 'unique_id'], how='left') # merging with test data for future predictions

    prediction_df[cluster_id] = p
    print(f"Model MAPE score : {mape(p['y'],p['LSTM'])}")

I'm getting the following error

image

jmoralez commented 1 month ago

Have you always used scaler_type='robust'? That can produce negative values which the Poisson distribution can't handle.

RajamannarAanjaram commented 1 month ago

Have you always used scaler_type='robust'? That can produce negative values which the Poisson distribution can't handle.

yes @jmoralez for my previous runs I was using robust

jmoralez commented 1 month ago

With poisson loss? I don't think those two work together.

RajamannarAanjaram commented 1 month ago

Hi @jmoralez, I beg to differ, attaching a snapshot of the model which I'm currently running (triggered the model training just now) with the type = 'robust' for the past dataset

image

jmoralez commented 1 month ago

I think it's because of the level, but you're kind of playing with fire because if at some point the prediction for the 80th percentile is negative it'll fail. Also consider that the Poisson distribution is meant for counts (i.e. non-negative integers) and by using the scaler it produces (possibly negative) float values.

RajamannarAanjaram commented 1 month ago

so for the loss function should I only stick with the MAE and MAPE or if I want to use DistributionLoss what should I restrict to. I'm trying to forecast a scaler value

Also, if you don't mind can you explain a bit more about what you meant by level

elephaint commented 1 month ago

so for the loss function should I only stick with the MAE and MAPE or if I want to use DistributionLoss what should I restrict to. I'm trying to forecast a scaler value

Also, if you don't mind can you explain a bit more about what you meant by level

1) You can use a DistributionLoss but if your target values are real numbers that can be negative, stick with a distribution that supports negative values. 2) The level parameter in DistributionLoss enables you to return the prediction intervals for the expected (e.g.) 80% or 90% of the observed values. See also this tutorial

github-actions[bot] commented 1 week ago

This issue has been automatically closed because it has been awaiting a response for too long. When you have time to to work with the maintainers to resolve this issue, please post a new comment and it will be re-opened. If the issue has been locked for editing by the time you return to it, please open a new issue and reference this one.