Issue
Start with $1000.
Invest it for 10 years, compounded annually.
Return table with annual returns, formatted thus $xxx,xxx.00
Solve without writing new implementation preferred. Pythonic and concise with existing API should be possible, since this is one of the most common calculations on the planet (and probably other planets too).
Solution
There are a range of financial tools in NumPy: see the np.lib.financial
module.
Here, I think you want fv
. An example given in the documentation is the following:
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`
If any of the input values is an array, an array of values is returned. To compute the annual balances over 10 years (with the same assumptions as the example), you could write:
>>> np.fv(0.05/12, np.arange(0, 121, 10), -100, -100)
array([ 100. , 1123.20552614, 2189.85294226, 3301.78664596,
4460.92934179, 5669.28536592, 6928.94415193, 8242.08384379,
9610.97506218, 11037.98483075, 12525.58066907])
This doesn't have the $xxx,xxx.00 formatting you've specified, but it should be straightforward to adjust NumPy's print options to get something satisfactory.
Answered By - Alex Riley
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.