U C^f{ @sdZddlmZmZmZddlZddlmZddlZddl Z ddl m Z dZ eje jddZd d d d d dddddg Zdddddddddd ZddZd;ddZeeddd Zd?ddZeed@d d ZdAd!d"ZeedBd#d Zd$d%ZdCd&d'ZeedDd(d ZdEd)d*ZeedFd+dZd,d-Z dGd.d/Z!ee!dHd1dZ"d2d3Z#ee#d4dZ$d5d6Z%ee%d7dZ&d8d9Z'ee'd:dZ(dS)IaKSome simple financial calculations patterned after spreadsheet computations. There is some complexity in each function so that the functions behave like ufuncs with broadcasting and being able to be called with scalars or arrays (or other sequences). Functions support the :class:`decimal.Decimal` type unless otherwise stated. )divisionabsolute_importprint_functionN)Decimal) overridesznumpy.{name} is deprecated and will be removed from NumPy 1.20. Use numpy_financial.{name} instead (https://pypi.org/project/numpy-financial/).numpy)modulefvpmtnperipmtppmtpvrateirrnpvmirr) endbeginebrrZ beginningstartfinishc CsFt|tjr|Sz t|WSttfk r@dd|DYSXdS)NcSsg|] }t|qS) _when_to_num).0xrr6/tmp/pip-install-6_kvzl1k/numpy/numpy/lib/financial.py 1sz!_convert_when..) isinstancenpZndarrayrKeyError TypeError)whenrrr _convert_when)s   r%cCs$tjtjddtdd||||fS)Nr name stacklevelwarningswarn_depmsgformatDeprecationWarning)rr r rr$rrr_fv_dispatcher4s r1rcCslt|}ttj|||||g\}}}}}d||}t|dk|d|||d|}|||| S)a Compute the future value. .. deprecated:: 1.18 `fv` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Given: * a present value, `pv` * an interest `rate` compounded once per period, of which there are * `nper` total * a (fixed) payment, `pmt`, paid either * at the beginning (`when` = {'begin', 1}) or the end (`when` = {'end', 0}) of each period Return: the value at the end of the `nper` periods Parameters ---------- rate : scalar or array_like of shape(M, ) Rate of interest as decimal (not per cent) per period nper : scalar or array_like of shape(M, ) Number of compounding periods pmt : scalar or array_like of shape(M, ) Payment pv : scalar or array_like of shape(M, ) Present value when : {{'begin', 1}, {'end', 0}}, {string, int}, optional When payments are due ('begin' (1) or 'end' (0)). Defaults to {'end', 0}. Returns ------- out : ndarray Future values. If all input is scalar, returns a scalar float. If any input is array_like, returns future values for each input element. If multiple inputs are array_like, they all must have the same shape. Notes ----- The future value is computed by solving the equation:: fv + pv*(1+rate)**nper + pmt*(1 + rate*when)/rate*((1 + rate)**nper - 1) == 0 or, when ``rate == 0``:: fv + pv + pmt * nper == 0 References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html .. [2] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12. Organization for the Advancement of Structured Information Standards (OASIS). Billerica, MA, USA. [ODT Document]. Available: http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula OpenDocument-formula-20090508.odt Examples -------- What is the future value after 10 years of saving $100 now, with an additional monthly savings of $100. Assume the interest rate is 5% (annually) compounded monthly? >>> np.fv(0.05/12, 10*12, -100, -100) 15692.928894335748 By convention, the negative sign represents cash flow out (i.e. money not available today). Thus, saving $100 a month at 5% annual interest leads to $15,692.93 available to spend in 10 years. If any input is array_like, returns an array of equal shape. Let's compare different interest rates from the example above. >>> a = np.array((0.05, 0.06, 0.07))/12 >>> np.fv(a, 10*12, -100, -100) array([ 15692.92889434, 16569.87435405, 17509.44688102]) # may vary rrr%mapr!asarraywhere)rr r rr$tempfactrrrr :s[   cCs$tjtjddtdd||||fS)Nr r&r(r)r+)rr rr r$rrr_pmt_dispatchers r8c Cst|}ttj|||||g\}}}}}d||}|dk}t|d|}t|dk|d|||d|}||| |S)a Compute the payment against loan principal plus interest. .. deprecated:: 1.18 `pmt` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Given: * a present value, `pv` (e.g., an amount borrowed) * a future value, `fv` (e.g., 0) * an interest `rate` compounded once per period, of which there are * `nper` total * and (optional) specification of whether payment is made at the beginning (`when` = {'begin', 1}) or the end (`when` = {'end', 0}) of each period Return: the (fixed) periodic payment. Parameters ---------- rate : array_like Rate of interest (per period) nper : array_like Number of compounding periods pv : array_like Present value fv : array_like, optional Future value (default = 0) when : {{'begin', 1}, {'end', 0}}, {string, int} When payments are due ('begin' (1) or 'end' (0)) Returns ------- out : ndarray Payment against loan plus interest. If all input is scalar, returns a scalar float. If any input is array_like, returns payment for each input element. If multiple inputs are array_like, they all must have the same shape. Notes ----- The payment is computed by solving the equation:: fv + pv*(1 + rate)**nper + pmt*(1 + rate*when)/rate*((1 + rate)**nper - 1) == 0 or, when ``rate == 0``:: fv + pv + pmt * nper == 0 for ``pmt``. Note that computing a monthly mortgage payment is only one use for this function. For example, pmt returns the periodic deposit one must make to achieve a specified future balance given an initial deposit, a fixed, periodically compounded interest rate, and the total number of periods. References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html .. [2] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12. Organization for the Advancement of Structured Information Standards (OASIS). Billerica, MA, USA. [ODT Document]. Available: http://www.oasis-open.org/committees/documents.php ?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt Examples -------- What is the monthly payment needed to pay off a $200,000 loan in 15 years at an annual interest rate of 7.5%? >>> np.pmt(0.075/12, 12*15, 200000) -1854.0247200054619 In order to pay-off (i.e., have a future-value of 0) the $200,000 obtained today, a monthly payment of $1,854.02 would be required. Note that this example illustrates usage of `fv` having a default value of 0. rr)r%r3r!arrayr5) rr rr r$r6maskZ masked_rater7rrrr s\   cCs$tjtjddtdd||||fS)Nr r&r(r)r+)rr rr r$rrr_nper_dispatcher s r;c Cst|}ttj|||||g\}}}}}d}tjdd8z|d|||}Wntk rjd}YnXW5QRX|r| ||S|| |d}t| |||td|}t|dk||SdS)a  Compute the number of periodic payments. .. deprecated:: 1.18 `nper` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. :class:`decimal.Decimal` type is not supported. Parameters ---------- rate : array_like Rate of interest (per period) pmt : array_like Payment pv : array_like Present value fv : array_like, optional Future value when : {{'begin', 1}, {'end', 0}}, {string, int}, optional When payments are due ('begin' (1) or 'end' (0)) Notes ----- The number of periods ``nper`` is computed by solving the equation:: fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate*((1+rate)**nper-1) = 0 but if ``rate = 0`` then:: fv + pv + pmt*nper = 0 References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html Examples -------- If you only had $150/month to pay towards the loan, how long would it take to pay-off a loan of $8,000 at 7% annual interest? >>> print(np.round(np.nper(0.07/12, -150, 8000), 5)) 64.07335 So, over 64 months would be required to pay off the loan. The same analysis could be done with several different interest rates and/or payments and/or total amounts to produce an entire table. >>> np.nper(*(np.ogrid[0.07/12: 0.08/12: 0.01/12, ... -150 : -99 : 50 , ... 8000 : 9001 : 1000])) array([[[ 64.07334877, 74.06368256], [108.07548412, 127.99022654]], [[ 66.12443902, 76.87897353], [114.70165583, 137.90124779]]]) Fraise)dividerTrN)r%r3r!r4ZerrstateFloatingPointErrorlogr5) rr rr r$Z use_zero_ratezABrrrr s? &cCs&tjtjddtdd|||||fS)Nr r&r(r)r+rperr rr r$rrr_ipmt_dispatcher`s rEcCst|}t||||||\}}}}}}t|||||}t||||||}z>> principal = 2500.00 The 'per' variable represents the periods of the loan. Remember that financial equations start the period count at 1! >>> per = np.arange(1*12) + 1 >>> ipmt = np.ipmt(0.0824/12, per, 1*12, principal) >>> ppmt = np.ppmt(0.0824/12, per, 1*12, principal) Each element of the sum of the 'ipmt' and 'ppmt' arrays should equal 'pmt'. >>> pmt = np.pmt(0.0824/12, 1*12, principal) >>> np.allclose(ipmt + ppmt, pmt) True >>> fmt = '{0:2d} {1:8.2f} {2:8.2f} {3:8.2f}' >>> for payment in per: ... index = payment - 1 ... principal = principal + ppmt[index] ... print(fmt.format(payment, ppmt[index], ipmt[index], principal)) 1 -200.58 -17.17 2299.42 2 -201.96 -15.79 2097.46 3 -203.35 -14.40 1894.11 4 -204.74 -13.01 1689.37 5 -206.15 -11.60 1483.22 6 -207.56 -10.18 1275.66 7 -208.99 -8.76 1066.67 8 -210.42 -7.32 856.25 9 -211.87 -5.88 644.38 10 -213.32 -4.42 431.05 11 -214.79 -2.96 216.26 12 -216.26 -1.49 -0.00 >>> interestpd = np.sum(ipmt) >>> np.round(interestpd, 2) -112.98 rr)r%r!Zbroadcast_arraysr _rblr5 logical_and IndexError)rrDr rr r$Z total_pmtr rrrr fs_ "cCst||d|||S)a1 This function is here to simply have a different name for the 'fv' function to not interfere with the 'fv' keyword argument within the 'ipmt' function. It is the 'remaining balance on loan' which might be useful as it's own function, but is easily calculated with the 'fv' function. r)r )rrDr rr$rrrrFsrFcCs&tjtjddtdd|||||fS)Nr r&r(r)r+rCrrr_ppmt_dispatchers rIcCs&t|||||}|t||||||S)a Compute the payment against loan principal. .. deprecated:: 1.18 `ppmt` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Parameters ---------- rate : array_like Rate of interest (per period) per : array_like, int Amount paid against the loan changes. The `per` is the period of interest. nper : array_like Number of compounding periods pv : array_like Present value fv : array_like, optional Future value when : {{'begin', 1}, {'end', 0}}, {string, int} When payments are due ('begin' (1) or 'end' (0)) See Also -------- pmt, pv, ipmt References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html )r r )rrDr rr r$totalrrrr s%cCs&tjtjddtdd|||t|fS)Nrr&r(r))r,r-r.r/r0r)rr r r r$rrr_pv_dispatcher s rKcCslt|}ttj|||||g\}}}}}d||}t|dk|d|||d|}||| |S)a Compute the present value. .. deprecated:: 1.18 `pv` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Given: * a future value, `fv` * an interest `rate` compounded once per period, of which there are * `nper` total * a (fixed) payment, `pmt`, paid either * at the beginning (`when` = {'begin', 1}) or the end (`when` = {'end', 0}) of each period Return: the value now Parameters ---------- rate : array_like Rate of interest (per period) nper : array_like Number of compounding periods pmt : array_like Payment fv : array_like, optional Future value when : {{'begin', 1}, {'end', 0}}, {string, int}, optional When payments are due ('begin' (1) or 'end' (0)) Returns ------- out : ndarray, float Present value of a series of payments or investments. Notes ----- The present value is computed by solving the equation:: fv + pv*(1 + rate)**nper + pmt*(1 + rate*when)/rate*((1 + rate)**nper - 1) = 0 or, when ``rate = 0``:: fv + pv + pmt * nper = 0 for `pv`, which is then returned. References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html .. [2] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12. Organization for the Advancement of Structured Information Standards (OASIS). Billerica, MA, USA. [ODT Document]. Available: http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula OpenDocument-formula-20090508.odt Examples -------- What is the present value (e.g., the initial investment) of an investment that needs to total $15692.93 after 10 years of saving $100 every month? Assume the interest rate is 5% (annually) compounded monthly. >>> np.pv(0.05/12, 10*12, -100, 15692.93) -100.00067131625819 By convention, the negative sign represents cash flow out (i.e., money not available today). Thus, to end up with $15,692.93 in 10 years saving $100 a month at 5% annual interest, one's initial deposit should also be $100. If any input is array_like, ``pv`` returns an array of equal shape. Let's compare different interest rates in the example above: >>> a = np.array((0.05, 0.04, 0.03))/12 >>> np.pv(a, 10*12, -100, 15692.93) array([ -100.00067132, -649.26771385, -1273.78633713]) # may vary So, to end up with the same $15692.93 under the same $100 per month "savings plan," for annual interest rates of 4% and 3%, one would need initial investments of $649.27 and $1273.79, respectively. rrr2)rr r r r$r6r7rrrrs _  &cCs|d|}|d|d}|||||d||d||||||d||d|d|||||d|||d||S)Nrr)rnprywt1t2rrr _g_div_gp{s &FrTcCs$tjtjddtdd||||fS)Nrr&r(r)r+)r r rr r$guesstolmaxiterrrr_rate_dispatchers rXdcCst|}t|trtnt}|dkr*|d}|dkr:|d}ttj|||||g\}}}}}|} d} d} | |kr| s| t| |||||} t| | } t | |k} | d7} | } qf| stj | S| SdS)a Compute the rate of interest per period. .. deprecated:: 1.18 `rate` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Parameters ---------- nper : array_like Number of compounding periods pmt : array_like Payment pv : array_like Present value fv : array_like Future value when : {{'begin', 1}, {'end', 0}}, {string, int}, optional When payments are due ('begin' (1) or 'end' (0)) guess : Number, optional Starting guess for solving the rate of interest, default 0.1 tol : Number, optional Required tolerance for the solution, default 1e-6 maxiter : int, optional Maximum iterations in finding the solution Notes ----- The rate of interest is computed by iteratively solving the (non-linear) equation:: fv + pv*(1+rate)**nper + pmt*(1+rate*when)/rate * ((1+rate)**nper - 1) = 0 for ``rate``. References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html .. [2] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May). Open Document Format for Office Applications (OpenDocument)v1.2, Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version, Pre-Draft 12. Organization for the Advancement of Structured Information Standards (OASIS). Billerica, MA, USA. [ODT Document]. Available: http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula OpenDocument-formula-20090508.odt Nz0.1z1e-6rFr) r%r rfloatr3r!r4rTabsallnan)r r rr r$rUrVrWZ default_typeZrniteratorcloseZrnp1Zdiffrrrrs&5    cCstjtjddtdd|fS)Nrr&r(r)r+)valuesrrr_irr_dispatchers racCsft|ddd}|jdk|jdk@}|s6tjS||j}d|d}|tt|}|S)a Return the Internal Rate of Return (IRR). .. deprecated:: 1.18 `irr` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. This is the "average" periodically compounded rate of return that gives a net present value of 0.0; for a more complete explanation, see Notes below. :class:`decimal.Decimal` type is not supported. Parameters ---------- values : array_like, shape(N,) Input cash flows per time period. By convention, net "deposits" are negative and net "withdrawals" are positive. Thus, for example, at least the first element of `values`, which represents the initial investment, will typically be negative. Returns ------- out : float Internal Rate of Return for periodic input values. Notes ----- The IRR is perhaps best understood through an example (illustrated using np.irr in the Examples section below). Suppose one invests 100 units and then makes the following withdrawals at regular (fixed) intervals: 39, 59, 55, 20. Assuming the ending value is 0, one's 100 unit investment yields 173 units; however, due to the combination of compounding and the periodic withdrawals, the "average" rate of return is neither simply 0.73/4 nor (1.73)^0.25-1. Rather, it is the solution (for :math:`r`) of the equation: .. math:: -100 + \frac{39}{1+r} + \frac{59}{(1+r)^2} + \frac{55}{(1+r)^3} + \frac{20}{(1+r)^4} = 0 In general, for `values` :math:`= [v_0, v_1, ... v_M]`, irr is the solution of the equation: [2]_ .. math:: \sum_{t=0}^M{\frac{v_t}{(1+irr)^{t}}} = 0 References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html .. [2] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed., Addison-Wesley, 2003, pg. 348. Examples -------- >>> round(np.irr([-100, 39, 59, 55, 20]), 5) 0.28095 >>> round(np.irr([-100, 0, 0, 74]), 5) -0.0955 >>> round(np.irr([-100, 100, 0, -7]), 5) -0.0833 >>> round(np.irr([-100, 100, 0, 7]), 5) 0.06206 >>> round(np.irr([-5, 10.5, 1, -8, 1]), 5) 0.0886 Nrr) r!rootsimagrealanyr]itemZargminr[)r`resr:rrrrrsK  cCstjtjddtdd|fS)Nrr&r(r)r+rr`rrr_npv_dispatcher@s rjcCs.t|}|d|tdt|jddS)aG Returns the NPV (Net Present Value) of a cash flow series. .. deprecated:: 1.18 `npv` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Parameters ---------- rate : scalar The discount rate. values : array_like, shape(M, ) The values of the time series of cash flows. The (fixed) time interval between cash flow "events" must be the same as that for which `rate` is given (i.e., if `rate` is per year, then precisely a year is understood to elapse between each cash flow event). By convention, investments or "deposits" are negative, income or "withdrawals" are positive; `values` must begin with the initial investment, thus `values[0]` will typically be negative. Returns ------- out : float The NPV of the input cash flow series `values` at the discount `rate`. Warnings -------- ``npv`` considers a series of cashflows starting in the present (t = 0). NPV can also be defined with a series of future cashflows, paid at the end, rather than the start, of each period. If future cashflows are used, the first cashflow `values[0]` must be zeroed and added to the net present value of the future cashflows. This is demonstrated in the examples. Notes ----- Returns the result of: [2]_ .. math :: \sum_{t=0}^{M-1}{\frac{values_t}{(1+rate)^{t}}} References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html .. [2] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed., Addison-Wesley, 2003, pg. 346. Examples -------- Consider a potential project with an initial investment of $40 000 and projected cashflows of $5 000, $8 000, $12 000 and $30 000 at the end of each period discounted at a rate of 8% per period. To find the project's net present value: >>> rate, cashflows = 0.08, [-40_000, 5_000, 8_000, 12_000, 30_000] >>> np.npv(rate, cashflows).round(5) 3065.22267 It may be preferable to split the projected cashflow into an initial investment and expected future cashflows. In this case, the value of the initial cashflow is zero and the initial investment is later added to the future cashflows net present value: >>> initial_cashflow = cashflows[0] >>> cashflows[0] = 0 >>> np.round(np.npv(rate, cashflows) + initial_cashflow, 5) 3065.22267 rr)Zaxis)r!r4ZarangelensumrirrrrFsJ cCstjtjddtdd|fS)Nrr&r(r)r+)r` finance_rate reinvest_raterrr_mirr_dispatchers rocCst|}|j}t|tr"t|}|dk}|dk}|rB|sHtjStt|||}tt|||}||d|dd|dS)ah Modified internal rate of return. .. deprecated:: 1.18 `mirr` is deprecated; for details, see NEP 32 [1]_. Use the corresponding function in the numpy-financial library, https://pypi.org/project/numpy-financial. Parameters ---------- values : array_like Cash flows (must contain at least one positive and one negative value) or nan is returned. The first value is considered a sunk cost at time zero. finance_rate : scalar Interest rate paid on the cash flows reinvest_rate : scalar Interest rate received on the cash flows upon reinvestment Returns ------- out : float Modified internal rate of return References ---------- .. [1] NumPy Enhancement Proposal (NEP) 32, https://numpy.org/neps/nep-0032-remove-financial-functions.html rr) r!r4sizer rrfr]r[r)r`rmrnrNposnegZnumerZdenomrrrrs  )N)r)NN)rr)NN)rr)NN)rr)NN)rr)NN)rr)NNNN)rNNrY))__doc__ __future__rrrr,decimalr functoolsrr!Z numpy.corerr.partialZarray_function_dispatch__all__rr%r1r r8r r;r rEr rFrIr rKrrTrXrrarrjrrorrrrrsx     b  e  P  k  (  i  Q V M