-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
XIRR financial function not calculating as in Excel #2469
Comments
I am able to verify that there is a problem. However, your result XIRR is calculated by making an initial guess and then using an algorithm which is fast but is not guaranteed to converge. Your example falls into one of those non-converging areas with the initial default guess of 0.1. However, noticing that the correct answer is negative, I took a shot and supplied an initial guess of -0.1, and that did converge to the correct result. It is certainly possible to change the code to try the negative of the original guess if the original guess fails. It solves this problem and doesn't break anything. I'm sure that's not the smartest possible approach, but I will at least have a PR out within a day or two that adopts that approach while I study the problem further. Note that the existing unit tests already have at least one example of a converging negative result even with the default guess. |
Firstly, thank you @oleibman for taking a look at this. Indeed something has been miskeyed because when I re-keyed the above into a new Excel worksheet I get In the library, when I enter I take your point about the nature of the proposed solution. I don't know how Excel converges but it is doing so with default Would be good to have this scenario added to the test suite. Let me know if I can help |
Fix PHPOffice#2469. The algorithm used for XIRR is known not to converge in some cases, some of which are because the value is legitimately unsolvable; for others, using a different guess might help. The algorithm uses continual guesses at a rate to hopefully converge on the solution. The code in Python package xirr (https://github.com/tarioch/xirr/) suggests a refinement when this rate falls below -1. Adopting this refinement solves the problem for the data in issue 2469 without any adverse effect on the existing tests. My thanks to @tarioch for that refinement. The data from 2469 is, of course, added to the test cases. The user also mentions that an initial guess equal to the actual result doesn't converge either. A test is also added to confirm that that case now works. The test cases are changed to run in the context of a spreadsheet rather than by direct calls to XIRR calculation routine. This revealed some data validation errors which are also cleaned up with this PR. This suggests that other financial tests might benefit from the same change; I will look into that.
* Refinement for XIRR Fix #2469. The algorithm used for XIRR is known not to converge in some cases, some of which are because the value is legitimately unsolvable; for others, using a different guess might help. The algorithm uses continual guesses at a rate to hopefully converge on the solution. The code in Python package xirr (https://github.com/tarioch/xirr/) suggests a refinement when this rate falls below -1. Adopting this refinement solves the problem for the data in issue 2469 without any adverse effect on the existing tests. My thanks to @tarioch for that refinement. The data from 2469 is, of course, added to the test cases. The user also mentions that an initial guess equal to the actual result doesn't converge either. A test is also added to confirm that that case now works. The test cases are changed to run in the context of a spreadsheet rather than by direct calls to XIRR calculation routine. This revealed some data validation errors which are also cleaned up with this PR. This suggests that other financial tests might benefit from the same change; I will look into that. * More Unit Tests From https://github.com/RayDeCampo/java-xirr/blob/master/src/test/java/org/decampo/xirr/XirrTest.java https://github.com/tarioch/xirr/blob/master/tests/test_math.py Note that there are some cases where the PHP tests do not converge, but the non-PHP tests do. I have confirmed in each of those cases that Excel does not converge, so the PhpSpreadsheet results are good, at least for now. The discrepancies are noted in comments in the test member.
This is:
What is the expected behavior?
XIRR should be able to return a value -0.6422991 for this simple use case
What is the current behavior?
XIRR returns #NUM!
What are the steps to reproduce?
Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:
Which versions of PhpSpreadsheet and PHP are affected?
PhpSpreadsheet 1.20.0 (tested with 1.15 also using pre namespace changes)
PHP 7.4 and 8.0
The text was updated successfully, but these errors were encountered: