Skip to content

Commit e47c5d9

Browse files
DOC: Describe method to solve IRR
Co-authored-by: Jamie Cook <jamie.cook@veitchlister.com.au>
1 parent e15e809 commit e47c5d9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

numpy_financial/_financial.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,19 @@ def irr(values, guess=0.1):
741741
if values.ndim != 1:
742742
raise ValueError("Cashflows must be a rank-1 array")
743743

744-
# NPV = V0 * (1+eirr)^0 + V1 * (1+eirr)^-1 + ...
745-
# let x = 1 / (1+eirr)
746-
# NPV = V0 * x^0 + V1 * x^1 + ...
744+
# We aim to solve eirr such that NPV is exactly zero. This can be framed as
745+
# simply finding the closest root of a polynomial to a given initial guess
746+
# as follows:
747+
# V0 V1 V2 V3
748+
# NPV = ---------- + ---------- + ---------- + ---------- + ...
749+
# (1+eirr)^0 (1+eirr)^1 (1+eirr)^2 (1+eirr)^3
750+
#
751+
# by letting x = 1 / (1+eirr), we substitute to get
752+
#
753+
# NPV = V0 * x^0 + V1 * x^1 + V2 * x^2 + V3 * x^3 + ...
754+
#
755+
# which we solve using Newton-Raphson and then reverse out the solution
756+
# as eirr = 1/x - 1 (if we are close enough to a solution)
747757
npv_ = np.polynomial.Polynomial(values)
748758
d_npv = npv_.deriv()
749759
x = 1 / (1 + guess)

0 commit comments

Comments
 (0)