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

XIRR returns Non Numeric value Encountered #689

Closed
Naveen95 opened this issue Sep 27, 2018 · 6 comments · Fixed by #3262
Closed

XIRR returns Non Numeric value Encountered #689

Naveen95 opened this issue Sep 27, 2018 · 6 comments · Fixed by #3262

Comments

@Naveen95
Copy link

XIRR returning Non Numeric Value Encountered thought the inputs passed are correct. Below is the input passed.

array:2 [▼
0 => -1000000.706
1 => 947003.58
]
array:2 [▼
0 => "2018-09-05"
1 => "2018-09-26"
]

The Expected XIRR Return should be -0.6118824173

@TJ909
Copy link

TJ909 commented Nov 9, 2018

+1

Has anyone encountered a different method for XIRR in PHP? This method for XIRR has difficulty with some negative returns. It's very good at most calculations but is not always the same as Excel with negative returns.

I found another package in PHP, but it looks like the same formula. Found something in C# as well, but unfortunately refactoring that code is outside my skill set.

Other PHP package, same method: https://www.phpclasses.org/package/892-PHP-Financial-functions-with-the-Excel-function-names-.html

C#: https://stackoverflow.com/questions/5179866/xirr-calculation

@stale
Copy link

stale bot commented Jan 14, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Jan 14, 2019
@stale stale bot closed this as completed Jan 22, 2019
@DilipsinhChudasama
Copy link

The same issue is still there for the following data

[ "2022-12-12"=> -20972.36000000 , "2022-12-16"=> 20350.54500000]

@oleibman
Copy link
Collaborator

Reopening issue. PR #2487 fixed a lot of similar issues with XIRR. However, I agree that it fixes neither the original problem mentioned in this ticket nor the new one just added.

@oleibman oleibman reopened this Dec 25, 2022
@stale stale bot removed stale labels Dec 25, 2022
@oleibman
Copy link
Collaborator

The python package xirr tries the Newton-Raphson method and, if that doesn't converge, tries Brent's method (both these methods come from the scipy package). I have confirmed that these failing examples give the expected result in Python, but they wind up using Brent's method. The PhpSpreadsheet code currently uses only Newton-Raphson. I will have to research if it is possible to come up with Php code for Brent. It could take a while.

@oleibman
Copy link
Collaborator

Looking again, only the new issue goes through Brent's method in Python. The original does not, so it's a different problem (which may nevertheless be solved via Brent).

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Dec 26, 2022
Fix PHPOffice#689. XIRR is calculated by making guesses which are hopefully better with each iteration. It is not guaranteed to succeed for Excel, PhpSpreadsheet, or any other implementation. PhpSpreadsheet uses the Newton-Raphson method for its guesses. So does Python package xirr (https://github.com/tarioch/xirr/), but, if Newton-Raphson fails to converge, Python tries Brent's method as an alternative. Two sets of non-converging data are noted in 689. For both, a solution does converge in Excel. For the first of the problems, a solution converges in Python with Newton-Raphson; but, for the second, a solution converges which requires Brent. For the Java package https://github.com/RayDeCampo/java-xirr on which Python was based, and which uses only Newton-Raphson, a solution converges for the first, and does not converge for the second.

To try to match the good results of the others, I added an alternate algorithm if Newton-Raphson fails. Brent's algorithm seems difficult to implement to me. I might have gone there regardless, but I first tried a slightly simpler alternative, bisection. This solved the problem for both of the cases in 689. Perhaps someone will one day report a problem that doesn't converge for Newton-Raphson or bisection, but does for Brent. We can review this decision then.

The new code causes 3 changes in the unit test. In all 3 tests, Excel and PhpSpreadsheet had not converged, but Python and/or Java had. I now believe that Python/Java is correct in those cases, and Excel is not. The new code aligns PhpSpreadsheet with Python/Java for those tests. It is, of course, impossible to know when Excel's implementation doesn't converge, so we aren't guaranteed to match its results in those hopefully rare situations.
oleibman added a commit that referenced this issue Dec 27, 2022
)

Fix #689. XIRR is calculated by making guesses which are hopefully better with each iteration. It is not guaranteed to succeed for Excel, PhpSpreadsheet, or any other implementation. PhpSpreadsheet uses the Newton-Raphson method for its guesses. So does Python package xirr (https://github.com/tarioch/xirr/), but, if Newton-Raphson fails to converge, Python tries Brent's method as an alternative. Two sets of non-converging data are noted in 689. For both, a solution does converge in Excel. For the first of the problems, a solution converges in Python with Newton-Raphson; but, for the second, a solution converges which requires Brent. For the Java package https://github.com/RayDeCampo/java-xirr on which Python was based, and which uses only Newton-Raphson, a solution converges for the first, and does not converge for the second.

To try to match the good results of the others, I added an alternate algorithm if Newton-Raphson fails. Brent's algorithm seems difficult to implement to me. I might have gone there regardless, but I first tried a slightly simpler alternative, bisection. This solved the problem for both of the cases in 689. Perhaps someone will one day report a problem that doesn't converge for Newton-Raphson or bisection, but does for Brent. We can review this decision then.

The new code causes 3 changes in the unit test. In all 3 tests, Excel and PhpSpreadsheet had not converged, but Python and/or Java had. I now believe that Python/Java is correct in those cases, and Excel is not. The new code aligns PhpSpreadsheet with Python/Java for those tests. It is, of course, impossible to know when Excel's implementation doesn't converge, so we aren't guaranteed to match its results in those hopefully rare situations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants