-
Notifications
You must be signed in to change notification settings - Fork 385
[MRG] Fix GAK returning NaN for time series longer than ~405 samples (#450) #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
charavelg
merged 1 commit into
tslearn-team:main
from
samim-reza:fix/450-gak-overflow-long-time-series
Aug 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi!
Thanks for this PR, it is of great interest for our lib! I just have one naive question: why not directly perform the computation in log space, to save time? In other words, why do you need the call to
gak_from_gramin the first place?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rtavenar, thanks for the quick look!
Not naive at all — it's a deliberate trade-off, but the numbers are the real answer.
The linear pass is there because the log-space recursion is much more expensive per cell: the inner loop trades two additions and a multiply for three
expand alog. Timing the numba kernels alone (square gram, best of 7):szSo it isn't a free swap: going straight to log space would slow down every user whose series are short enough to work today. End-to-end on
gak(numpy backend, best of 5):szBelow the threshold the linear pass is ~2x faster; above it you're right that the speculative pass is wasted, and it costs ~20%.
A way to get both, if you want it
The overflow point depends only on the lengths, so it can be decided up front instead of by trial. Every gram entry is
u / (2 - u)withu = exp(-d / 2σ²) ∈ (0, 1], hence≤ 1, so the unnormalized value is bounded by the number of alignment paths — the Delannoy numberD(sz1-1, sz2-1).That bound turns out to be tight exactly where users hit the bug:
which is precisely the 405/406 cutoff reported in #450 / #510 / #188. So
_log_unnormalized_gakcould pick the recursion fromsz1, sz2in O(1) and never run a speculative pass — keeping the linear speed below the threshold and dropping the ~20% overhead above it.Happy to push that, or to drop the linear path entirely if you'd rather keep the code simple and accept the ~2x on short series. Your call — just say which and I'll update.
Two smaller notes while I'm here:
0could make the linear pass silently drop paths: over 400 randomised cases where 4–13% of gram entries underflowed and the guard accepted the linear result, it agreed with the log-space value to 4e-16. A path through a cell withgram < 5e-324carries weight≤ 5e-324, so it only matters when essentially every path is killed — and then the total underflows to0, which the0.0 < valueguard already rejects.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation in this PR seems legit: no overcost for existing working code is good IMHO.