RayDeCampo / java-xirr

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

org.decampo.xirr.NonconvergenceException for high value dataset #10

Closed neosis closed 5 years ago

neosis commented 5 years ago

Hi

Tried calculating xirr as follows -

final double xirr = new Xirr( new Transaction(-516250000, "2019-12-01"), new Transaction(6676867.348, "2019-12-31"), new Transaction(-23985279.65, "2020-03-31"), new Transaction(-9226199.498, "2020-06-30"), new Transaction(-4756239.031, "2020-09-30"), new Transaction(9424601.751, "2020-12-31"), new Transaction(13316322.85, "2021-03-31"), new Transaction(42283061.51, "2021-06-30"), new Transaction(48107917.48, "2021-09-30"), new Transaction(73370583.37, "2021-12-31"), new Transaction(78071059.19, "2022-03-31"), new Transaction(88001127.23, "2022-06-30"), new Transaction(93155837.61, "2022-09-30"), new Transaction(98048409.57, "2022-12-31"), new Transaction(102678843.1, "2023-03-31"), new Transaction(91850303.81, "2023-06-30"), new Transaction(36640855.3, "2023-09-30") ).xirr();

Got following error -

org.decampo.xirr.NonconvergenceException: Newton-Raphson failed to converge within 10000 iterations.

at org.decampo.xirr.NewtonRaphson.inverse(NewtonRaphson.java:102)
at org.decampo.xirr.NewtonRaphson.findRoot(NewtonRaphson.java:70)
at org.decampo.xirr.NewtonRaphson$Builder.findRoot(NewtonRaphson.java:149)
at org.decampo.xirr.Xirr.xirr(Xirr.java:155)
at org.decampo.xirr.XirrTest.xirr_vs_spreadsheet_1(XirrTest.java:82)

Any help/pointers where I am going wrong? Will providing guess help?

Best Ashutosh

RayDeCampo commented 5 years ago

You managed to hit a cycle where the candidates kept going through the same set of numbers over and over without quite getting the value close to zero. It will converge if you ease up on the tolerance:

        final double xirr = Xirr.builder()
            .withNewtonRaphsonBuilder(
                NewtonRaphson.builder().withTolerance(0.000_001))
            .withTransactions(
                new Transaction(-516250000, "2019-12-01"),
                new Transaction(6676867.348, "2019-12-31"),
                new Transaction(-23985279.65, "2020-03-31"),
                new Transaction(-9226199.498, "2020-06-30"),
                new Transaction(-4756239.031, "2020-09-30"),
                new Transaction(9424601.751, "2020-12-31"),
                new Transaction(13316322.85, "2021-03-31"),
                new Transaction(42283061.51, "2021-06-30"),
                new Transaction(48107917.48, "2021-09-30"),
                new Transaction(73370583.37, "2021-12-31"),
                new Transaction(78071059.19, "2022-03-31"),
                new Transaction(88001127.23, "2022-06-30"),
                new Transaction(93155837.61, "2022-09-30"),
                new Transaction(98048409.57, "2022-12-31"),
                new Transaction(102678843.1, "2023-03-31"),
                new Transaction(91850303.81, "2023-06-30"),
                new Transaction(36640855.3, "2023-09-30")
            ).xirr();
neosis commented 5 years ago

Worked with specified tolerance.

Best Ashutosh