RayDeCampo / java-xirr

Java implementation of xirr with bonus Newton-Raphson implementation
MIT License
75 stars 37 forks source link

zero derivative exception #7

Closed hrj closed 5 years ago

hrj commented 5 years ago
  -4.625, 2019-03-14
  -4.375, 2019-03-15
  -3.975, 2019-03-18
  -4.350, 2019-03-19
  -4.725, 2019-03-20
  -5.050, 2019-03-22
  -5.000, 2019-03-25
  -4.750, 2019-03-26
  -3.800, 2019-04-02
  -3.650, 2019-04-03
  -3.500, 2019-04-04
  -3.350, 2019-04-05
  -3.200, 2019-04-08
  -3.050, 2019-04-09
  -2.900, 2019-04-10
  -2.800, 2019-04-11
  -2.700, 2019-04-12
  -2.600, 2019-04-15
  45.000, 2019-04-16

causes this on the latest master commit 5cfe3c24732a3ee948c8b7d587ce8fffc32f0d71

Exception in thread "main" java.lang.ArithmeticException: Newton-Raphson failed due to zero-valued derivative.
    at org.decampo.xirr.NewtonRaphson.inverse(NewtonRaphson.java:93)
    at org.decampo.xirr.NewtonRaphson.findRoot(NewtonRaphson.java:67)
    at org.decampo.xirr.NewtonRaphson$Builder.findRoot(NewtonRaphson.java:147)
    at org.decampo.xirr.Xirr.xirr(Xirr.java:109)
hrj commented 5 years ago

Here's a smaller case that also fails:

-87.000, 2019-01-23
 -88.575, 2019-01-24
 -87.050, 2019-01-25
 252.808, 2019-01-28
RayDeCampo commented 5 years ago

Try using the tools from issue #6 to change the initial guess to make these converge. Note that this happens at times even in commercial spreadsheets, where they will not converge without specifying a guess. Also, I have added details to the exception to make reacting programmatically more feasible.