Skip to content

Commit

Permalink
Rename variables using _
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreyas Srinivasan committed Feb 11, 2022
1 parent 277668a commit 128db9f
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/fixedincome/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import scipy.optimize


def npv(rate : float, values : np.ndarray) -> float:
def npv(rate_ : float, values : np.ndarray) -> float:
"""
Calculates the Net Present Value (NPV) of a series of cash flows.
Parameters
----------
rate : float
rate_ : float
The (simple) discount rate over the length of one period.
values : np.ndarray
Correspond to payments and income per period, in order.
Expand All @@ -26,7 +26,7 @@ def npv(rate : float, values : np.ndarray) -> float:
The NPV of the given series of cash flows, discounted at the given rate.
"""

weights = np.fromfunction(function=lambda i: 1/(1 + rate)**(i + 1), shape=(values.size,), dtype=int)
weights = np.fromfunction(function=lambda i: 1/(1 + rate_)**(i + 1), shape=(values.size,), dtype=int)
return np.dot(values, weights)


Expand All @@ -50,7 +50,7 @@ def irr(values: np.ndarray, guess : float = 0.1) -> float:
return scipy.optimize.newton(func=npv, x0=guess, args=(values,), tol=0.0000001, maxiter=20)


def rate(nper : int, pmt : float, pv : float, fv : float = 0, guess : float = 0.1) -> float:
def rate(nper : int, pmt : float, pv_ : float, fv_ : float = 0, guess : float = 0.1) -> float:
"""
Calculates the interest rate per period of an annuity.
Expand All @@ -60,9 +60,9 @@ def rate(nper : int, pmt : float, pv : float, fv : float = 0, guess : float = 0.
The total number of payment periods in an annuity.
pmt : float
The constant payment amount made each period.
pv : float
pv_ : float
The present value (i.e., total amount that a series of payments is worth now) of the annuity.
fv : float [optional]
fv_ : float [optional]
The future value (i.e., cash balance to be attained after the last payment is made) of the annuity.
guess : float [optional]
An initial guess for the interest rate.
Expand All @@ -73,27 +73,27 @@ def rate(nper : int, pmt : float, pv : float, fv : float = 0, guess : float = 0.
The interest rate per period of the annuity.
"""

cash_flows = np.array([pv] + [pmt] * nper, dtype=float)
cash_flows[-1] += fv
cash_flows = np.array([pv_] + [pmt] * nper, dtype=float)
cash_flows[-1] += fv_

return irr(values=cash_flows, guess=guess)


def pv(rate : float, nper : int, pmt : float, fv : float = 0, payment_type : int = 0) -> float:
def pv(rate_ : float, nper : int, pmt : float, fv_ : float = 0, type_ : int = 0) -> float:
"""
Calculates the present value of a loan based on a constant interest rate.
Parameters
----------
rate : float
rate_ : float
The interest rate per period.
nper : int
The total number of payment periods in an annuity.
pmt : float
The constant payment amount made each period.
fv : float [optional]
fv_ : float [optional]
The future value (i.e., cash balance to be attained after the last payment is made) of the annuity.
payment_type : int [optional]
type_ : int [optional]
Indicates when payments are due.
- 0 [default] : at the end of the period.
- 1 : at the beginning of the period
Expand All @@ -105,28 +105,28 @@ def pv(rate : float, nper : int, pmt : float, fv : float = 0, payment_type : int
"""

if rate != 0:
pv = -(pmt*(1 + rate*payment_type)*(((1 + rate)**nper - 1)/rate) + fv)/((1 + rate)**nper)
_pv = -(pmt*(1 + rate_*type_)*(((1 + rate_)**nper - 1)/rate_) + fv_)/((1 + rate_)**nper)
else:
pv = -(pmt*nper + fv)
_pv = -(pmt*nper + fv_)

return pv
return _pv


def fv(rate : float, nper : int, pmt : float, pv : float = 0, payment_type : int = 0) -> float:
def fv(rate_ : float, nper : int, pmt : float, pv_ : float = 0, type_ : int = 0) -> float:
"""
Calculates the future value of a loan based on a constant interest rate.
Parameters
----------
rate : float
rate_ : float
The interest rate per period.
nper : int
The total number of payment periods in an annuity.
pmt : float
The constant payment amount made each period.
pv : float [optional]
pv_ : float [optional]
The present value of the annuity.
payment_type : int [optional]
type_ : int [optional]
Indicates when payments are due.
- 0 [default] : at the end of the period.
- 1 : at the beginning of the period
Expand All @@ -138,8 +138,8 @@ def fv(rate : float, nper : int, pmt : float, pv : float = 0, payment_type : int
"""

if rate != 0:
fv = -(pv*(1 + rate)**nper + pmt*(1 + rate*payment_type)*(((1 + rate)**nper - 1)/rate))
_fv = -(pv_*(1 + rate_)**nper + pmt*(1 + rate_*type_)*(((1 + rate_)**nper - 1)/rate_))
else:
fv = -(pmt*nper + pv)
_fv = -(pmt*nper + pv_)

return fv
return _fv

0 comments on commit 128db9f

Please sign in to comment.