from datetime import date
from pyxirr import xirr
dates = [date(2020, 1, 1), date(2021, 1, 1), date(2022, 1, 1)]
amounts = [-1000, 750, 500]
# feed columnar data
xirr(dates, amounts)
# feed iterators
xirr(iter(dates), (x / 2 for x in amounts))
# feed an iterable of tuples
xirr(zip(dates, amounts))
# feed a dictionary
xirr(dict(zip(dates, amounts)))
# dates as strings
xirr(['2020-01-01', '2021-01-01'], [-1000, 1200])
Multiple IRR problem
The Multiple IRR problem occurs when the signs of cash flows change more than
once. In this case, we say that the project has non-conventional cash flows.
This leads to situation, where it can have more the one IRR or have no IRR at all.
PyXIRR addresses the Multiple IRR problem as follows:
It looks for positive result around 0.1 (the same as Excel with the default guess=0.1).
If it can't find a result, it uses several other attempts and selects the lowest IRR to be conservative.
Here is an example illustrating how to identify multiple IRRs:
import numpy as np
import pyxirr
# load cash flow:
cf = pd.read_csv("tests/samples/30-22.csv", names=["date", "amount"])
# check whether the cash flow is conventional:
print(pyxirr.is_conventional_cash_flow(cf["amount"])) # false
# build NPV profile:
# calculate 50 NPV values for different rates
rates = np.linspace(-0.5, 0.5, 50)
# any iterable, any rates, e.g.
# rates = [-0.5, -0.3, -0.1, 0.1, -0.6]
values = pyxirr.xnpv(rates, cf)
# print NPV profile:
# NPV changes sign two times:
# 1) between -0.316 and -0.295
# 2) between -0.03 and -0.01
print("NPV profile:")
for rate, value in zip(rates, values):
print(rate, value)
# plot NPV profile
import pandas as pd
series = pd.Series(values, index=rates)
pd.DataFrame(series[series > -1e6]).assign(zero=0).plot()
# find points where NPV function crosses zero
indexes = pyxirr.zero_crossing_points(values)
print("Zero crossing points:")
for idx in indexes:
print("between", rates[idx], "and", rates[idx+1])
# XIRR has two results:
# -0.31540826742734207
# -0.028668460065441048
for i, idx in enumerate(indexes, start=1):
rate = pyxirr.xirr(cf, guess=rates[idx])
npv = pyxirr.xnpv(rate, cf)
print(f"{i}) {rate}; XNPV = {npv}")
If all input is scalar, returns a scalar float. If any input is array_like,
returns values for each input element. If multiple inputs are
array_like, performs broadcasting and returns values for each element.
import pyxirr
# feed list
pyxirr.fv([0.05/12, 0.06/12], 10*12, -100, -100)
pyxirr.fv([0.05/12, 0.06/12], [10*12, 9*12], [-100, -200], -100)
# feed numpy array
import numpy as np
rates = np.array([0.05, 0.06, 0.07])/12
pyxirr.fv(rates, 10*12, -100, -100)
# feed any iterable!
pyxirr.fv(
np.linspace(0.01, 0.2, 10),
(x + 1 for x in range(10)),
range(-100, -1100, -100),
tuple(range(-100, -200, -10))
)
# 2d, 3d, 4d, and more!
rates = [[[[[[0.01], [0.02]]]]]]
pyxirr.fv(rates, 10*12, -100, -100)
Running tests with pyo3 is a bit tricky. In short, you need to compile your tests without extension-module feature to avoid linking errors.
See the following issues for the details: #341, #771.
If you are using pyenv, make sure you have the shared library installed (check for ${PYENV_ROOT}/versions/<version>/lib/libpython3.so file).