Skip to content

Updated derivation with improved numerical stability - #1180

Open
seanlaw wants to merge 3 commits into
stumpy-dev:mainfrom
seanlaw:updated_matrix_profile_derivation
Open

Updated derivation with improved numerical stability#1180
seanlaw wants to merge 3 commits into
stumpy-dev:mainfrom
seanlaw:updated_matrix_profile_derivation

Conversation

@seanlaw

Copy link
Copy Markdown
Contributor

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 15%+ 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

Copy link
Copy Markdown

Found 1 changed notebook. Review the changes at https://app.gitnotebooks.com/stumpy-dev/stumpy/pull/1180

@seanlaw

seanlaw commented Aug 12, 2026

Copy link
Copy Markdown
ContributorAuthor

@NimaSarajpoor Would you mind taking a look when you have some time? Thanks in advance!

You may need to download the file in order to get the LaTex to render properly

@NimaSarajpoorNimaSarajpoor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@seanlaw
It was great to see all the steps involved in deriving equation (91). Thanks for putting them together so clearly! I have shared a few comments for your consideration.

Comment threaddocs/REF/Matrix_Profile_Derivation.ipynb Outdated
Comment threaddocs/REF/Matrix_Profile_Derivation.ipynb Outdated
Comment threaddocs/REF/Matrix_Profile_Derivation.ipynb Outdated
Comment threaddocs/REF/Matrix_Profile_Derivation.ipynb
Comment threaddocs/REF/Matrix_Profile_Derivation.ipynb
"3. $dg^{T}_{i,m} = \\left( t_{i+m-1} - M_{T_{i,m}} \\right) + \\left( t_{i-1} - M_{T_{i-1,m}} \\right)$\n",
"4. $dg^{Q}_{i,m} = \\left( q_{i+m-1} - \\mu_{Q_{i,m}} \\right) + \\left( q_{i-1} - \\mu_{Q_{i-1,m}} \\right)$\n",
"\n",
"Then our equation simply becomes:\n",

@NimaSarajpoorNimaSarajpoorAug 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It should be worth it to better demonstrate/explain the benefit of achieving this new equation. We can show that the new equation (91) has LARGER UPDATE and therefore should be more stable as it has less catastrophic cancelation.

Let's start with the equation (40), i.e.

$$cov(Q_{i,m}, T_{i,m}) = cov(Q_{i-1,m}, T_{i-1,m}) + update$$

Let's multiply both sides by m:

$$ m * cov(Q_{i,m}, T_{i,m}) = m * cov(Q_{i-1,m}, T_{i-1,m}) + m * update $$

Recall that m * cov is basically the summation part of "centered sum-of-products". So, this means the update term in the equation (91), i.e. $df^{T}.dg^{Q} + df^{Q}.dg^{T}$, is equivalent to m * update (NOT update). This quickly shows that the new equation has LARGER UPDATE.

@NimaSarajpoorNimaSarajpoorAug 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's see what else we can get from the relationship between equation (40) and (91). Let's start from equation (40) again:

$$cov(Q_{i,m}, T_{i,m}) = cov(Q_{i-1,m}, T_{i-1,m}) + \frac{m-1}{m^{2}}U$$

where

$$ U = [(q_{i+m-1} - \mu_{Q_{i,m-1}})(t_{i+m-1} - M_{T_{i,m-1}}) - (q_{i-1} - \mu_{Q_{i,m-1}}) (t_{i-1} - M_{T_{i,m-1}})]$$


Let's multiply both sides of equation (40) by $m$:

$$m * cov(Q_{i,m}, T_{i,m}) = m * cov(Q_{i-1,m}, T_{i-1,m}) + \frac{m-1}{m}U$$

Again, recall that m * cov is basically the summation part of "centered sum-of-products". So, if I compare the equation above with the equation (91), I can see:

$$ \frac{m-1}{m}U = df^{T}.dg^{Q} + df^{Q}.dg^{T} $$

Note that $\frac{m-1}{m} < 1$. If $U$ is small, then $\frac{m-1}{m}U$ is smaller, meaning the value on the right hand side of the equation above is smaller than $U$. Therefore, if we say that $U$ involves the subtraction of two nearby numbers and that can lead to catastrophic cancellation, then, in that scenario, we will have "catastrophic cancellation" on the right hand side too as that is even smaller!

Can we simply use the update term $\frac{m-1}{m}U$ when we use the summation part of "centered sum-of-products"?

@seanlawseanlawAug 15, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Maybe it's an issue on my end when things are being rendered in my Jupyter notebook but I'm only seeing a single equation number for a block of related/multiline equations:

Screenshot 2026-08-15 at 8 53 58 AM

If you are seeing the same thing, maybe we can refer to the lines as (14.1) - (14.5) (to mean lines 1-5 in equation block 14)? Otherwise, I am counting equations one-by-one each time :(

Or maybe you have a different workflow?

@seanlawseanlawAug 15, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Can we simply use the update term $\frac{m−1}{m}U$ when we use the summation part of "centered sum-of-products"?

I've tried reading your comments a few times but I am unable to get your point.

Therefore, if we say that $U$ involves the subtraction of two nearby numbers and that can lead to catastrophic cancellation, then, in that scenario, we will have "catastrophic cancellation" on the right hand side too as that is even smaller!

I feel like I'm only reading this as a statement of fact but I am getting the sense that you have a much stronger point/argument that I am overlooking :(

You started with:

It should be worth it to better demonstrate/explain the benefit of achieving this new equation.

Maybe I've been staring at the equations too long but I'm not able to see "how" this is demonstrating the benefit any further (i.e., what is the special insight that "proves" the benefit clearly). I know you so I can feel that there is something (intuition?) there. I'm just not smart enough to infer your intuition but I would like to hear more.

@NimaSarajpoorNimaSarajpoorAug 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Or maybe you have a different workflow?

I didn't download the notebook. I just opened the file on Github from your branch. It renders the file differently and I didn't know that it affects the equations numbers! Apologies for the inconvenience.

Please allow me to download the updated notebook and read it again to see the added information. I will use the equation numbers from there.

@NimaSarajpoorNimaSarajpoorAug 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I downloaded and read the (updated) notebook. I opened the file in notebook this time and cannot see equation numbers. Strange!

image

I've tried reading your comments a few times but I am unable to get your point.

If I understand correctly, the gist of this PR is that:

  • The correction term $[(q_{i+m-1} - \mu_{Q_{i,m-1}})(t_{i+m-1} - M_{T_{i,m-1}}) - (q_{i-1} - \mu_{Q_{i,m-1}}) (t_{i-1} - M_{T_{i,m-1}})]$, which involves the subtraction of two nearby numbers, is small and can lead to catastrophic cancellation.
  • The proposal is to use $df^{T}.dg^{Q} + df^{Q}.dg^{T}$ instead (in a new recurrence relation).

HOWEVER, it can be shown that $df^{T}.dg^{Q} + df^{Q}.dg^{T}$ is actually smaller than the original correction term $[(q_{i+m-1} - \mu_{Q_{i,m-1}})(t_{i+m-1} - M_{T_{i,m-1}}) - (q_{i-1} - \mu_{Q_{i,m-1}}) (t_{i-1} - M_{T_{i,m-1}})]$. IMO, it means that if the original "correction" term $[(q_{i+m-1} - \mu_{Q_{i,m-1}})(t_{i+m-1} - M_{T_{i,m-1}}) - (q_{i-1} - \mu_{Q_{i,m-1}}) (t_{i-1} - M_{T_{i,m-1}})]$ is small and can lead to catastrophic cancelation, then $df^{T}.dg^{Q} + df^{Q}.dg^{T}$ is smaller, meaning $(df^{T}.dg^{Q}) - (-df^{Q}.dg^{T})$ can also lead to catastrophic cancelation and it can be even worse!

So, we might be better off using the original correction term when updating the $\overline{QT}$.

Sign up for freeto 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.

2 participants

@seanlaw@NimaSarajpoor