Conversation
This branch has not been deployed
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.
ratecan converge to an algebraic root below -100% for the cash flows in #126. This PR restricts accepted results tor > -1: it keeps Newton for ordinary cases and adds a bracket-search/bisection fallback when Newton converges at or below -1.For the inputs matching the reported IRR cash flows,
rate(8, 263175, -440000, 25500)returns approximately 0.583878 instead of -1.855744. For the original, different inputs, the result is approximately 1.671184 instead of -1.896442.The fallback searches around the supplied guess, uses the remaining per-element iteration budget, and checks both bracket width and a scaled equation residual. It evaluates the zero-rate residual explicitly and uses
log1p/expm1for floating-point inputs. Nonfinite guesses and guesses at or below -1 now raiseValueError.Compatibility and scope
This intentionally excludes roots at or below -100%, which can be algebraically valid for integer periods but fall outside the positive compounding-factor model for conventional loans and annuities. Newton's intermediate steps are not constrained; this restriction applies to accepted results and the fallback search.
The change targets converged out-of-domain roots, rather than generally retrying Newton failures. Bracket discovery is heuristic: it may miss tangent roots and does not guarantee the root nearest the guess. If both search directions find brackets in the same expansion, the left bracket is selected first. The existing Newton stopping criterion is unchanged.
Validation
Refs #126.
AI assistance was used to implement this change, run validation, and prepare this description.