Repository navigation
Conversation
|
I checked an additional example against this PR's current head, import numpy_financial as npf
print(npf.rate(10, 400, -1000, 0))
print(npf.irr([-1000] + [400] * 10))With this PR's financial module: Released 1.1.0 and current main return Could this go into the regression set if useful? The default-guess result is within These were focused checks of the pinned source module on Python 3.13.5 / NumPy 2.5.3, not a full package build or test-suite run. |
|
I investigated the difference between the two results using a high-precision reference:
The So this is a stopping-tolerance difference. Your example would make a useful regression test: I’ll check it against the high-precision reference at both default and tighter tolerances. |
|
Thanks for checking it against the high-precision value. Testing it at both the default and a tighter tolerance sounds good to me. |
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.The fallback normalizes cash flows before evaluating the equation, so changing currency scale does not cause large finite Decimal inputs to be rejected. Bracket expansion continues in either direction when the other has reached its representable limit; bisection also detects when its endpoints are adjacent representable values and validates the residual before accepting a result.
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.