Skip to main content

Rust-powered collection of financial functions for Python.

Project description

rust-lang.org License pypi versions

PyXIRR

Rust-powered collection of financial functions.

PyXIRR stands for "Python XIRR" (for historical reasons), but contains many other financial functions such as IRR, FV, NPV, etc.

Features:

  • correct
  • supports different day count conventions (e.g. ACT/360, 30E/360, etc.)
  • works with different input data types (iterators, numpy arrays, pandas DataFrames)
  • no external dependencies
  • type annotations
  • blazingly fast

Installation

pip install pyxirr

WASM wheels for pyodide are also available, but unfortunately are not supported by PyPI. You can find them on the GitHub Releases page.

Benchmarks

Rust implementation has been tested against existing xirr package (uses scipy.optimize under the hood) and the implementation from the Stack Overflow (pure python).

bench

PyXIRR is much faster than the other implementations.

Powered by github-action-benchmark and plotly.js.

Live benchmarks are hosted on Github Pages.

Example

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:

  1. It looks for positive result around 0.1 (the same as Excel with the default guess=0.1).
  2. 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}")

More Examples

Numpy and Pandas

import numpy as np
import pandas as pd

# feed numpy array
xirr(np.array([dates, amounts]))
xirr(np.array(dates), np.array(amounts))

# feed DataFrame (columns names doesn't matter; ordering matters)
xirr(pd.DataFrame({"a": dates, "b": amounts}))

# feed Series with DatetimeIndex
xirr(pd.Series(amounts, index=pd.to_datetime(dates)))

# bonus: apply xirr to a DataFrame with DatetimeIndex:
df = pd.DataFrame(
    index=pd.date_range("2021", "2022", freq="MS", inclusive="left"),
    data={
        "one": [-100] + [20] * 11,
        "two": [-80] + [19] * 11,
    },
)
df.apply(xirr)  # Series(index=["one", "two"], data=[5.09623547168478, 8.780801977141174])

Day count conventions

Check out the available options on the docs/day-count-conventions.

from pyxirr import DayCount

xirr(dates, amounts, day_count=DayCount.ACT_360)

# parse day count from string
xirr(dates, amounts, day_count="30E/360")

Private equity performance metrics

from pyxirr import pe

pe.pme_plus([-20, 15, 0], index=[100, 115, 130], nav=20)

pe.direct_alpha([-20, 15, 0], index=[100, 115, 130], nav=20)

Docs

Other financial functions

import pyxirr

# Future Value
pyxirr.fv(0.05/12, 10*12, -100, -100)

# Net Present Value
pyxirr.npv(0, [-40_000, 5_000, 8_000, 12_000, 30_000])

# IRR
pyxirr.irr([-100, 39, 59, 55, 20])

# ... and more! Check out the docs.

Docs

Vectorization

PyXIRR supports numpy-like vectorization.

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)

API reference

See the docs

Roadmap

  • Implement all functions from numpy-financial
  • Improve docs, add more tests
  • Type hints
  • Vectorized versions of numpy-financial functions.
  • Compile library for rust/javascript/python

Development

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

$ PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install <version>

Install dev-requirements

$ pip install -r dev-requirements.txt

Building

$ maturin develop

Testing

$ LD_LIBRARY_PATH=${PYENV_ROOT}/versions/3.10.8/lib cargo test

Benchmarks

$ pip install -r bench-requirements.txt
$ LD_LIBRARY_PATH=${PYENV_ROOT}/versions/3.10.8/lib cargo +nightly bench

Building and distribution

This library uses maturin to build and distribute python wheels.

$ docker run --rm -v $(pwd):/io ghcr.io/pyo3/maturin build --release --manylinux 2010 --strip
$ maturin upload target/wheels/pyxirr-${version}*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyxirr-0.10.3.tar.gz (40.2 kB view hashes)

Uploaded Source

Built Distributions

pyxirr-0.10.3-cp312-none-win_amd64.whl (465.1 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

pyxirr-0.10.3-cp312-cp312-musllinux_1_2_x86_64.whl (691.2 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

pyxirr-0.10.3-cp312-cp312-musllinux_1_2_i686.whl (681.0 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ i686

pyxirr-0.10.3-cp312-cp312-musllinux_1_2_armv7l.whl (707.0 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARMv7l

pyxirr-0.10.3-cp312-cp312-musllinux_1_2_aarch64.whl (637.8 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

pyxirr-0.10.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (518.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pyxirr-0.10.3-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (863.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

pyxirr-0.10.3-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (611.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

pyxirr-0.10.3-cp312-cp312-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (503.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64

pyxirr-0.10.3-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (443.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

pyxirr-0.10.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (457.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

pyxirr-0.10.3-cp312-cp312-macosx_11_0_arm64.whl (431.8 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pyxirr-0.10.3-cp312-cp312-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (906.3 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

pyxirr-0.10.3-cp311-none-win_amd64.whl (460.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

pyxirr-0.10.3-cp311-cp311-musllinux_1_2_x86_64.whl (688.4 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

pyxirr-0.10.3-cp311-cp311-musllinux_1_2_i686.whl (678.4 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ i686

pyxirr-0.10.3-cp311-cp311-musllinux_1_2_armv7l.whl (704.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARMv7l

pyxirr-0.10.3-cp311-cp311-musllinux_1_2_aarch64.whl (636.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

pyxirr-0.10.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (515.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pyxirr-0.10.3-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (900.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

pyxirr-0.10.3-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (609.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

pyxirr-0.10.3-cp311-cp311-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (502.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64

pyxirr-0.10.3-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (441.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

pyxirr-0.10.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (455.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

pyxirr-0.10.3-cp311-cp311-macosx_11_0_arm64.whl (429.7 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pyxirr-0.10.3-cp311-cp311-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (904.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

pyxirr-0.10.3-cp310-none-win_amd64.whl (460.4 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pyxirr-0.10.3-cp310-cp310-musllinux_1_2_x86_64.whl (688.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

pyxirr-0.10.3-cp310-cp310-musllinux_1_2_i686.whl (678.4 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ i686

pyxirr-0.10.3-cp310-cp310-musllinux_1_2_armv7l.whl (704.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARMv7l

pyxirr-0.10.3-cp310-cp310-musllinux_1_2_aarch64.whl (636.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

pyxirr-0.10.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (515.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pyxirr-0.10.3-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (900.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

pyxirr-0.10.3-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (609.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

pyxirr-0.10.3-cp310-cp310-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (502.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64

pyxirr-0.10.3-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (441.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARMv7l

pyxirr-0.10.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (455.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pyxirr-0.10.3-cp310-cp310-macosx_11_0_arm64.whl (429.8 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pyxirr-0.10.3-cp310-cp310-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (904.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

pyxirr-0.10.3-cp39-none-win_amd64.whl (460.8 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pyxirr-0.10.3-cp39-cp39-musllinux_1_2_x86_64.whl (688.8 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

pyxirr-0.10.3-cp39-cp39-musllinux_1_2_i686.whl (678.8 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ i686

pyxirr-0.10.3-cp39-cp39-musllinux_1_2_armv7l.whl (705.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARMv7l

pyxirr-0.10.3-cp39-cp39-musllinux_1_2_aarch64.whl (636.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

pyxirr-0.10.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (516.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pyxirr-0.10.3-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (900.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

pyxirr-0.10.3-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (609.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

pyxirr-0.10.3-cp39-cp39-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (502.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64

pyxirr-0.10.3-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (441.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARMv7l

pyxirr-0.10.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (456.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pyxirr-0.10.3-cp39-cp39-macosx_11_0_arm64.whl (434.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pyxirr-0.10.3-cp39-cp39-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (903.3 kB view hashes)

Uploaded CPython 3.9 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

pyxirr-0.10.3-cp38-none-win_amd64.whl (458.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pyxirr-0.10.3-cp38-cp38-musllinux_1_2_x86_64.whl (686.9 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

pyxirr-0.10.3-cp38-cp38-musllinux_1_2_i686.whl (677.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ i686

pyxirr-0.10.3-cp38-cp38-musllinux_1_2_armv7l.whl (703.2 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ARMv7l

pyxirr-0.10.3-cp38-cp38-musllinux_1_2_aarch64.whl (635.1 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ARM64

pyxirr-0.10.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (514.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pyxirr-0.10.3-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (900.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

pyxirr-0.10.3-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (607.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

pyxirr-0.10.3-cp38-cp38-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (500.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64

pyxirr-0.10.3-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (439.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARMv7l

pyxirr-0.10.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (454.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pyxirr-0.10.3-cp38-cp38-macosx_11_0_arm64.whl (432.0 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pyxirr-0.10.3-cp38-cp38-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (900.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

pyxirr-0.10.3-cp37-none-win_amd64.whl (458.6 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

pyxirr-0.10.3-cp37-cp37m-musllinux_1_2_x86_64.whl (686.9 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ x86-64

pyxirr-0.10.3-cp37-cp37m-musllinux_1_2_i686.whl (677.1 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ i686

pyxirr-0.10.3-cp37-cp37m-musllinux_1_2_armv7l.whl (703.2 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ ARMv7l

pyxirr-0.10.3-cp37-cp37m-musllinux_1_2_aarch64.whl (635.3 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ ARM64

pyxirr-0.10.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (514.1 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pyxirr-0.10.3-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl (900.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ s390x

pyxirr-0.10.3-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (607.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ppc64le

pyxirr-0.10.3-cp37-cp37m-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (500.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ppc64

pyxirr-0.10.3-cp37-cp37m-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (439.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARMv7l

pyxirr-0.10.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (454.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

pyxirr-0.10.3-cp37-cp37m-macosx_11_0_arm64.whl (432.0 kB view hashes)

Uploaded CPython 3.7m macOS 11.0+ ARM64

pyxirr-0.10.3-cp37-cp37m-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (900.2 kB view hashes)

Uploaded CPython 3.7m macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page