Skip to content

Fix precision loss for identical subsequences - #1206

Open
iAnjaneySingh wants to merge 1 commit into
stumpy-dev:mainfrom
iAnjaneySingh:investigate/issue-1160
Open

Fix precision loss for identical subsequences#1206
iAnjaneySingh wants to merge 1 commit into
stumpy-dev:mainfrom
iAnjaneySingh:investigate/issue-1160

Conversation

@iAnjaneySingh

Copy link
Copy Markdown

Description

Fixes #1160.

When two identical subsequences are compared, the rolling covariance
recurrence can accumulate a small floating-point error. This can produce
a Pearson correlation slightly below 1.0 and therefore a small non-zero
distance for exactly identical subsequences.

This change detects cases where the computed Pearson correlation is
extremely close to 1.0 and the two raw subsequences are exactly equal,
then sets the Pearson correlation to 1.0.

Validation

  • Reproduced the failure from Investigate Failed Test in test_stump.py #1160.
  • Confirmed the discrepancy came from floating-point error in the
    rolling covariance recurrence.
  • tests/test_stump.py: 29 passed.
  • Full test suite: passed.
  • Test coverage: 100%.

Pull Request Checklist

Below is a simple checklist but please do not hesitate to ask for assistance!

  • Read our Contributing Guide
  • Referenced a Github issue (or create one if one doesn't already exist)
  • Read and reviewed all of the comments in the Github issue that you've referenced (along with cross-referenced issues/pull requests) to ensure that the issue still requires a pull request
  • Checked that the issue has not already been assigned to anybody else or is already being addressed in another pull request
  • Left a meaningful comment on the original Github issue to discuss the detailed approach for your contribution and received confirmation from the maintainers before proceeding with this pull request
  • Forked, cloned, and checked out the newest version of the code
  • Created a new branch
  • Made necessary code changes
  • Installed black (i.e., python -m pip install black or conda install -c conda-forge black)
  • Installed flake8 (i.e., python -m pip install flake8 or conda install -c conda-forge flake8)
  • Installed pytest-cov (i.e., python -m pip install pytest-cov or conda install -c conda-forge pytest-cov)
  • Ran black --exclude=".*\.ipynb" --extend-exclude=".venv" --diff ./ in the root stumpy directory
  • Ran flake8 --extend-exclude=.venv ./ in the root stumpy directory
  • Ran ./setup.sh dev && ./test.sh in the root stumpy directory and ensured that all tests are passing locally
  • Check this box if AI code assistance was used to generate
    • less than 25% of the code in this pull request
    • 25-50% of the code in this pull request
    • more than 50% of the code in this pull request

Please do not commit any code to avoid/circumvent a failing test and, instead, engage in a discussion (below) to determine the best course of action.

Only request a review after the checklist above is fully completed!

@gitnotebooks

gitnotebooks Bot commented Sep 12, 2026

Copy link
Copy Markdown

Review these changes at https://app.gitnotebooks.com/stumpy-dev/stumpy/pull/1206

@seanlaw seanlaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iAnjaneySingh I've left a comment for you to address and consider

Comment thread stumpy/stump.py
pearson = min(1.0, pearson)

if pearson > 1.0 - 1e-8 and np.array_equal(
T_A[uint64_i : uint64_i + uint64_m],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _compute_diagonal function is the most important function in our entire package and needs to be highly performant (i.e., without bloat or unneeded code).

Adding np.array_equal is bad because:

  1. it would dramatically slow down the computation, say, for a long time series with mostly zero values
  2. This does not take into account time series with identical subsequences that are only different by a constant vertical shift

Additionally, what's missing is that you should clearly demonstrate that the originally failing test is passing after this fix is applied. That is the most important criteria (in addition to the regular test suite passing)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed feedback, Sean. That makes sense, especially since _compute_diagonal is performance-critical. I’ll remove the np.array_equal check and look into a more efficient numerical fix that also handles subsequences that differ by a constant shift. I will also make sure to clearly show the original failing test passing after the fix, along with the full test suite results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iAnjaneySingh Another approach that we had discussed before was adding a (optional) post-processing step. So, after the matrix profile is computed, could we take the subsequence pairs that have:

  1. a distance/pearson value below a certain threshold
  2. (nearly) identical standard deviations
  3. select a handful of data points (e.g., 4 data points) from each pair of subsequences and see if their pairwise differences are the same
  4. Maybe some other fast criteria that leverages numbers that we've already computed

If so, then we recompute the dot product for these two subsequences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate Failed Test in test_stump.py

2 participants