Fix distance_to_holiday to return the distance to the nearest occurrence - #3335
Open
Hrafz wants to merge 1 commit into
Open
Fix distance_to_holiday to return the distance to the nearest occurrence#3335Hrafz wants to merge 1 commit into
distance_to_holiday to return the distance to the nearest occurrence#3335Hrafz wants to merge 1 commit into
Conversation
holiday.dates() is queried over a +/-200 day window, which spans more than a year and therefore can contain two occurrences of a yearly holiday. Taking holiday_date[0] picks the earliest occurrence in the window, not the nearest one, so for the 17-18 days a year that sit 183-200 days after a holiday the feature reports a large positive distance instead of the small negative distance to the next occurrence. Return the occurrence with the smallest absolute distance instead, and add regression tests pinning both the specific dates and the invariant that no reported distance exceeds half a year.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of changes:
gluonts.time_feature.holiday.distance_to_holidayqueries the holidaycalendar over a
±MAX_WINDOW = ±200day window and then takes the firstelement of the result:
holiday.dates()returns an ascendingDatetimeIndex, soholiday_date[0]is the earliest occurrence in the window, not the nearest one. The window
is 401 days wide, so it can hold two occurrences of a yearly holiday: for the
fixed-date holidays once
indexis 165-200 days past one of them, and forthe Easter-based ones, whose consecutive occurrences are 350-385 days
apart, from about 157 days past. The earlier occurrence is then returned,
which is the wrong one as soon as
indexis past the midpoint between thetwo. The inline comment above the return says the smaller distance is
returned; it isn't.
Repro (
pandas 2.2.3,numpy 1.26.4,srconPYTHONPATH):Sweeping all 18 features in
SPECIAL_DATE_FEATURESover every day from2015-01-01 to 2022-12-31 (52596 feature/day pairs), the reported distance
disagrees with the distance to the nearest occurrence on 2525 of them, 138
to 142 days per feature over the eight years. The maximum reported magnitude
is 200 days for every one of the 18 features, while the distance to the
nearest occurrence never exceeds 192: consecutive Easter dates can be up to
385 days apart (Easter moves by up to 20 days from one year to the next), and
half of the largest gap is the worst case.
In the feature itself this is a discontinuity: for
new_years_day,2019-07-20 reports
+200and 2019-07-21 reports-164. The kernels floorvalues below
tol=1e-9to zero, so on the affected days the reported and thecorrect distance both map to 0 with the default
indicatorkernel and withexponential_kernel()/squared_exponential_kernel()at their defaultalpha=1.0; they give different feature values for kernels wide enough tostill be nonzero around 200 days, e.g.
exponential_kernel(alpha=0.12)andwider. Either way the sign of the distance is wrong on those days, and the
function does not do what its own comment says.
Fix: return the occurrence with the smallest absolute distance.
Exact ties (possible in leap years, where a date can be 183 days from both
neighbouring occurrences) resolve to the earlier occurrence, which is what
the current code returns in that case, so ties are unchanged.
Tests: added
test_distance_to_nearest_occurrence, pinning the two datesabove plus a leap-year case (
independence_day, 2020-01-20) and three caseswhose result does not change, and
test_distance_never_exceeds_half_a_year,which asserts the
|distance| <= 192invariant for all 18 holidays over twoyears. On the unmodified tree these fail:
pytest test/time_feature/before the change: 105 passed. After: 129passed. Of the 24 added cases, 21 fail on
devand pass with this change;the other 3 are controls that already pass on
devand are included to pinthe behaviour that should not move. Nothing went green to red.
Happy to adjust the bound, the tie-breaking, or the test layout if you'd
prefer them elsewhere.
By submitting this pull request, I confirm that you can use, modify, copy,
and redistribute this contribution, under the terms of your choice.
Please tag this pr with at least one of these labels to make our release process faster: bug fix