Here’s a self‐contained Python script that lets you plug in all the key variables and then compares:
A) Your net profit (home equity) if you buy a house with a 30-year mortgage, pay taxes & insurance, then sell after N years
B) Your portfolio value if you rent instead and invest the monthly savings (rent minus your home carrying cost) into the S&P 500
Save this as buy_vs_rent.py and run it from your terminal:
import numpy as np
def monthly_payment(principal, annual_rate, years):
"""
Compute the fixed monthly mortgage payment using the annuity formula.
"""
r = annual_rate / 12
n = years * 12
return principal * (r * (1 + r)**n) / ((1 + r)**n - 1)
def simulate_buy(
purchase_price,
down_payment_pct,
annual_interest,
mortgage_years,
annual_tax_rate,
annual_insurance,
home_appreciation_rate,
selling_years,
closing_cost_sell_pct
):
# 1) Loan and down payment
down_payment = purchase_price * down_payment_pct
loan_amount = purchase_price - down_payment
m_payment = monthly_payment(loan_amount, annual_interest, mortgage_years)
# 2) Monthly taxes & insurance
tax_monthly = purchase_price * annual_tax_rate / 12
ins_monthly = annual_insurance / 12
carrying_cost = m_payment + tax_monthly + ins_monthly
# 3) Amortization schedule up to selling month
balance = loan_amount
months = int(selling_years * 12)
for _ in range(months):
interest = balance * (annual_interest / 12)
principal = m_payment - interest
balance -= principal
# 4) Sale price & net proceeds
sale_price = purchase_price * (1 + home_appreciation_rate) ** selling_years
sell_fees = sale_price * closing_cost_sell_pct
net_proceeds = sale_price - sell_fees - balance
# 5) Net profit = net_proceeds minus original down payment
profit = net_proceeds - down_payment
return profit, carrying_cost
def simulate_rent_and_invest(
rent_monthly,
rent_growth_rate,
invest_return,
months,
monthly_savings
):
"""
Simulate investing a fixed amount each month at a given annual return.
"""
balance = 0.0
r = invest_return / 12
for m in range(months):
balance = balance * (1 + r) + monthly_savings[m]
# escalate next month's rent-savings step outside
return balance
def main():
print("=== Buy vs Rent + Invest Comparison ===\n")
# --- Inputs ---
P = float(input("Purchase price ($): "))
dp_pct = float(input("Down payment (% as decimal, e.g. 0.20): "))
ir_mortgage = float(input("Mortgage annual rate (e.g. 0.035): "))
term_yrs = int(input("Mortgage term (years, e.g. 30): "))
tax_rate = float(input("Annual property tax rate (e.g. 0.012): "))
ins_annual = float(input("Annual insurance cost ($): "))
app_rate = float(input("Home appreciation rate (e.g. 0.03): "))
sell_after = float(input("Sell after N years (e.g. 10): "))
sell_costs_pct = float(input("Selling closing costs (% of sale as decimal): "))
rent0 = float(input("Current monthly rent ($): "))
rent_growth = float(input("Rent annual growth rate (e.g. 0.02): "))
invest_ret = float(input("Invest return p.a. (e.g. 0.07): "))
# --- BUY Scenario ---
buy_profit, carry_cost = simulate_buy(
P, dp_pct, ir_mortgage, term_yrs,
tax_rate, ins_annual,
app_rate, sell_after,
sell_costs_pct
)
# --- RENT + INVEST Scenario ---
months = int(sell_after * 12)
# calculate monthly savings array: rent_t - carry_cost
monthly_savings = []
rent = rent0
for m in range(months):
saving = max(0.0, rent - carry_cost)
monthly_savings.append(saving)
# grow rent for next month
if (m+1) % 12 == 0:
rent *= (1 + rent_growth)
invest_bal = simulate_rent_and_invest(
rent0, rent_growth, invest_ret, months, monthly_savings
)
# --- Results ---
print("\n=== Results after {:.1f} years ===".format(sell_after))
print(f"Buy & Sell Profit (net of loan & costs): ${buy_profit:,.2f}")
print(f"Rent + Invest Balance: ${invest_bal:,.2f}")
if buy_profit > invest_bal:
print("--> Buying wins by ${:,.2f}".format(buy_profit - invest_bal))
else:
print("--> Renting & investing wins by ${:,.2f}".format(invest_bal - buy_profit))
if __name__ == "__main__":
main()
How it works step-by-step:
-
Inputs
You enter purchase-price, down‐payment %, mortgage rate/term, tax & insurance,
home appreciation, how many years you’ll sell after, closing costs,
current rent, rent growth, and S&P withdrawal return.
-
Buying side
- Computes your 30-year fixed mortgage payment.
- Adds monthly property-tax & insurance to get total carrying cost.
- Builds a mini amortization over your
sell_after years,
finds the outstanding balance, and sale proceeds net of commissions.
- Subtracts your down-payment to yield net profit.
-
Renting side
- For each month until sale, measures “savings” =
max(0, rent – carrying_cost).
- Invests that savings into a lump‐sum that compounds at your S&P return.
-
Compares final home profit vs your investment balance.
Feel free to tweak the assumptions (maintenance, HOA fees, closing costs at purchase, negative savings rules, etc.) to match your own scenario.