Use box midpoint to choose finite-difference direction in jacobian_fd#3301
Open
Nas01010101 wants to merge 1 commit into
Open
Use box midpoint to choose finite-difference direction in jacobian_fd#3301Nas01010101 wants to merge 1 commit into
Nas01010101 wants to merge 1 commit into
Conversation
When bounds are provided, `jacobian_fd` chooses each coordinate's finite-difference direction so the perturbation steps away from the nearer bound. It compared `x` against `0.5 * (bounds[1] - bounds[0])`, which is half the box *width*, not the box midpoint. For bounds that are not centered on the origin this selects the wrong direction, and at the lower bound the perturbation steps outside the box. Compare against the midpoint `0.5 * (bounds[0] + bounds[1])` instead. Adds a regression test checking that all residual evaluations stay within an off-center box when `x` is at the lower bound; it fails before this change and passes after.
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.
Problem
In
minimize.py,jacobian_fdchooses each coordinate's finite-difference direction so the perturbation steps away from the nearer bound. It comparedxagainstbut
0.5 * (bounds[1] - bounds[0])is half the box width, not the box midpoint. For bounds that are not centered on the origin this picks the wrong direction. Concretely, withbounds = [10, 20]the comparison value is5.0(true midpoint is15.0), so a point at the lower boundx = 10satisfiesx > 5and steps −eps, i.e. to9.9999998— outside the box.Fix
Compare against the actual midpoint:
At the lower bound the step is now
+eps(inward); at the upper bound−eps(inward).Tests
Adds
test_jacobian_fd_respects_off_center_bounds, which records every pointjacobian_fdevaluates withxat the lower bound of the off-center box[10, 20]and asserts they all stay within[lo, hi]. It fails before this change (evaluates9.9999998) and passes after; the rest ofminimize_test.pyremains green.