具有 xirr 和 xnpv 功能的金融 python 库?
numpy 有 irr 和 npv 函数,但我需要 xirr 和 xnpv 函数。
此链接指出 xirr 和 xnpv 即将推出。 http://www.projectdirigible.com/documentation/spreadsheet-functions.html #coming-soon
有没有Python库具有这两个功能?谢了。
numpy has irr and npv function, but I need xirr and xnpv function.
this link points out that xirr and xnpv will be coming soon.
http://www.projectdirigible.com/documentation/spreadsheet-functions.html#coming-soon
Is there any python library that has those two functions? tks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是实现这两个功能的一种方法。
Here is one way to implement the two functions.
在我在网上找到的各种实现的帮助下,我想出了一个 python 实现:
With the help of various implementations I found in the net, I came up with a python implementation:
创建了一个用于快速 XIRR 计算的包,PyXIRR
它没有外部依赖项,并且比任何现有实现都运行得更快。
Created a package for fast XIRR calculation, PyXIRR
It doesn't have external dependencies and works faster than any existing implementation.
这个答案是对 @uuazed 答案的改进,并源自于此。但是,有一些变化:
解释:
在测试块中,它检查增加贴现率是否会增加或减少贴现值。根据此测试,确定猜测应该朝哪个方向移动。该块使函数能够处理现金流,而不管用户假设的方向如何。
np.sign(residual) != np.sign(prev_residual)
检查猜测何时增加/减少超出所需的 XIRR 率,因为此时残差从负变为正,反之亦然。此时步长减小。numpy 包并不是绝对必要的。如果没有 numpy,
np.sign(residual)
可以替换为residual/abs(residual)
。我使用 numpy 使代码更具可读性和直观性我尝试使用各种现金流来测试此代码。如果您发现此功能无法处理的任何情况,请告诉我。
编辑:这是使用 numpy 数组的更清晰、更快的代码版本。在我对大约 700 个事务的测试中,此代码的运行速度比上面的代码快 5 倍:
This answer is an improvement on @uuazed's answer and derives from that. However, there are a few changes:
Explanation:
In the test block, it checks whether increasing the discounting rate increases the discounted value or reduces it. Based on this test, it is determined which direction the guess should move. This block makes the function handle cashflows regardless of direction assumed by the user.
The
np.sign(residual) != np.sign(prev_residual)
checks when the guess has increased/decreased beyond the required XIRR rate, because that's when the residual goes from negative to positive or vice versa. The step size is reduced at this point.The numpy package is not absolutely necessary. without numpy,
np.sign(residual)
can be replaced withresidual/abs(residual)
. I have used numpy to make the code more readable and intuitiveI have tried to test this code with a variety of cash flows. If you find any cases which are not handled by this function, do let me know.
Edit: Here's a cleaner and faster version of the code using numpy arrays. In my test with about 700 transaction, this code ran 5 times faster than the one above:
我从 @KT 的解决方案开始,但在几个方面进行了改进:
下面具体示例的主要发现(其他情况的结果可能会有所不同,我没有时间测试许多其他情况):
牛顿与 fsolve 的执行时间:
结论
这是我的最终代码:
测试
放在一起的一些测试
这些是我与pytest xnpv 和 numpy.npv 之间的重要区别
严格来说,这与这个答案无关,但对于使用 numpy 进行财务计算的人来说,了解它很有用:
numpy.npv 不会对现金流的第一项进行折扣 - 它从其次,例如
和
Excel,第一项的折扣:
Numpy 的财务功能将被弃用,并替换为 numpy_financial 的功能,但 numpy_financial 的行为可能会继续相同,如果只是为了向后兼容。
I started from @KT 's solution but improved on it in a few ways:
Key findings for the specific example below (results may well be different for other cases, I have not had the time to test many other cases):
Execution time with newton vs fsolve:
Conclusions
This is my final code:
Tests
These are some tests I have put together with pytest
P.S. Important difference between this xnpv and numpy.npv
This is not, strictly speaking, relevant to this answer, but useful to know for whoever runs financial calculations with numpy:
numpy.npv doesn't discount the first item of cashflow - it starts from the second, e.g.
and
Excel, however, discounts from the very first item:
Numpy's financial functions will be deprecated and replaced with those of numpy_financial, which however will likely continue to behave the same, if only for backward compatibility.
创建了一个可用于 xirr 计算的 python 包 finance-calulator 。底层,它使用牛顿法。
我还做了一些时间分析,它比 @KT. 的答案中建议的 scipy 的 xnpv 方法好一点。
这是实现。
Created a python package finance-calulator which can be used for xirr calculation. underlying, it uses newton's method.
Also I did some time profiling and it is little better than the scipy's xnpv method as suggested in @KT.'s answer.
Here's the implementation.
有了 Pandas,我可以完成以下工作:
(注意,我使用的是 ACT/365 约定)
With Pandas, I got the following to work:
(note, I'm using ACT/365 convention)