DaSarfyCode
DaSarfyCode

Reputation: 445

Calculating XIRR in Python

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?

Reference: What is XIRR?

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

Upvotes: 10

Views: 23038

Answers (3)

Alexander Volkovsky
Alexander Volkovsky

Reputation: 2918

Created a package for fast XIRR calculation, PyXIRR

It doesn't have external dependencies and works faster than any existing implementation.

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}))

Upvotes: 13

mattf1216
mattf1216

Reputation: 11

This implementation calculates the time delta once and then vectorizes the NPV calculation. It should run much faster than @pyCthon's solution for larger datasets. The input is a pandas series of cashflows with dates for the index.

Code

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

Tests

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))

Upvotes: 1

pyCthon
pyCthon

Reputation: 12361

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)

Upvotes: 5

Related Questions