Skip to content
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

Refinement for XIRR #2487

Merged
merged 3 commits into from
Jan 14, 2022
Merged

Refinement for XIRR #2487

merged 3 commits into from
Jan 14, 2022

Conversation

oleibman
Copy link
Collaborator

@oleibman oleibman commented Jan 6, 2022

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 Python project cites https://github.com/RayDeCampo/java-xirr as a reference. Thanks also to @RayDeCampo.

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.

This is:

- [x] a bugfix
- [ ] a new feature

Checklist:

Why this change is needed?

Owen Leibman added 2 commits January 6, 2022 08:33
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.
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.
@blacknell
Copy link
Contributor

Great work. For what it's worth, I re-ran my workflow on my dataset with the fixes and it worked a charm. Previously, I was getting 285 failed convergences out of a total of 7,177 calls to the XIRR method. Now, there are zero.

Furthermore, these were masking an additional 15 failures caused by bad data on my part which I've now been able to fix.

@oleibman
Copy link
Collaborator Author

Wonderful, thank you for the confirmation. I hope to be able to merge this by the weekend.

@oleibman oleibman merged commit 95d9cc9 into PHPOffice:master Jan 14, 2022
@oleibman oleibman deleted the xirragain branch February 6, 2022 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

XIRR financial function not calculating as in Excel
2 participants