Skip to main content

Rust-powered collection of financial functions.

Project description

rust-lang.org License pypi

PyXIRR

Rust-powered collection of financial functions.

Features:

  • correct
  • blazingly fast
  • works with iterators
  • no external dependencies

Installation

pip install pyxirr

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

Implementation Sample size Execution time
pyxirr (Rust) 100 45.89 us
xirr (scipy) 100 790.76 us
pure Python 100 14.37 ms
pyxirr (Rust) 1000 404.03 us
xirr (scipy) 1000 3.47 ms
pure Python 1000 35.97 ms
pyxirr (Rust) 10000 3.58 ms
xirr (scipy) 10000 28.04 ms
pure Python 10000 24.23 s

PyXIRR is ~10-20x faster than other solutions!

Usage

xirr

Function signature:

# You have two options:
# 1. Two iterables for dates and amounts
# 2. Single iterable of tuples (date, amount)

DateLike = Union[datetime.date, datetime.datetime]
Amount = Union[int, float, Decimal]

def xirr(
    dates: Union[Iterable[DateLike], Iterable[Tuple[DateLike, Amount]]]
    amounts Optional[Iterable[Amount]] = None
    guess: Optional[float] = None
)

Example:

from datetime import date
from pyxirr import xirr

dates = [date(2020, 1, 1), date(2020, 2, 1)]
amounts = [-100, 125]

xirr(dates, amounts)

# list of tuples is also possible:
xirr(zip(dates, amounts))

xnpv

Function signature:

# similar to xirr: iterable of tuples or two iterables

def xnpv(
    rate: float,
    dates: Union[Iterable[DateLike], Iterable[Tuple[DateLike, Amount]]]
    amounts Optional[Iterable[Amount]] = None
)

Example:

from pyxirr import xnpv

xnpv(0.1, dates, amounts)
xnpv(0.1, zip(dates, amounts))

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

$ export PYO3_PYTHON_VERSION=3.8.6
$ PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install ${PYO3_PYTHON_VERSION}
# running tests
$ LD_LIBRARY_PATH=${PYENV_ROOT}/versions/${PYO3_PYTHON_VERSION}/lib cargo tests --no-default-features --features tests
# running benches
$ LD_LIBRARY_PATH=${PYENV_ROOT}/versions/${PYO3_PYTHON_VERSION}/lib cargo bench --no-default-features --features tests

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.2.1.tar.gz (155.4 kB view hashes)

Uploaded Source

Built Distributions

pyxirr-0.2.1-cp39-cp39-manylinux2010_x86_64.whl (172.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pyxirr-0.2.1-cp38-cp38-manylinux2010_x86_64.whl (172.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pyxirr-0.2.1-cp37-cp37m-manylinux2010_x86_64.whl (172.4 kB view hashes)

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

pyxirr-0.2.1-cp36-cp36m-manylinux2010_x86_64.whl (172.0 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

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