Having trouble implementing a rate of return calculation to compare to Excel's XIRR calc which is essentially an solver algorithm to find the discount rate at which the NPV of an investment is zero using provided cash flow and dates in a table.
Per MSFT documentation, the function loops through 100 times (not limited to this..I'm using 10,000 in my code below to test a wide range of rates)
Here is the example with my dummy data to match the above in a single table
t: ([] id:`PROJ_1`PROJ_1`PROJ_1`PROJ_1`PROJ_1`PROJ_1`PROJ_2`PROJ_2`PROJ_2`PROJ_2`PROJ_2`PROJ_2;kdbdate:2021.04.01 2021.12.31 2022.12.31 2023.12.31 2024.12.31 2025.12.31 2021.04.01 2021.12.31 2022.12.31 2023.12.31 2024.12.31 2025.12.31; cf: -800 200 250 300 350 400 -500 150 170 178 250 300);
t: update cum_cf: sums cf by id from t;
t: update irr: count [t]# enlist `float$() from t; // assign a float return value
calcIRR:{[t] update irr: first[irr_func] t by id from t};
updateTable: calcIRR ::;
t: updateTable over t;
irr_func:{[d]; //need to test various discount rates to compare to 0 ;
Pi: exec sums cum_cf from d;
D1: exec first kdbdate from d;
Di: exec last kdbdate from d;
r: .001* til 10000; //create vector of discount rates 10,000 seems excessive but covers a wide range
val:Pi % xexp[(1.0 + r);(Di - D1)%365]; // calculate the value at the different rates
// look for value closest to zero and return the indexed r associated with it
result: val
};
I am looking at kx's documentation on precision https://code.kx.com/q/basics/precision/ to make the comparison.
Thanks in advance!