Fix Newton-Raphson derivative sign in swap output solver#2
Open
lolieatapple wants to merge 1 commit intoApostlex0:mainfrom
Open
Fix Newton-Raphson derivative sign in swap output solver#2lolieatapple wants to merge 1 commit intoApostlex0:mainfrom
lolieatapple wants to merge 1 commit intoApostlex0:mainfrom
Conversation
The newton_raphson_solve function uses compute_invariant_derivative to get dI/dy (or dI/dx), but the objective function f(d) = I(x, y - d, L) requires df/dd = -dI/dy by the chain rule. Without negating, the Newton step moves in the wrong direction, causing divergence or reliance on damping for convergence.
|
@lolieatapple is attempting to deploy a commit to the sachin's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
Bug: Missing chain-rule negation in
newton_raphson_solveSummary
The
newton_raphson_solvefunction ininvariant.moveuses the wrong sign for the derivative when solving for swap output amounts. The functioncompute_invariant_derivativereturns ∂I/∂y (or ∂I/∂x), but the Newton-Raphson objective function isf(d) = I(x, y − d, L), so by the chain rule the correct derivative isdf/dd = −∂I/∂y. Without negating, each Newton step moves in the opposite direction from the root.Root Cause
In
newton_raphson_solve(line 252–253), the derivative is used directly:let deriv = compute_invariant_derivative(&curr_x, &curr_y, liquidity_L, solving_for_y); // ... let adjust = signed_fixed_point::div(&f_val, &deriv);compute_invariant_derivativereturns:∂I/∂y = Φ(z) − 1 = P − 1(always negative, since P < 1)∂I/∂x = −Φ(z) = −P(always negative)But the solver is finding the output amount
d, not the reserve coordinate directly. Sincey_actual = y_after − d:The Newton update
d_new = d − f/f'requires the correct derivative sign. UsingP − 1(negative) instead of1 − P(positive) reverses the step direction.Numerical Example
Pool at P = 0.5, L = 100,000 → reserves ≈ (39,759, 39,759). Swap input: 397 X tokens.
With the correct derivative, Newton converges in ~2 iterations. With the wrong sign, each step moves away from the solution.
Why the code doesn't completely break
The damping logic partially masks the bug:
if (fixed_point::greater_than(&adjust_abs, &half_current)) { adjust_abs = fixed_point::div(&adjust_abs, &fixed_point::two()); };This clamps the step size, preventing the solver from completely exploding. Combined with 20 max iterations, the solver sometimes stumbles close enough to the root through oscillation. However:
E_INSUFFICIENT_OUTPUTFix
One-line change: negate the derivative before using it in the Newton step.
let raw_deriv = compute_invariant_derivative(&curr_x, &curr_y, liquidity_L, solving_for_y); let deriv = signed_fixed_point::negate(&raw_deriv); // chain rule: df/dd = -dI/dy