Skip to content

Instantly share code, notes, and snippets.

@ppsreejith
Created September 10, 2019 08:08
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/3f8858b01d012ceeb2e5b45f6c749a70 to your computer and use it in GitHub Desktop.
Save ppsreejith/3f8858b01d012ceeb2e5b45f6c749a70 to your computer and use it in GitHub Desktop.
To calculate spending and burn rate over a large period of time
G_CONSTANT_AMOUNT = 400000.0 # Minimum bank balance kept or liquid cash
G_INITIAL_INVESTMENT = 150000.0 # Existing investments
G_MONTHLY_INVESTMENT = 70000.0 # How much you plan to invest monthly
G_RETURN_RATE = 1.11 ** (1.0/12.0) # compound annual return rate on investment (maybe last 5 years?)
G_INFLATION_RATE = 1.08 ** (1.0/12.0) # compound annual inflation rate (maybe last 10 years?)
G_CURRENT_SPENDING = 80000.0 # Current (and planned future) monthly spending
G_LEEWAY_MONTHS = 3.0 # Number of months you need to keep spare (maybe to look for a new job etc)
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 = G_CONSTANT_AMOUNT + profit(current_amount, investment, G_RETURN_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_RETURN_RATE
spending *= G_INFLATION_RATE
time += 1.0
return max(time - G_LEEWAY_MONTHS, 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_MONTHLY_INVESTMENT, G_RETURN_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_MONTHLY_INVESTMENT, after_time)
if calculated_burn_time >= needed_burn_time:
return after_time
after_time += 1.0
def get_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_INVESTMENT, required_time)
received_burn_time = burn_time(G_INITIAL_INVESTMENT, G_CURRENT_SPENDING, G_MONTHLY_INVESTMENT, needed_work_time)
total_amount = G_CONSTANT_AMOUNT + profit(G_INITIAL_INVESTMENT, G_MONTHLY_INVESTMENT, G_RETURN_RATE, needed_work_time)
return (needed_work_time, received_burn_time, total_amount)
def iterate_wait_time_for_burn(required_burn_time):
"Iterates over and prints different working periods for different burn times"
print("Time in months")
print("Wait time\tBurn time\tAmount")
calculated_times = []
for required_time in range(1, required_burn_time + 1):
calculated_times.append(get_wait_time_for_burn(float(required_time)))
used = set()
unique_calculated_times = [x for x in calculated_times if x not in used and (used.add(x) or True)]
for calculated_time in unique_calculated_times:
print("%d\t\t%d\t\t%d" % calculated_time)
iterate_wait_time_for_burn(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment