Skip to content

Instantly share code, notes, and snippets.

@mpwoz
Created January 6, 2017 00:04
Show Gist options
  • Save mpwoz/d07c72e3d7133514789bb38d9fd0f614 to your computer and use it in GitHub Desktop.
Save mpwoz/d07c72e3d7133514789bb38d9fd0f614 to your computer and use it in GitHub Desktop.
#!/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