【发布时间】:2021-02-22 20:50:20
【问题描述】:
无法实现收益率计算以与 Excel 的 XIRR calc 进行比较,后者本质上是一种求解算法,用于使用提供的现金流和表格中的日期来找到投资 NPV 为零的贴现率。
根据 MSFT 文档,该函数循环 100 次(不限于此......我在下面的代码中使用 10,000 来测试各种速率)
这是我的虚拟数据在单个表中匹配上述示例的示例
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
};
我正在查看 kx 关于精度 https://code.kx.com/q/basics/precision/ 的文档以进行比较。
提前致谢!
【问题讨论】:
标签: kdb