在Python中计算XIRR

时间:2017-10-10 13:35:11

标签: python pandas numpy

我需要计算一段时间内进行的金融投资的XIRR。在numpy,pandas或plain python中是否有任何函数可以执行此操作?

Reference: What is XIRR?

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

3 个答案:

答案 0 :(得分:1)

为快速 XIRR 计算创建了一个包,PyXIRR

它没有外部依赖项,并且比任何现有实现都运行得更快。

from datetime import date
from pyxirr import xirr

dates = [date(2020, 1, 1), date(2021, 1, 1), date(2022, 1, 1)]
amounts = [-1000, 1000, 1000]

# feed columnar data
xirr(dates, amounts)

# feed tuples
xirr(zip(dates, amounts))

# feed DataFrame
import pandas as pd
xirr(pd.DataFrame({"dates": dates, "amounts": amounts}))

答案 1 :(得分:0)

以下是here的实施。

import datetime
from scipy import optimize

def xnpv(rate,cashflows):
    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)

答案 2 :(得分:0)

此实现计算一次时间增量,然后矢量化 NPV 计算。对于更大的数据集,它应该比 @pyCthon 的解决方案运行得快得多。输入是带有指数日期的熊猫系列现金流量。

代码

import pandas as pd
import numpy as np
from scipy import optimize

def xirr2(valuesPerDate):
  """  Calculate the irregular rate of return.
  valuesPerDate is a pandas series of cashflows with index of dates.
  """

  # Clean values
  valuesPerDateCleaned = valuesPerDate[valuesPerDate != 0]

  # Check for sign change
  if valuesPerDateCleaned.min() * valuesPerDateCleaned.max() >= 0:
    return np.nan

  # Set index to time delta in years
  valuesPerDateCleaned.index = (valuesPerDateCleaned.index - valuesPerDateCleaned.index.min()).days / 365.0

  result = np.nan
  try:
    result = optimize.newton(lambda r: (valuesPerDateCleaned / ((1 + r) ** valuesPerDateCleaned.index)).sum(), x0=0, rtol=1e-4)
  except (RuntimeError, OverflowError): 
    result = optimize.brentq(lambda r: (valuesPerDateCleaned / ((1 + r) ** valuesPerDateCleaned.index)).sum(), a=-0.999999999999999, b=100, maxiter=10**4)

  if not isinstance(result, complex):
    return result
  else:
    return np.nan

测试

valuesPerDate = pd.Series()
for d in pd.date_range(start='1990-01-01', end='2019-12-31', freq='M'):
  valuesPerDate[d] = 10*np.random.uniform(-0.5,1)
valuesPerDate[0] = -100

print(xirr2(valuesPerDate))