When I calculate the Internal Rate of Return (irr) using the numpy method irr
, I receive nan
as return.
当我使用numpy方法irr计算内部收益率(irr)时,我收到nan作为回报。
In [45]: numpy.irr([-10, 2, 2, 2, 2])
Out[45]: nan
Shouldn't the results be at least negative? Let's say -8%? When I tried to understand the implementation better, I looked at the master branch of the NumPy repository, but the implementation did not make any sense to me.
结果不应该至少是负面的吗?比方说-8%?当我试图更好地理解实现时,我查看了NumPy存储库的主分支,但实现对我没有任何意义。
The comments and the given literature do not help to understand under which condition nan
is issued. When I calculate the irr with another program, I get -8% returned.
评论和给定的文献无助于理解在哪个条件下发布nan。当我用另一个程序计算irr时,我得到了-8%的回报。
Why is NumPy returning nan
for the array above?
为什么NumPy会为上面的数组返回nan?
2 个解决方案
#1
6
If you look in the implementation of this function, it only looks for solutions for IRR within (0, 1]. This is because the equation can have several solutions and so only a valid one is left. Here it is a rather (IMO) poor implementation choice, because IRR certainly can be outside this range and still be perfectly valid. In your case, I'd suggest to write your own function (along the lines of the existing one) which will do what you need.
如果你看一下这个函数的实现,它只会在(0,1)内寻找IRR的解决方案。这是因为方程可以有多个解决方案,所以只留下一个有效的解决方案。这里它是一个相当的(IMO)糟糕的实现选择,因为IRR当然可以在这个范围之外并且仍然是完全有效的。在你的情况下,我建议编写你自己的功能(沿着现有的功能),它将做你需要的。
#2
4
Just a small correction to the previous answer. The implementation does not limit IRR to (0,1], it limits 1/(1+IRR) to (0,1]. That limits IRR to [0,+Inf). It is still an incomplete implementation because it returns NaN for cash flows which have an IRR less than 0 (i.e. the investor lost money). The correct range for IRR is (-1,+Inf). The correction, however, is not trivial, because NPV(rate) can have more than one zero, but will have no more than one zero crossing where the rate is greater than zero. So limiting the range to [0,+inf) as the function is implemented means you fail on negative IRRs but also never have to deal with multiple roots being returned.
只是对前一个答案的一个小修正。实现不会将IRR限制为(0,1),它将1 /(1 + IRR)限制为(0,1)。这将IRR限制为[0,+ Inf]。它仍然是一个不完整的实现,因为它返回NaN对于IRR小于0的现金流(即投资者赔钱).IRR的正确范围是(-1,+ Inf)。然而,修正并非微不足道,因为NPV(利率)可能超过一个零,但是在速率大于零的情况下将不会超过一个零交叉。因此,当函数实现时,将范围限制为[0,+ inf]意味着您在负IRR上失败但也不必处理多个根被归还。
As a side note, if you're curious about the behavior of NPV(rate), it approaches either +Inf or -Inf as rate approaches -1. The sign of the infinity which it approaches is the same as the sign of the final cashflow. At the other end, as rate approaches +Inf, NPV asymptotically approaches the value of the initial cash flow in the series (usually a negative cash flow). At rate = zero, the value of NPV is the sum of the cash flows.
作为旁注,如果您对NPV(速率)的行为感到好奇,当速率接近-1时,它接近+ Inf或-Inf。它所接近的无穷大的符号与最终现金流的符号相同。另一方面,随着利率接近+ Inf,NPV渐近接近系列中初始现金流的价值(通常是负现金流)。在速率=零时,NPV的值是现金流量的总和。
#1
6
If you look in the implementation of this function, it only looks for solutions for IRR within (0, 1]. This is because the equation can have several solutions and so only a valid one is left. Here it is a rather (IMO) poor implementation choice, because IRR certainly can be outside this range and still be perfectly valid. In your case, I'd suggest to write your own function (along the lines of the existing one) which will do what you need.
如果你看一下这个函数的实现,它只会在(0,1)内寻找IRR的解决方案。这是因为方程可以有多个解决方案,所以只留下一个有效的解决方案。这里它是一个相当的(IMO)糟糕的实现选择,因为IRR当然可以在这个范围之外并且仍然是完全有效的。在你的情况下,我建议编写你自己的功能(沿着现有的功能),它将做你需要的。
#2
4
Just a small correction to the previous answer. The implementation does not limit IRR to (0,1], it limits 1/(1+IRR) to (0,1]. That limits IRR to [0,+Inf). It is still an incomplete implementation because it returns NaN for cash flows which have an IRR less than 0 (i.e. the investor lost money). The correct range for IRR is (-1,+Inf). The correction, however, is not trivial, because NPV(rate) can have more than one zero, but will have no more than one zero crossing where the rate is greater than zero. So limiting the range to [0,+inf) as the function is implemented means you fail on negative IRRs but also never have to deal with multiple roots being returned.
只是对前一个答案的一个小修正。实现不会将IRR限制为(0,1),它将1 /(1 + IRR)限制为(0,1)。这将IRR限制为[0,+ Inf]。它仍然是一个不完整的实现,因为它返回NaN对于IRR小于0的现金流(即投资者赔钱).IRR的正确范围是(-1,+ Inf)。然而,修正并非微不足道,因为NPV(利率)可能超过一个零,但是在速率大于零的情况下将不会超过一个零交叉。因此,当函数实现时,将范围限制为[0,+ inf]意味着您在负IRR上失败但也不必处理多个根被归还。
As a side note, if you're curious about the behavior of NPV(rate), it approaches either +Inf or -Inf as rate approaches -1. The sign of the infinity which it approaches is the same as the sign of the final cashflow. At the other end, as rate approaches +Inf, NPV asymptotically approaches the value of the initial cash flow in the series (usually a negative cash flow). At rate = zero, the value of NPV is the sum of the cash flows.
作为旁注,如果您对NPV(速率)的行为感到好奇,当速率接近-1时,它接近+ Inf或-Inf。它所接近的无穷大的符号与最终现金流的符号相同。另一方面,随着利率接近+ Inf,NPV渐近接近系列中初始现金流的价值(通常是负现金流)。在速率=零时,NPV的值是现金流量的总和。