Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: NPV: Support calculation for vectors of rates and cashflows #96

Merged
merged 15 commits into from
Dec 9, 2023

Conversation

Kai-Striega
Copy link
Member

NumPy-Financial now supports calculation of NPV for vectors of rates and cashflows e.g.

>>> rates = [0.00, 0.05, 0.10]
>>> cashflows = [[-4_000, 500, 800], [-5_000, 600, 900]]
>>> npf.npv(rates, cashflows)
array([[-2700.  , -3500.  ],
           [-2798.19, -3612.24],
           [-2884.3 , -3710.74]])

This was previously not supported. This behaviour was first implemented by naively looping through rates and values. As Python for loops are slow, numba is applied to accelerate the process. This has lead to a significant speedup, especially for native types.

There are some potential drawbacks to using numba:

  • It adds another dependency to our stack
  • That dependency is large (>100MB)
  • numba requires a runtime step to compile
  • We need to duplicate the functions for Decimal support

This was taking too long and needs to be reduced
This commit introduces a native hot path using numba.
This leads to an ~50x speed up on the `time_broadcast`
benchmark with 100x100x100 dimension
This leads to the following improvements on the `time_broadcast` benchmark
with dimension 100x100x100:

* raw Python: 197 ms
* numba only: 4.08 ms
* numba + prange 0.3 ms
The newest released numba version is 0.58.1. This version does not support Python 3.12.
Since we are using it we also cannot support Python 3.12. This should be added once
numba version 0.59 is released.
@Kai-Striega Kai-Striega self-assigned this Dec 9, 2023
@Kai-Striega Kai-Striega added enhancement New feature or request bench Related to benchmarking numpy-financial labels Dec 9, 2023
@Kai-Striega Kai-Striega added this to the 2.0 milestone Dec 9, 2023
These were originally added when the code was written as one function without numba. As numba
is now being used, and the functions are therefore seperated. This is no longer required and
makes the code more complex.
This fixes a bug where we weren't creating decimal arrays in the benchmark.
Instead of creating an array and setting the dtype we manually make a list of
Decimals and create an array from that list.
@@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are required to drop Python 3.12 as Numba 0.58 doesn't support it yet. Python 3.12 should be supported in the next release. This should be released in early 2024.

@Kai-Striega Kai-Striega merged commit db5ab0a into main Dec 9, 2023
19 checks passed
@Kai-Striega Kai-Striega deleted the feature/move-npv-to-numba-2 branch December 9, 2023 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bench Related to benchmarking numpy-financial enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant