Skip to content

Conversation

araujoms
Copy link
Collaborator

There's a catastrophic cancellation happening in the computation of the divided differences matrix, as @antoine-levitt told me about on Slack. The problem is when we don't approximate the divided difference by the derivative of the mean, and the values are still close to eachother. Currently the threshold is set to be abs(x-y) < sqrt(eps(T)). Then the error of (f(x)-f(y))/(x-y) will be roughly eps(T)/sqrt(eps(T)) = sqrt(eps(T)), which is pretty bad. Things become catastrophic when we look at the second-order divided difference, then the error is roughly eps(T)/(sqrt(eps(T)) * sqrt(eps(T))) = 1, and the third order that gives us error 1/sqrt(eps(T)).

Luckily there's a simple fix: the error incurred by using the derivative of the mean is roughly tol^2, so we can use it for x,y that are further apart instead of the naïve formula. Equating both errors gives us eps(T) / tol = tol^2 => tol = cbrt(eps(T)). Substituting that into the formula for the errors of the second-order divided difference gives us eps(T)/ (tol * tol_2) = tol_2^2 => tol_2 = eps(T)^(2/9), and doing the same for the third order gives us eps(T)^(4/27).

Copy link

codecov bot commented Sep 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.73%. Comparing base (e350ede) to head (e8f31ad).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #858      +/-   ##
==========================================
+ Coverage   96.68%   96.73%   +0.04%     
==========================================
  Files          56       56              
  Lines        9092     9103      +11     
==========================================
+ Hits         8791     8806      +15     
+ Misses        301      297       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkapelevich
Copy link
Collaborator

Can we please rename rteps, maybe thresh? Otherwise I trust you and good by me to merge.

Note to self, eps(T) / tol = tol^2 equates the cancellation error to the truncation error of the mean approximation.

@araujoms araujoms merged commit 01488be into master Sep 27, 2025
5 checks passed
@araujoms araujoms deleted the araujoms/divided_differences branch September 27, 2025 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants