Skip to content

Commit 4046a2c

Browse files
authored
Avoid redundant computations in IRR calculation
The IRR computation adds and then subtract the previous iteration value before comparing to the tolerance. We can just compute the delta and compare that to the tolerance instead. This should also make the computation more robust if there is a large difference in magnitude between `x` and `delta`
1 parent 1d2e229 commit 4046a2c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

numpy_financial/_financial.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,10 @@ def irr(values, guess=0.1, tol=1e-12, maxiter=100):
773773
x = 1 / (1 + guess)
774774

775775
for _ in range(maxiter):
776-
x_new = x - (npv_(x) / d_npv(x))
777-
if abs(x_new - x) < tol:
778-
return 1 / x_new - 1
779-
x = x_new
776+
delta = npv_(x) / d_npv(x)
777+
if abs(delta) < tol:
778+
return 1 / (x - delta) - 1
779+
x -= delta
780780

781781
return np.nan
782782

0 commit comments

Comments
 (0)