Rust-powered collection of financial functions.
Project description
PyXIRR
Rust-powered collection of financial functions.
Features:
- correct
- blazingly fast
- works with iterators
- works with unordered input
- 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!
Examples
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 iterators
xirr(iter(dates), (x for x in amounts))
# feed an iterable of tuples
xirr(zip(dates, amounts))
# feed a dictionary
xirr(dict(zip(dates, amounts)))
Numpy and Pandas support
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}))
API reference
Let's define type annotations:
# `None` if the calculation fails to converge or result is not finite (`inf` or `nan`)
FiniteOrNone = Optional[float]
DateLike = Union[datetime.date, datetime.datetime, numpy.datetime64, pandas.Timestamp]
Rate = float # rate as decimal, not percentage, normally between [-1, 1]
Guess = Optional[Rate]
Amount = Union[int, float, Decimal]
Payment = Tuple[DateLike, Amount]
DateLikeArray = Iterable[DateLike]
AmountArray = Iterable[Amount]
CashFlowTable = Iterable[Payment]
CashFlowDict = Dict[DateLike, Amount]
Exceptions
InvalidPaymentsError
. Occurs if either:- the amounts and dates arrays (
AmountArray
,DateLikeArray
) are of different lengths - the given arrays do not contain at least one negative and at least one positive value
- the amounts and dates arrays (
XIRR
# raises: InvalidPaymentsError
def xirr(
dates: Union[CashFlowTable, CashFlowDict, DateLikeArray],
amounts: Optional[AmountArray] = None,
guess: Guess = None,
) -> FiniteOrNone
XNPV
# raises: InvalidPaymentsError
def xnpv(
rate: Rate,
dates: Union[CashFlowTable, CashFlowDict, DateLikeArray],
amounts: Optional[AmountArray] = None,
) -> FiniteOrNone
IRR
Compute the Internal Rate of Return (IRR)
# raises: InvalidPaymentsError
def irr(amounts: AmountArray, guess: Guess = None) -> FiniteOrNone
NPV
Compute the Net Present Value.
# raises: InvalidPaymentsError
def npv(rate: Rate, amounts: AmountArray) -> FiniteOrNone
FV
Compute the future value.
def fv(
rate: Rate, # Rate of interest per period
nper: int, # Number of compounding periods
pmt: Amount, # Payment
pv: Amount, # Present value
pmt_at_begining: bool = False # When payments are due
) -> FiniteOrNone
Roadmap
- NumPy support
- XIRR
- XNPV
- NPV
- IRR
- FV
- PV
- MIRR
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.8.6/lib cargo test --no-default-features
Building and distribution
This library uses maturin to build and distribute python wheels.
$ docker run --rm -v $(pwd):/io konstin2/maturin build --release --manylinux 2010 --strip
$ maturin upload target/wheels/pyxirr-${version}*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pyxirr-0.4.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d17e9d5426d297595ae7236556460457f5f4efecc0f4d47996c3908827d44909 |
|
MD5 | 6ab229cfce64c3bba980249c487e37fd |
|
BLAKE2b-256 | 47944100a310e4c969f248c675237fd2f549bf787ec3a7dc7c36884593dbacf1 |
Hashes for pyxirr-0.4.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a01aae90172a4e54d584785d6948bd5eed9271756936876493a4d6c6850b348 |
|
MD5 | b6e4e47c538ecde75d678c2fde432ddd |
|
BLAKE2b-256 | 07db38372a930ad0097f27aa198f8167276e4d767770eb2d2c4bb3799cce5513 |
Hashes for pyxirr-0.4.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9c768beed7b49ea4b90c5ea0df390102f2abdac07a8b07d6ab004ce84a5c0b5 |
|
MD5 | 7c16d8fddaa980346b352c561fa68e7f |
|
BLAKE2b-256 | 36cb20a9574357bc699c3a166eb84e38e171d6587bd647bd70df00aa8e95e0ed |
Hashes for pyxirr-0.4.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fee53fcf88f430925a9fd2c041d6f5832d5703673d74d07914305cc865f786a |
|
MD5 | b1c2c3ac001ce68cc16fb9a56945e289 |
|
BLAKE2b-256 | b2f2dc54e93c9c49677bc812bc29e2be3265d2692a064ad8aa3e3456bdac2539 |
Hashes for pyxirr-0.4.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c8865c8418fe297ae2efc650552fdf8e124be5e0cd56b726178c2ee39cbd7ec |
|
MD5 | 1ed7e5830358da5e857d6a5365be094f |
|
BLAKE2b-256 | 0a22abc1165ae4bb204cf7beb2dd0dda06c87cb41be8aabd4743d261039b6040 |
Hashes for pyxirr-0.4.1-cp38-cp38-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 473450fed16d92f201fc12c6118405cbd446ce6e843107464d767c2e6a93c065 |
|
MD5 | 42783733c836aea4171144b39c40a5f1 |
|
BLAKE2b-256 | e108d83e615cb97b675b9f28e533aed86ee4be62b99a0ce907a17d83243b3349 |
Hashes for pyxirr-0.4.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecd15de9a7bf27995faa4f378bfaef567de96f04fc3673ee05ca93cda97c88d2 |
|
MD5 | bbab0c9d4db947bd2e8655cc5285dfc3 |
|
BLAKE2b-256 | cc463131a216acb575fe257b1eca6acdadfdb4c9c0b71880c1f7f68544084d36 |
Hashes for pyxirr-0.4.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7eca3f8a46f64234bd857282dcfee6ee32f5ddd833e62bddd09e6af9962885d |
|
MD5 | 4717a1fa73934d9c6545cc445163a57d |
|
BLAKE2b-256 | 86f408ca6ee2986616bda873d5aee8558a445ae73a79785049d815dcaab1cd79 |
Hashes for pyxirr-0.4.1-cp37-cp37m-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08fd81c999823cc9345dc511f9c79f62711836ad7d6ff8b84d5ab16c5b01777e |
|
MD5 | f5aa1520b1c454b8e8c311adb5e76de8 |
|
BLAKE2b-256 | 66061bb871cfd21c8263f3f9c91cdc568ab5d16ac944fffefd3c950fb0b02828 |
Hashes for pyxirr-0.4.1-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4652d47c6a404f170fd795c3a03e0e8591b87a08186e5e123d4caeba5837ffb |
|
MD5 | 5bb94458e6afe2e78a7a42345a673bfd |
|
BLAKE2b-256 | d10e96d7f0c29ef71b1bfd65515adf64c38d3ad92428e1eff52acdedc4f3fbef |
Hashes for pyxirr-0.4.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 826e77cf7d28d46cebe0b8a83765068123e8fcd465b4fd54f4aef0c43b12056f |
|
MD5 | 61268a443bbd277e08201b21aa1dd695 |
|
BLAKE2b-256 | 76dc442b66a899ab6769835274cf79b88ba71b6175ef25142b1bd0f00311f831 |
Hashes for pyxirr-0.4.1-cp36-cp36m-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd77c2784b1ea3e006eb70a4c3dabc9da72bda27bda39ad4960abb7510653a33 |
|
MD5 | ff9c3e8955d9854830a1113db438f338 |
|
BLAKE2b-256 | 6fdc395ecd77d4ecfe67f952ccd11df4d8f73f1dee22d82768c6390ed757b2c1 |