Skip to content

Instantly share code, notes, and snippets.

@ppsreejith
Created November 24, 2018 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppsreejith/08f0ba42517f5d4c7fdb5ec30dc71524 to your computer and use it in GitHub Desktop.
Save ppsreejith/08f0ba42517f5d4c7fdb5ec30dc71524 to your computer and use it in GitHub Desktop.
Spending calculator
#Everything is monthly
G_INITIAL_AMOUNT = 0.0
G_INVESTMENT = 1200.0
G_INTEREST_RATE = 1.07 ** (1.0/12.0)
G_INFLATION_RATE = 1.0228 ** (1.0/12.0)
G_CURRENT_SPENDING = 1000.0
def compound(principal, investment, rate, time):
"Compound interest calculator"
return float(principal) * float(rate ** time) + float(investment) * float(rate ** time - 1.0) / float(rate - 1.0)
def profit(initial_amount, investment, interest_rate, time):
"Profit calculator"
return compound(initial_amount, investment, interest_rate, time)
def loss(spending, inflation_rate, time):
"Loss calculator"
return compound(0, spending, inflation_rate, time)
def burn_time(current_amount, current_spending, investment, after_time):
"Calculates amount of burn time available"
amount = profit(current_amount, investment, G_INTEREST_RATE, after_time)
spending = current_spending * (G_INFLATION_RATE ** after_time)
time = 0.0
while True:
amount -= spending
if amount < 0.0:
break
amount *= G_INTEREST_RATE
spending *= G_INFLATION_RATE
time += 1.0
return max(time - 7.0, 0.0)
def expected_to_reach_amount(current_amount, expected_amount):
"Calculates expected time to reach a given amount"
time = 0.0
while True:
if current_amount >= expected_amount:
return time
time += 1.0
current_amount = compound(current_amount, G_INVESTMENT, G_INTEREST_RATE, 1.0)
def expected_to_reach_burn_time(current_amount, needed_burn_time):
"Calculates expected time to reach burn"
after_time = 1.0
while True:
calculated_burn_time = burn_time(current_amount, G_CURRENT_SPENDING, G_INVESTMENT, after_time)
if calculated_burn_time >= needed_burn_time:
return after_time
after_time += 1.0
def print_wait_time_for_burn(required_time):
"Prints wait time to reach a given burn time in human readable format"
needed_work_time = expected_to_reach_burn_time(G_INITIAL_AMOUNT, required_time)
received_burn_time = burn_time(G_INITIAL_AMOUNT, G_CURRENT_SPENDING, G_INVESTMENT, needed_work_time)
print("You'll have %d months of burn time after %d months" % (received_burn_time, needed_work_time))
def iterate_wait_time_for_burn(required_burn_time):
"Iterates over and prints different working periods for different burn times"
for required_time in range(1, required_burn_time + 1):
print_wait_time_for_burn(float(required_time))
G_INITIAL_AMOUNT = 0.0
iterate_wait_time_for_burn(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment