Skip to content

Commit 08a6f8d

Browse files
DOC: Update the examples in the docstring to use numpy_financial.
1 parent 54dd869 commit 08a6f8d

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

numpy_financial/_financial.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,14 @@ def fv(rate, nper, pmt, pv, when='end'):
111111
112112
Examples
113113
--------
114+
>>> import numpy as np
115+
>>> import numpy_financial as nf
116+
114117
What is the future value after 10 years of saving $100 now, with
115118
an additional monthly savings of $100. Assume the interest rate is
116119
5% (annually) compounded monthly?
117120
118-
>>> np.fv(0.05/12, 10*12, -100, -100)
121+
>>> nf.fv(0.05/12, 10*12, -100, -100)
119122
15692.928894335748
120123
121124
By convention, the negative sign represents cash flow out (i.e. money not
@@ -126,7 +129,7 @@ def fv(rate, nper, pmt, pv, when='end'):
126129
compare different interest rates from the example above.
127130
128131
>>> a = np.array((0.05, 0.06, 0.07))/12
129-
>>> np.fv(a, 10*12, -100, -100)
132+
>>> nf.fv(a, 10*12, -100, -100)
130133
array([ 15692.92889434, 16569.87435405, 17509.44688102]) # may vary
131134
132135
"""
@@ -215,10 +218,12 @@ def pmt(rate, nper, pv, fv=0, when='end'):
215218
216219
Examples
217220
--------
221+
>>> import numpy_financial as nf
222+
218223
What is the monthly payment needed to pay off a $200,000 loan in 15
219224
years at an annual interest rate of 7.5%?
220225
221-
>>> np.pmt(0.075/12, 12*15, 200000)
226+
>>> nf.pmt(0.075/12, 12*15, 200000)
222227
-1854.0247200054619
223228
224229
In order to pay-off (i.e., have a future-value of 0) the $200,000 obtained
@@ -272,18 +277,21 @@ def nper(rate, pmt, pv, fv=0, when='end'):
272277
273278
Examples
274279
--------
280+
>>> import numpy as np
281+
>>> import numpy_financial as nf
282+
275283
If you only had $150/month to pay towards the loan, how long would it take
276284
to pay-off a loan of $8,000 at 7% annual interest?
277285
278-
>>> print(np.round(np.nper(0.07/12, -150, 8000), 5))
286+
>>> print(np.round(nf.nper(0.07/12, -150, 8000), 5))
279287
64.07335
280288
281289
So, over 64 months would be required to pay off the loan.
282290
283291
The same analysis could be done with several different interest rates
284292
and/or payments and/or total amounts to produce an entire table.
285293
286-
>>> np.nper(*(np.ogrid[0.07/12: 0.08/12: 0.01/12,
294+
>>> nf.nper(*(np.ogrid[0.07/12: 0.08/12: 0.01/12,
287295
... -150 : -99 : 50 ,
288296
... 8000 : 9001 : 1000]))
289297
array([[[ 64.07334877, 74.06368256],
@@ -356,6 +364,9 @@ def ipmt(rate, per, nper, pv, fv=0, when='end'):
356364
357365
Examples
358366
--------
367+
>>> import numpy as np
368+
>>> import numpy_financial as nf
369+
359370
What is the amortization schedule for a 1 year loan of $2500 at
360371
8.24% interest per year compounded monthly?
361372
@@ -365,13 +376,13 @@ def ipmt(rate, per, nper, pv, fv=0, when='end'):
365376
financial equations start the period count at 1!
366377
367378
>>> per = np.arange(1*12) + 1
368-
>>> ipmt = np.ipmt(0.0824/12, per, 1*12, principal)
369-
>>> ppmt = np.ppmt(0.0824/12, per, 1*12, principal)
379+
>>> ipmt = nf.ipmt(0.0824/12, per, 1*12, principal)
380+
>>> ppmt = nf.ppmt(0.0824/12, per, 1*12, principal)
370381
371382
Each element of the sum of the 'ipmt' and 'ppmt' arrays should equal
372383
'pmt'.
373384
374-
>>> pmt = np.pmt(0.0824/12, 1*12, principal)
385+
>>> pmt = nf.pmt(0.0824/12, 1*12, principal)
375386
>>> np.allclose(ipmt + ppmt, pmt)
376387
True
377388
@@ -521,12 +532,15 @@ def pv(rate, nper, pmt, fv=0, when='end'):
521532
522533
Examples
523534
--------
535+
>>> import numpy as np
536+
>>> import numpy_financial as nf
537+
524538
What is the present value (e.g., the initial investment)
525539
of an investment that needs to total $15692.93
526540
after 10 years of saving $100 every month? Assume the
527541
interest rate is 5% (annually) compounded monthly.
528542
529-
>>> np.pv(0.05/12, 10*12, -100, 15692.93)
543+
>>> nf.pv(0.05/12, 10*12, -100, 15692.93)
530544
-100.00067131625819
531545
532546
By convention, the negative sign represents cash flow out
@@ -538,7 +552,7 @@ def pv(rate, nper, pmt, fv=0, when='end'):
538552
Let's compare different interest rates in the example above:
539553
540554
>>> a = np.array((0.05, 0.04, 0.03))/12
541-
>>> np.pv(a, 10*12, -100, 15692.93)
555+
>>> nf.pv(a, 10*12, -100, 15692.93)
542556
array([ -100.00067132, -649.26771385, -1273.78633713]) # may vary
543557
544558
So, to end up with the same $15692.93 under the same $100 per month
@@ -704,15 +718,17 @@ def irr(values):
704718
705719
Examples
706720
--------
707-
>>> round(np.irr([-100, 39, 59, 55, 20]), 5)
721+
>>> import numpy_financial as nf
722+
723+
>>> round(nf.irr([-100, 39, 59, 55, 20]), 5)
708724
0.28095
709-
>>> round(np.irr([-100, 0, 0, 74]), 5)
725+
>>> round(nf.irr([-100, 0, 0, 74]), 5)
710726
-0.0955
711-
>>> round(np.irr([-100, 100, 0, -7]), 5)
727+
>>> round(nf.irr([-100, 100, 0, -7]), 5)
712728
-0.0833
713-
>>> round(np.irr([-100, 100, 0, 7]), 5)
729+
>>> round(nf.irr([-100, 100, 0, 7]), 5)
714730
0.06206
715-
>>> round(np.irr([-5, 10.5, 1, -8, 1]), 5)
731+
>>> round(nf.irr([-5, 10.5, 1, -8, 1]), 5)
716732
0.0886
717733
718734
"""
@@ -783,13 +799,16 @@ def npv(rate, values):
783799
784800
Examples
785801
--------
802+
>>> import numpy as np
803+
>>> import numpy_financial as nf
804+
786805
Consider a potential project with an initial investment of $40 000 and
787806
projected cashflows of $5 000, $8 000, $12 000 and $30 000 at the end of
788807
each period discounted at a rate of 8% per period. To find the project's
789808
net present value:
790809
791810
>>> rate, cashflows = 0.08, [-40_000, 5_000, 8_000, 12_000, 30_000]
792-
>>> np.npv(rate, cashflows).round(5)
811+
>>> nf.npv(rate, cashflows).round(5)
793812
3065.22267
794813
795814
It may be preferable to split the projected cashflow into an initial
@@ -799,7 +818,7 @@ def npv(rate, values):
799818
800819
>>> initial_cashflow = cashflows[0]
801820
>>> cashflows[0] = 0
802-
>>> np.round(np.npv(rate, cashflows) + initial_cashflow, 5)
821+
>>> np.round(nf.npv(rate, cashflows) + initial_cashflow, 5)
803822
3065.22267
804823
805824
"""

0 commit comments

Comments
 (0)