We're going to model the cost of one specific home, and compare it to an equivalent rent. The house is here. I live in this area, and represent that the asking price, Zillow rent estimate, and financing terms are all reasonable.

So here's some rent estimates:

```
In [1]:
```rent_per_month = 1695
rent_increase_per_month = 50.0 / 12

Zillow has also helpfully calculated the 30-year fixed mortgage terms:

```
In [2]:
```price = 216000
down = price * 0.20
apr = 0.03411
term = 12 * 30
yearly_taxes = 3317

First, the fundamental law of finance. Net present value of any sum of money:

```
In [3]:
```def npv(future_value, discount_rate, t):
return future_value / (1 + discount_rate) ** t

```
In [4]:
```def rent(r, months):
def cost_of_month(t):
return rent_per_month + (rent_increase_per_month * t)
deposit = rent_per_month
deposit_back = npv(deposit, r, months)
return -deposit + sum(-npv(cost_of_month(t), r, t) for t in xrange(0, months)) + deposit_back

So, after 10 years, we will pay:

```
In [5]:
```rent(0, 12 * 10)

```
Out[5]:
```

Sounds about right.

Calculating the cost of buying is obviously more involved. Let's start with some basic mortgage definitions:

```
In [6]:
```class Mortgage:
def __init__(self, down, total, payments, rate):
self.down, self.total, self.payments, self.rate = down, total, payments, rate
def monthly_payment(self):
r = self.rate
P = self.total - self.down
N = self.payments
# From https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula
return (r * P) / (1 - (1 + r) ** -N)
def remaining(self, t):
balance = self.total - self.down
for _ in xrange(0, t):
interest = balance * self.rate
balance -= (self.monthly_payment() - interest)
return max(0, balance)

And plug in the terms Zillow gave us:

```
In [7]:
```mortgage = Mortgage(down, price, term, apr / 12)

Now let's do some basic sanity checking:

```
In [8]:
```mortgage.monthly_payment()

```
Out[8]:
```

```
In [9]:
```mortgage.remaining(12 * 30)

```
Out[9]:
```

```
In [10]:
```mortgage.remaining(0)

```
Out[10]:
```

All looking good.

Now we can model the cost of buying. We need to pay:

- money down for the mortgage
- closing costs on purchase, including agent fees, inspection, etc
- closing costs on sale
- monthly maintenance, monthly mortgage
- taxes (there are actually some important tax deductions you can claim against mortgage interest and PMI, but we're going to ignore those for simplicity)

```
In [11]:
```def buy_in_general(r, months, mortgage, buy_closing_costs, sale_closing_costs, maintenance, taxes, appreciation):
cost_to_close = mortgage.down + (mortgage.total * buy_closing_costs)
mortgage_cost = sum(npv(mortgage.monthly_payment(), r, t) for t in xrange(0, min(months, mortgage.payments)))
maintenance = sum(npv(maintenance * mortgage.total / 12, r, t) for t in xrange(0, months))
taxes = sum(npv(taxes, r, year * 12) for year in xrange(months / 12))
owed_bank = mortgage.remaining(months)
from_buyer = mortgage.total * appreciation
cash_at_sale = from_buyer - owed_bank - (from_buyer * sale_closing_costs)
return -cost_to_close - mortgage_cost - maintenance - taxes + npv(cash_at_sale, r, months)

Let's assume:

- Closing costs are 1% on buying, and 6.5% on sale. Costs are dramatically higher on sale because the sale transaction is typically structured so that agent commissions are "paid" by the seller out of the price of the home.
- Maintenance is 1% per year.
- The home does not appreciate in value whatsoever during the term of the investment (which is arguably pessimistic).

```
In [12]:
```def buy(r, months,
mortgage=mortgage,
buy_closing_costs=0.01,
sell_closing_costs=0.065,
maintenance=0.01,
taxes=yearly_taxes,
appreciation=1.0):
return buy_in_general(r, months,
mortgage, buy_closing_costs, sell_closing_costs,
maintenance, yearly_taxes, appreciation)

Let's see how much this house will cost us, undiscounted, over several years:

```
In [13]:
```[buy(0, y * 12) for y in xrange(1, 11)]

```
Out[13]:
```

Let's compare that to renting:

```
In [14]:
```[rent(0, y * 12) for y in xrange(1, 11)]

```
Out[14]:
```

So, renting becomes more expensive quickly, if we assume our money has no time value. That's not a good assumption, so let's delve a little deeper.

IRR is a common concept when evaluating investments. The IRR essentially states the smallest time value your money needs to have for you to lose money on your investment. In other words, you want to know the rate that causes your discounted cash flow to become negative.

```
In [15]:
```def irr(dcf):
if dcf(0) < 0: return
for ix in xrange(1000):
r_t = float(ix) / 1000
if dcf(r_t) < 0:
return r_t

Note that there are cases where there is **no** IRR. Your investment loses money, even assuming your upfront dollars are valued equally to later hypothetical dollars. It cannot grow your wealth.

There will be cases where this happens with our house investment.

We can model this "investment" by subtracting the rent we're not paying from the other costs that we now must pay. Now, (drumroll please), let's look at the IRR for our hypothetical house, depending on how long we stay in it:

```
In [16]:
```for year in xrange(1, 16):
def dcf(r_i): return buy(r_i / 12, 12 * year) - rent(r_i / 12, 12 * year)
print year, irr(dcf)

```
```

Not bad. We're well above the paltry 1.3% current risk free rate in year three. By year four, our return rises above the 10% average annual returns for the S&P 500.

There's obviously a couple caveats. First, all of this analysis is predicated on the house being a principle residence. There are many benefits, especially regarding capital gains, that flow from the government in this case. Investment properties are a whole 'nother ball of wax.

Finally, let's keep in mind that this investment scenario has one big caveat: we disappear into thin air after we sell the house, and never spend money on housing again. We'll return to that assumption later.

Of course, market conditions can change drastically and quickly. What does our investment look like in a super-pessimistic case, where our house loses half its value?

```
In [17]:
```def buy_ohno(r, months):
return buy(r, months, appreciation=0.5)

```
In [18]:
```for year in xrange(1, 21):
def dcf(r_i): return buy_ohno(r_i / 12, 12 * year) - rent(r_i / 12, 12 * year)
print year, irr(dcf)

```
```

Pretty grim, but still no doomsday. We'll need to stay in the house for a long time to beat the stock market if we get caught in a bad market cycle.

We've established that the longer you stay in the house, the better off you are. Let's look at what happens after your mortgage is payed off, and if your house reliably appreciates to keep up with a 2% inflation rate over the years.

```
In [19]:
```for year in xrange(1, 31, 5):
def buy_and_appreciate(r, months):
return buy(r, months, appreciation=1.02 ** year)
def dcf(r_i): return buy_and_appreciate(r_i / 12, 12 * year) - rent(r_i / 12, 12 * year)
print year, irr(dcf)

```
```

What? Our rate of return gets **worse** the longer we wait to sell?

Because the house appreciates less quickly than our return rate, it makes sense to sell it ASAP. We're forgetting that we actually have to live somewhere if we don't own a house. Let's do a more realistic comparison: whenever we sell our house, we'll buy another similar house somewhere else. So we'll be taking out a series of mortgages with the following guidelines:

- We want to retire someday, so we structure our payments to zero the mortgage after thirty years no matter what.
- We plow all of our equity from before into the new mortgage, minus closing costs.
- If we don't have enough equity to cover closing costs, we assume we can pony up the difference.

```
In [20]:
```total_months = 50 * 12
mortgage_months = 30 * 12
appreciation = 0.02
def mortgages(swap_months, appreciation):
prior_mortgage = None
swaps = [m for m in xrange(swap_months, total_months, swap_months)]
for month in [0] + swaps:
new_term = mortgage_months - month
if new_term > 0:
new_price = price * (1 + appreciation) ** month
new_down = new_price * 0.20
if not prior_mortgage:
equity = down
else:
equity = new_price - prior_mortgage.remaining(swap_months) - 0.075 * new_price
prior_mortgage = Mortgage(max(equity, new_down), new_price, new_term, apr / 12)
yield month, prior_mortgage

Let's look at how our equity changes in the first years:

```
In [21]:
```for year in (1, 2, 5, 10, 25, 50):
print year, list(m.total - m.remaining(year * 12) for _, m in mortgages(year * 12, appreciation / 12))[:5]

```
```

```
In [22]:
```for year in (1, 2, 5, 10, 25, 50):
print year, list(m.monthly_payment() for _, m in mortgages(year * 12, appreciation / 12))[-5:]

```
```

Wow, those one and two year scenarios look pretty grisly. We have to really pay to catch our equity back up. Even the final payments for five-year switching don't look too great, but they're still manageable.

The math is basically reinforcing common sense. It makes very little finanacial sense to constantly house swap. Let's look at the rate of return for the sensible swapping intervals:

```
In [23]:
```for year in (5, 10, 25, 50):
swap_months = year * 12
def buy_and_appreciate(r, term, mortgage):
return buy(r, term, mortgage=mortgage, appreciation=1.0)
def dcf_swapping_houses(r):
mortgage_list = list(mortgages(swap_months, appreciation / 12))
buy_costs = [npv(buy_and_appreciate(r / 12, swap_months, m), r / 12, month)
for month, m in mortgage_list[:-1]]
final_month, final_mortgage = mortgage_list[-1]
mortgage_remaining = 30 * 12 - swap_months
months_to_final = final_month
months_remaining = total_months - months_to_final
final_cost = npv(buy_and_appreciate(r / 12, months_remaining, final_mortgage), r / 12, months_to_final)
return sum(buy_costs) + final_cost - rent(r / 12, total_months)
print year, irr(dcf_swapping_houses)

```
```

Not bad. If we're willing to put up with some pretty high catch-up payments at the end of our 30-year term, we easily beat the stock market. Even if we switch houses every five years.

If we're planning on staying put for a very long time, a house looks like it could be a fantastic investment.

Another thing to keep in mind: with this time horizon, the final sale price of the house itself is practically inconsequential. Here's the present value of the cash we'll get on sale, discounted at 10% per year:

```
In [24]:
```npv(price * (1 + appreciation) ** 50, 0.1 / 12, 50 * 12)

```
Out[24]:
```

This is a tiny fraction of the total NPV of our housing costs:

```
In [25]:
```buy(0.1 / 12, 50 * 12)

```
Out[25]:
```

So is housing a shitty investment? It certainly can be, especially if you change houses every year. But if you're in a specific location for the long haul, investment in housing for your principle residence will probably beat market returns.

Another final note: the current lending environment is **very** favorable towards buying a house. Interest rates are at all time lows. All the analysis here will certainly need to change in the future.