Gluon-ts: Best way to add holiday, weekend, weekend features

Created on 21 Nov 2019  路  8Comments  路  Source: awslabs/gluon-ts

What's the best way to add holiday, weekend, weekend features to DeepAR?

I've seen one example that used feat_dynamic_real, but DeepAR has a parameter for time_features. Is time_features the better way? I have not seen examples of using the time_features parameter anywhere. If time_features is the best option, how would I use it?

Thanks!

bug good first issue question

Most helpful comment

This said, I agree we should add an example on this :-)

All 8 comments

@RXY3832 the time_features option in the estimator allows you to customize which features (such as the ones you mention) the model should use. The default settings will enable the features which are relevant for the specific frequency that you set: these are selected according to the logic here https://github.com/awslabs/gluon-ts/blob/2cb59f42fc933e931aa61d6519100eda07731441/src/gluonts/time_feature/_base.py#L126

So, in principle you don鈥檛 need to explicitly set them. However, if you really want to customize them you can explicitly provide them in a list:

time_features=[HourOfDay(), DayOfWeek()]

will make the model see the hour of day and day of week features. You can check the module I linked above to see how to import the necessary stuff.

This said, I agree we should add an example on this :-)

We also have a few holidays already implemented here: https://github.com/awslabs/gluon-ts/blob/master/src/gluonts/time_feature/holiday.py

Thanks for the information!

I tried adding Holidays, like in the link provided, and while Christmas and Christmas Eve worked, other date are giving me AssertionError: No closest holiday for the date index error. Below is the code and more detailed error.

Code:

from gluonts.time_feature.holiday import (
        squared_exponential_kernel,
        SpecialDateFeatureSet,
        NEW_YEARS_DAY,
        MARTIN_LUTHER_KING_DAY,
        SUPERBOWL,
        THANKSGIVING,
        CHRISTMAS_EVE,
        CHRISTMAS_DAY,
        NEW_YEARS_EVE
    )

h_days = [NEW_YEARS_DAY,
          MARTIN_LUTHER_KING_DAY,
          SUPERBOWL,
          THANKSGIVING,
          CHRISTMAS_EVE,
          CHRISTMAS_DAY,
          NEW_YEARS_EVE]

#import pandas as pd
sfs = SpecialDateFeatureSet(h_days)
date_indices = pd.date_range(
    start="2016-01-01",
    end="2019-11-30",
    freq='D'
    )

sfs(date_indices)

Error Details:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-50-58f607c96e79> in <module>()
     27     )
     28 
---> 29 sfs(date_indices)

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/gluonts/time_feature/holiday.py in __call__(self, dates)
    211                     ]
    212                 )
--> 213                 for feat_name in self.feature_names
    214             ]
    215         )

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/gluonts/time_feature/holiday.py in <listcomp>(.0)
    211                     ]
    212                 )
--> 213                 for feat_name in self.feature_names
    214             ]
    215         )

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/gluonts/time_feature/holiday.py in <listcomp>(.0)
    208                             SPECIAL_DATE_FEATURES[feat_name](index)
    209                         )
--> 210                         for index in dates
    211                     ]
    212                 )

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/gluonts/time_feature/holiday.py in distance_to_day(index)
     43         assert (
     44             len(holiday_date) != 0
---> 45         ), f"No closest holiday for the date index {index} found."
     46         # It sometimes returns two dates if it is exactly half a year after the
     47         # holiday. In this case, the smaller distance (182 days) is returned.

AssertionError: No closest holiday for the date index 2018-07-18 00:00:00 found.

Let me know if I should move this over to another thread too.

Thanks!

Sorry for coming back to this so late. I'll look into this.

i have the same error -
code:

from gluonts.time_feature.holiday import (
        squared_exponential_kernel,
        SpecialDateFeatureSet,
        NEW_YEARS_DAY,
        MARTIN_LUTHER_KING_DAY,
        SUPERBOWL,
        THANKSGIVING,
        CHRISTMAS_EVE,
        CHRISTMAS_DAY,
        NEW_YEARS_EVE
    )

hdays = [NEW_YEARS_DAY,
          MARTIN_LUTHER_KING_DAY,
          SUPERBOWL,
          THANKSGIVING,
          CHRISTMAS_EVE,
          CHRISTMAS_DAY,
          NEW_YEARS_EVE]
kernel = squared_exponential_kernel(alpha=1.0)

sfs = SpecialDateFeatureSet(hdays, kernel)
date_indices = pd.date_range(
     start="2018-01-01",
     end="2019-12-31",
     freq='D')

sfs(date_indices)

error:

```

AssertionError Traceback (most recent call last)
in
20 kernel = squared_exponential_kernel(alpha=1.0)
21 sfs = SpecialDateFeatureSet(hdays, kernel)
---> 22 sfs(dfwide.index)

~/anaconda3/envs/btq/lib/python3.7/site-packages/gluonts/time_feature/holiday.py in __call__(self, dates)
211 ]
212 )
--> 213 for feat_name in self.feature_names
214 ]
215 )

~/anaconda3/envs/btq/lib/python3.7/site-packages/gluonts/time_feature/holiday.py in (.0)
211 ]
212 )
--> 213 for feat_name in self.feature_names
214 ]
215 )

~/anaconda3/envs/btq/lib/python3.7/site-packages/gluonts/time_feature/holiday.py in (.0)
208 SPECIAL_DATE_FEATURESfeat_name
209 )
--> 210 for index in dates
211 ]
212 )

~/anaconda3/envs/btq/lib/python3.7/site-packages/gluonts/time_feature/holiday.py in distance_to_day(index)
43 assert (
44 len(holiday_date) != 0
---> 45 ), f"No closest holiday for the date index {index} found."
46 # It sometimes returns two dates if it is exactly half a year after the
47 # holiday. In this case, the smaller distance (182 days) is returned.

AssertionError: No closest holiday for the date index 2018-07-18 00:00:00 found.```

Has been fixed by @kashif in #542. Thank you @kashif!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MariPlaza picture MariPlaza  路  5Comments

robertosannazzaro picture robertosannazzaro  路  5Comments

mharvan picture mharvan  路  9Comments

alexcombessie picture alexcombessie  路  6Comments

alexhallam picture alexhallam  路  5Comments