-
-
Notifications
You must be signed in to change notification settings - Fork 82
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a518eac
BENCH: NPV: Benchmark 2d broadcasting
Kai-Striega 3ea3a6e
TST: NPV: Add tests for NPV
Kai-Striega f1c2e54
ENH: NPV: Support "gufunc" like behaviour
Kai-Striega 6561c6d
ENH: NPV: Move private array conversion functions to top of file
Kai-Striega 553647c
BENCH: NPV: Reduce benchmark size
Kai-Striega 2d08783
ENH: NPV: Make hot path for native types
Kai-Striega 8c723d2
ENH: NPV: Parallelize native hot-path
Kai-Striega f58c8df
DOC: NPV: Document new "broadcasting" behaviour
Kai-Striega e99ed93
REV: Remove Python 3.12 dependency
Kai-Striega 4d329ac
MAINT: Make linting happy and add numba to poetry
Kai-Striega b126521
MAINT: Refactor output array shape creation into own function
Kai-Striega 04af33f
DOC: NPV: Update documentation to support array arguments
Kai-Striega b8592cf
MAINT: NPV: Remove typed constants
Kai-Striega 4afacd4
BENCH: NPV: Actually create decimal arrays in benchmarks
Kai-Striega 0db86ec
DOC: NPV: Document with decimal.Decimal
Kai-Striega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,55 @@ | ||
from decimal import Decimal | ||
|
||
import numpy as np | ||
|
||
import numpy_financial as npf | ||
|
||
|
||
class Npv1DCashflow: | ||
|
||
param_names = ["cashflow_length"] | ||
params = [ | ||
(1, 10, 100, 1000), | ||
] | ||
|
||
def __init__(self): | ||
self.cashflows = None | ||
def _to_decimal_array_1d(array): | ||
return np.array([Decimal(x) for x in array.tolist()]) | ||
|
||
def setup(self, cashflow_length): | ||
rng = np.random.default_rng(0) | ||
self.cashflows = rng.standard_normal(cashflow_length) | ||
|
||
def time_1d_cashflow(self, cashflow_length): | ||
npf.npv(0.08, self.cashflows) | ||
def _to_decimal_array_2d(array): | ||
decimals = [Decimal(x) for row in array.tolist() for x in row] | ||
return np.array(decimals).reshape(array.shape) | ||
|
||
|
||
class Npv2DCashflows: | ||
class Npv2D: | ||
|
||
param_names = ["n_cashflows", "cashflow_lengths"] | ||
param_names = ["n_cashflows", "cashflow_lengths", "rates_lengths"] | ||
params = [ | ||
(1, 10, 100, 1000), | ||
(1, 10, 100, 1000), | ||
(1, 10, 100), | ||
(1, 10, 100), | ||
(1, 10, 100), | ||
] | ||
|
||
def __init__(self): | ||
self.rates_decimal = None | ||
self.rates = None | ||
self.cashflows_decimal = None | ||
self.cashflows = None | ||
|
||
def setup(self, n_cashflows, cashflow_lengths): | ||
def setup(self, n_cashflows, cashflow_lengths, rates_lengths): | ||
rng = np.random.default_rng(0) | ||
self.cashflows = rng.standard_normal((n_cashflows, cashflow_lengths)) | ||
cf_shape = (n_cashflows, cashflow_lengths) | ||
self.cashflows = rng.standard_normal(cf_shape) | ||
self.rates = rng.standard_normal(rates_lengths) | ||
self.cashflows_decimal = _to_decimal_array_2d(self.cashflows) | ||
self.rates_decimal = _to_decimal_array_1d(self.rates) | ||
|
||
def time_broadcast(self, n_cashflows, cashflow_lengths, rates_lengths): | ||
npf.npv(self.rates, self.cashflows) | ||
|
||
def time_for_loop(self, n_cashflows, cashflow_lengths, rates_lengths): | ||
for rate in self.rates: | ||
for cashflow in self.cashflows: | ||
npf.npv(rate, cashflow) | ||
|
||
def time_broadcast_decimal(self, n_cashflows, cashflow_lengths, rates_lengths): | ||
npf.npv(self.rates_decimal, self.cashflows_decimal) | ||
|
||
def time_for_loop_decimal(self, n_cashflows, cashflow_lengths, rates_lengths): | ||
for rate in self.rates_decimal: | ||
for cashflow in self.cashflows_decimal: | ||
npf.npv(rate, cashflow) | ||
|
||
def time_2d_cashflow(self, n_cashflows, cashflow_lengths): | ||
npf.npv(0.08, self.cashflows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.