-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added better guess for IRR #61
base: main
Are you sure you want to change the base?
Conversation
I am all for introducing better guesses (having a good guess is critical for Newton-Raphson to converge!) but it seems the method in this PR is producing the wrong answer in several cases. Do you have any examples in which the simple-interest heuristic (the one introduced in the PR you quote, which in turn was replacing the old "always use 0.1 as the guess") produces a wrong result? If not, I'd say that correctness trumps efficiency for the default choice (users can still pass their own guess if they want to!) |
Thanks for the quick reply!
I'm not sure I understand. The test cases are failing, but I would argue that -1.8% is no worse an IRR than 12% (in |
In the first test, the positive cashflows are larger than the negative ones, so a negative IRR doesn't really make sense. Similarly in the second one, how can an IRR of basically -1 be correct? That would basically mean all the investment is lost |
There was a mistake in my code, please see the latest commit. I'm thankful for Regarding |
There are two different considerations here. One is the logical meaning of IRR as a return on an investment. The other is the definition as a solution of a certain polynomial equation. The polynomial has degree n (length of cashflows minus 1) so there will potentially be many solutions. This does NOT mean that EVERY solution should be thought of as a "valid" IRR. In the case of the first example, the fact that the sum of the values is positive together with the fact that not all cashflows have the same sign guarantees that there exist a POSITIVE real solution. Because an investment in which the inflows are smaller than the outflows is intuitively understood as being "profitable", textbook implementations of IRR will always pick the positive solution instead of the negative one. Similarly, if sum of values is negative, it is guaranteed to have a NEGATIVE solution (which is consistent with the logical interpretation as a financial loss). The heuristic I implemented just made sure the initial guess will be closer to a positive solution than to a negative one in the case of positive cashflows, and closer to a negative one in the case of negative cashflows, nothing more. It is not perfect and it tends to "overestimate" the initial guess (either positive or negative), but this is to prevent it from accidentally changing signs, which would be very unintuitive. In your example when you change all the signs in the cashflows, then you have a positive solution, but also a negative one, and using the logical meaning the reasonable choice is the negative one (because outflows are larger than inflows, the investment should be thought of as a loss). We need to be careful here as we are talking about picking a DEFAULT guess, and this has the potential implication of changing the values that people have been getting in their code |
This is a good point, and it made me realize that I need to think about IRR more deeply. I found this interesting & relevant paper: https://www.tandfonline.com/doi/abs/10.1080/00137910490453419. Based on this paper, I think it would be helpful if the user supplied the following additional information to
The input 2. can be inferred for certain cashflows. If More generally we could infer it based on the some heuristics:
The existing heuristic for the IRR is very much in the spirit of investment projects. In fact, for loan projects it guesses the wrong sign. Consider the pure loan project [1, -2] with IRR of 100%. The heuristic gives -50%. It would be good to make sure that a correct root is found, i.e. if the user sends in an investment project, we don't return a root with The input 1. could be used to resolve the remaining ambiguity over which IRR to return.
I'm not familiar with this, do you have a reference for the existence of positive & negative solutions? I think the way it's written is not entirely accurate, e.g. [4, -6, 4, -1] has a single real IRR of -50%, but the sum of cashflows is positive and the cashflows have different signs. Am I missing something here? I think it's true if we require the first cashflow to be negative (and we can relax the assumption of different signs). |
You are right, I was assuming the first cashflow is negative. The requirement that "not all cashflows have the same sign" is necessary for the existence of a real solution. If we rewrite the IRR expression using the gain
for |
There are many ways the computation of the IRR can be sped up, such as choosing a different root finding algorithm like Brent-Q. But the essence of |
@user799595 thanks for taking the time to contribute. I'm all for supporting better guesses. My apologies for taking so long to respond. I've been really busy with work + life outside FOSS. There has been quite a bit of discussion here, I don't have the time to give it the proper depth it deserves, so I'll make some time this week.
I agree with this. It has given numpy-financial a lot of trouble to maintain and was the reason we originally rewrote it using an iterative solver. To me, the most important factor is choosing the financially correct solution. |
Following up on my comment here: #60 (comment)
This PR implements a better guess for IRR.
I tested it on some of my data and it's faster than using guess = 0. Did not benchmark it against the previous heuristic.
Two tests now fail:
test_gh_39
andtest_gh_44
.In the former,
-0.0180967864739624
is found instead of0.12
. In the latter,-0.9997912604283283
is found instead of1.00426
.