Skip to content

Commit

Permalink
MAINT: NPV: Remove typed constants
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kai-Striega committed Dec 9, 2023
1 parent 04af33f commit b8592cf
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions numpy_financial/_financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,23 +857,23 @@ def irr(values, *, guess=None, tol=1e-12, maxiter=100, raise_exceptions=False):


@nb.njit(parallel=True)
def _npv_native(rates, values, out, zero, one):
def _npv_native(rates, values, out):
for i in nb.prange(rates.shape[0]):
for j in nb.prange(values.shape[0]):
acc = zero
acc = 0.0
for t in range(values.shape[1]):
acc += values[j, t] / ((one + rates[i]) ** t)
acc += values[j, t] / ((1.0 + rates[i]) ** t)
out[i, j] = acc


# We require ``forceobj=True`` here to support decimal.Decimal types
@nb.jit(forceobj=True)
def _npv_decimal(rates, values, out, zero, one):
def _npv_decimal(rates, values, out):
for i in range(rates.shape[0]):
for j in range(values.shape[0]):
acc = zero
acc = Decimal("0.0")
for t in range(values.shape[1]):
acc += values[j, t] / ((one + rates[i]) ** t)
acc += values[j, t] / ((Decimal("1.0") + rates[i]) ** t)
out[i, j] = acc


Expand Down Expand Up @@ -972,16 +972,14 @@ def npv(rate, values):
if dtype == Decimal:
rates = _to_decimal_array_1d(rates)
values = _to_decimal_array_2d(values)
zero = dtype("0.0")
one = dtype("1.0")

shape = _get_output_array_shape(rates, values)
out = np.empty(shape=shape, dtype=dtype)

if dtype == Decimal:
_npv_decimal(rates, values, out, zero, one)
_npv_decimal(rates, values, out)
else:
_npv_native(rates, values, out, zero, one)
_npv_native(rates, values, out)

return _return_ufunc_like(out)

Expand Down

0 comments on commit b8592cf

Please sign in to comment.