Created
January 6, 2017 00:04
-
-
Save mpwoz/d07c72e3d7133514789bb38d9fd0f614 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" Calculate compounding interest on recurring expenses """ | |
import locale | |
locale.setlocale(locale.LC_ALL, "en_US") | |
# User configured parameters | |
years = 15 | |
interest = .05 | |
# Frequency names. Number of these per year | |
DAILY = 365.25 | |
WORKDAILY = DAILY * (5.0/7) | |
WEEKLY = 52.0 | |
MONTHLY = 12.0 | |
YEARLY = 1.0 | |
# List of (Label, Cost, Frequency) | |
payments = [ | |
("drinks", 4, DAILY), | |
("lunches", 15, WORKDAILY), | |
("yogas", 70, MONTHLY), | |
("dinners", 50, WEEKLY), | |
("cars", 20000, YEARLY/5), | |
] | |
# Annuity formula: http://www.cs.ucr.edu/~ehwang/interest.html | |
# pmt=payment per period, i=interest per period, n=no. of periods | |
def calc(pmt, i, n): | |
return pmt*(i+1)*((((1+i)**n)-1)/i) | |
def fmt(money): | |
return locale.currency(money, grouping=True) | |
def print_cost(label, cost, periods_in_year): | |
periods = periods_in_year * years | |
total = calc(cost, interest / periods_in_year, periods) | |
print("{0} x {1} {2} cost \t{3}".format( | |
int(periods * 10) / 10.0, | |
fmt(cost), | |
label, | |
fmt(total))) | |
return total | |
def main(): | |
total = 0 | |
print("For {0} years at {1}% return per year:".format(years, interest*100)) | |
for payment in payments: | |
total = total + print_cost(*payment) | |
print("For a total of {0}".format(fmt(total))) | |
if __name__ == '__main__': | |
main(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment