Sub topic 11
A simple rent-versus-buy model
This chapter assumes you can read basic Python and know three things from earlier chapters: how the monthly mortgage payment is calculated, why cash has an opportunity cost, and why selling a home costs money. Here you combine those pieces into one model that reports net wealth for each option at a chosen time horizon.
What the model compares
The model does not compare monthly payments. It compares what you would own if you chose each path and then stopped at the same date.
- Owner wealth is the sale price minus selling costs, minus the loan balance still owed.
- Renter wealth is an investment portfolio. It starts with the cash the buyer would have spent up front. Each month it also receives the difference between the owner's monthly cost and the rent.
Both paths start with the same cash and have the same monthly budget. The renter invests whatever the owner spends beyond the rent. If renting is more expensive in a given month, the difference comes out of the portfolio.
The inputs
Every number in the model is an assumption. Keep them as named parameters so you can change one at a time.
| Parameter | Default | Meaning |
|---|---|---|
| `price` | 400,000 | Purchase price of the home |
| `down_pct` | 0.20 | Share of the price paid in cash |
| `mort_rate` | 0.06 | Annual mortgage interest rate |
| `term` | 30 | Mortgage length in years |
| `appreciation` | 0.03 | Annual growth of the home's value |
| `invest_return` | 0.05 | Annual return on the renter's portfolio |
| `rent` | 2,000 | Starting monthly rent |
| `rent_growth` | 0.03 | Annual rent increase |
| `carry_rate` | 0.02 | Yearly property tax, insurance and maintenance as a share of home value |
| `sell_cost` | 0.06 | Share of the sale price lost to agent fees and closing costs on sale |
The mortgage payment
The simulation needs the payment from the amortization formula. With monthly rate r and n monthly payments, the payment is principal * r / (1 - (1 + r) ** -n). When the rate is zero, the payment is the principal divided by n.
def monthly_payment(principal, annual_rate, years):
r = annual_rate / 12
n = years * 12
if r == 0:
return principal / n
return principal * r / (1 - (1 + r) ** -n)
print(round(monthly_payment(300_000, 0.06, 30), 2)) # 1798.65The monthly loop below takes the interest part of each payment as balance * rate / 12. The rest of the payment reduces the balance. That is why the balance falls slowly in the early years.
The simulation
This is the core model. It runs one loop iteration per month.
def monthly_payment(principal, annual_rate, years):
r = annual_rate / 12
n = years * 12
if r == 0:
return principal / n
return principal * r / (1 - (1 + r) ** -n)
def simulate(years, price=400_000, down_pct=0.20, mort_rate=0.06, term=30,
appreciation=0.03, invest_return=0.05, rent=2_000,
rent_growth=0.03, carry_rate=0.02, sell_cost=0.06):
loan = price * (1 - down_pct)
pay = monthly_payment(loan, mort_rate, term)
balance = loan
portfolio = price * down_pct # the renter invests the cash the buyer puts down
for m in range(years * 12):
balance -= pay - balance * mort_rate / 12
value = price * (1 + appreciation) ** ((m + 1) / 12)
owner_cost = pay + value * carry_rate / 12 # tax, insurance, maintenance
rent_m = rent * (1 + rent_growth) ** (m // 12)
portfolio = portfolio * (1 + invest_return / 12) + (owner_cost - rent_m)
value = price * (1 + appreciation) ** years
owner_wealth = value * (1 - sell_cost) - balance
return owner_wealth, portfolio
for y in (3, 7, 15):
owner, renter = simulate(y)
print(y, "buy" if owner > renter else "rent")With these defaults, the output should show renting ahead at 3 years and buying ahead at 7 and 15 years. The exact crossover year depends on the assumptions. The next sections explain why.
Reading the loop line by line
balance -= pay - balance * mort_rate / 12subtracts only the principal part of the payment.valuecompounds the home price monthly. The exponent(m + 1) / 12converts months to years.owner_costadds a carrying cost to the mortgage payment. It is a percentage of the current home value, so it grows as the home appreciates.rent_mraises the rent once per year.m // 12is the number of whole years elapsed.- The portfolio first earns one month of return, then receives
owner_cost - rent_m. A negative value is a withdrawal.
What the model leaves out
The model is simple on purpose, and several costs are missing:
- Buyer closing costs. Loan fees, title charges and similar costs at purchase are not modeled. Buying has a bigger cash outlay than the model shows, so it flatters buying.
- Taxes and incentives. Mortgage interest deductions, local credits and capital gains rules vary by place. The previous chapter covered why these are left to your own situation.
- Mortgage insurance. Many loans with less than 20% down add a monthly charge.
- Taxes on investment returns. The 5% renter return is treated as untaxed, which flatters renting.
- Renters' costs. Renter's insurance, moving costs and deposits are not modeled.
Finding the break-even year
A single comparison at one horizon is not enough. The question is when the lines cross. The function below runs the model for each horizon from 1 year upward and reports the first year in which the owner is ahead. It also adds a buyer closing cost parameter, which is the first omission worth fixing. Extra cash spent at purchase is cash the renter can invest instead.
def monthly_payment(principal, annual_rate, years):
r = annual_rate / 12
n = years * 12
if r == 0:
return principal / n
return principal * r / (1 - (1 + r) ** -n)
def simulate(years, price=400_000, down_pct=0.20, mort_rate=0.06, term=30,
appreciation=0.03, invest_return=0.05, rent=2_000,
rent_growth=0.03, carry_rate=0.02, sell_cost=0.06, buy_cost=0.03):
loan = price * (1 - down_pct)
pay = monthly_payment(loan, mort_rate, term)
balance = loan
# The renter invests the down payment and the buyer's closing costs.
portfolio = price * down_pct + price * buy_cost
for m in range(years * 12):
balance -= pay - balance * mort_rate / 12
value = price * (1 + appreciation) ** ((m + 1) / 12)
owner_cost = pay + value * carry_rate / 12
rent_m = rent * (1 + rent_growth) ** (m // 12)
portfolio = portfolio * (1 + invest_return / 12) + (owner_cost - rent_m)
value = price * (1 + appreciation) ** years
owner_wealth = value * (1 - sell_cost) - balance
return owner_wealth, portfolio
def break_even(max_years=30, **kwargs):
for y in range(1, max_years + 1):
owner, renter = simulate(y, **kwargs)
if owner > renter:
return y
return None # buying never pulled ahead within max_years
print("baseline:", break_even())
for a in (0.01, 0.03, 0.05):
print("appreciation", a, "->", break_even(appreciation=a))break_even returns None if buying never wins within the search window. That result is informative. It tells you the assumptions do not favor buying at any horizon you are likely to stay.
Common errors
- Forgetting the renter's investing. If the renter's down payment sits idle, the comparison is unfair to renting. The model assumes the money is invested at
invest_return. - Counting the whole payment as cost. The principal part of the payment builds equity. The model captures this through the balance, so the owner's cost is not the full payment minus nothing.
- Mixing nominal and real rates. If rent and appreciation include inflation, the investment return should too. A real return paired with nominal growth rates distorts the result.
- Using the same growth rate for everything. Home values, rents and investments grow at different rates. Keep them as separate parameters.
- Reading the result as a forecast. The output is a function of your assumptions. It shows what has to be true for buying to win.
Using the model well
Work in this order:
- Replace the defaults with your local price, rent, rate and tax numbers.
- Run
break_even()and note the year. - Change one assumption at a time and rerun it.
- Compare the break-even year with how long you realistically expect to stay.
The next chapter does step 3 systematically to find which assumptions matter most.
Exercise
In simulate, the line portfolio = portfolio * (1 + invest_return / 12) + (owner_cost - rent_m) can add a negative number. In what situation does that happen, and what does it mean in the real world?
Show answer
It happens when the rent for that month is higher than the owner's total monthly cost (mortgage payment plus carrying cost). The renter is then the one paying more each month, so the model withdraws that difference from the investment portfolio to cover it. In the real world, this is the renter dipping into savings to pay a rent that exceeds what the owner would spend.
Exercise
Add a mortgage-insurance charge of 0.5% of the loan per year to the owner's monthly cost, and explain which direction this moves the break-even year.
Show answer
In simulate, add loan * 0.005 / 12 to owner_cost (for example, owner_cost = pay + value * carry_rate / 12 + loan * 0.005 / 12). This raises the owner's monthly cost, so the renter invests a larger difference each month. Renter wealth grows faster, so buying needs more years to pull ahead and the break-even year moves later. In practice, this charge usually ends once the owner has enough equity, so a more exact model would stop it at that point.