pandas 在 Python 中计算 XIRR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46668172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:36:51  来源:igfitidea点击:

Calculating XIRR in Python

pythonpandasnumpy

提问by DaSarfyCode

I need to calculate XIRR of financial investments made over a period of time. Is there any function to do this in numpy, pandas or plain python?

我需要计算一段时间内进行的金融投资的 XIRR。在 numpy、pandas 或普通 python 中是否有任何功能可以做到这一点?

Reference: What is XIRR?

参考:什么是 XIRR?

The accepted answer in the original question is not correct and can be improved.

原始问题中接受的答案不正确,可以改进。

回答by pyCthon

Here's an implementation taken from here.

下面是取自实施这里

import datetime
from scipy import optimize

def xnpv(rate,cashflows):
    chron_order = sorted(cashflows, key = lambda x: x[0])
    t0 = chron_order[0][0]
    return sum([cf/(1+rate)**((t-t0).days/365.0) for (t,cf) in chron_order])

def xirr(cashflows,guess=0.1):
    return optimize.newton(lambda r: xnpv(r,cashflows),guess)