Amortized Mortgage Payments

### Computing the Payment Amount
With home ownership on the horizon, I've been curious about how amortized mortgage payments work. My goal was the following: given a loan for the amount $P$, to be paid in $N$ installments, and compounded at the time of each payment with interest rate $r$, determine what constant payment $x$ should be paid each month to discharge the loan.
For one thing, it's not entirely obvious that this should be possible. The problem sounds like it might be over- or under-constrained so that $x$ may not be uniquely determined (or even exist at all). However, practical experience suggested that I ignore my misgivings. My next difficulty was in defining the problem carefully and correctly. While I had vague notions as to the mechanics of the loan and its payments, writing them down in mathematical terms is not totally trivial (I have never held a loan, amortized or otherwise).
On the first due date, there is still a principal $P$ outstanding. The bank calculates its interest: you owe $P(r-1)$ interest on top of your principal. [Note that I define my $r>1$ so, e.g., a $5\%$ per annum interest rate would translate to $r=1+\frac{0.05}{12}$ as a monthly compounding factor in my model.] Your first payment $x$ will cover the $P(r-1)$ in interest, and whatever is left over will go to reduce the principal. What if $x$ isn't enough to cover that interest? Then, come next month, your outstanding loan will have increased: you will never pay off the loan. Things only make sense if $x$ always exceeds the interest payment.
It's helpful to split up payment $n$ into a principal payment $p_n$ and an interest payment $i_n$. We stipulate that $p_n+i_n=x$, for every $n$. At the time of the first payment, we know that
$$i_1=P(r-1),$$
$$p_1=x-P(r-1).$$
At the time of the next payment, we only owe $P-p_1$ in principal (the principal is the same as the outstanding loan amount, because the interest tacked on each pay period gets paid off in full), so the interest charged is
$$i_2=(P-p_1)(r-1).$$
Continuing, we find a recurrence relation:
$$i_n=i_{n-1}-(r-1)p_{n-1}.$$
I know more about the principal payments than I do about the interest payments: specifically, I know that my principal payments eventually sum to the original value of the loan,
$$\sum_{i=1}^N p_i=P.$$
Because of that, I prefer to rewrite the recurrence relation in terms of $p_n$:
$$x-p_n=x-p_{n-1}-(r-1)p_{n-1}.$$
Rearranging:
$$p_n=rp_{n-1}.$$
We're in luck, because this is as easy as recurrence relations come! This is a geometric sequence:
$$p_n=cr^n$$
for some constant $c$.
Now my knowledge about principal payments comes in handy:
$$P=\sum_{n=1}^N p_n=c\sum_{n=1}^N r^n=cr\frac{r^N-1}{r-1}$$
determines $c$. We also previously computed the first principal payment, so now we have two expressions for it:
$$p_1=cr=P\frac{r-1}{r^N-1}=x-P(r-1)=p_1.$$
Solving for $x$, we have the desired payment amount
$$x=P(r-1)\left(1+\frac{1}{r^N-1}\right).$$
With a typical loan, we have $r\approx 1$ and $N\sim 100$ ($360$?), so $x$ is ever so slightly greater than the first interest payment. As a result, you put very little towards principal at the outset. As we see above, the principal payments increase geometrically from then on.
### Anything else of interest?
How much interest gets paid over the lifetime of the loan?
$$I=\sum_{n=1}^N (x-p_n)=xN-P=P\left[N(r-1)\frac{r^N}{r^N-1}-1\right].$$
We can easily compare the total interest paid to the face value of the loan: their ratio is
$$\frac{I}{P}=N(r-1)\frac{r^N}{r^N-1}-1.$$
For example, on a 30-year loan with a $5\%$ per annum interest rate, the total interest paid over the lifetime of the loan is approximately $93\%$ of the principal. With a $4\%$ rate, the total interest paid drops to $72\%$ of the principal.
### Playing around
The result above is fine, but it's not obvious how it behaves as a function of interest rate, $r$. Intuitively, the payment amount will go up if the interest rate goes up. How can this be quantified?
Letting $r=1+\epsilon$, we have
$$x=P\epsilon\left(1+\frac{1}{(1+\epsilon)^N-1}\right).$$
We can expand the binomial and then, if $\epsilon\ll 1$ (which is typical), expand in a Taylor series and drop high-order terms in $\epsilon$:
$$x=P\epsilon\left[1+\frac{1}{N\epsilon}\left(1-\frac{N-1}{2}\epsilon+O(\epsilon^2)\right)\right]=\frac{P}{N}+\left(\frac{N+1}{N}\right)\frac{P}{2}\epsilon+O(\epsilon^2).$$
To leading order, the payment is $x_0=P/N$, just what you'd pay if there was no interest. However, we have seen that, under typical circumstances, you can easily end up paying more in total **interest** than the face value of the loan. Thus the leading order approximation must not be very good if it completely discounts interest.
The first correction is effectively independent of $N$:
$$x_1=\left(\frac{N+1}{N}\right)\frac{P}{2}\epsilon\approx\frac{P}{2}\epsilon.$$
We recall that the interest payment at the first pay period was $P\epsilon$ and, of course, the interest payment at the final payment is approximately zero. Thus, this correction is an averaging of interest payments between the beginning and end, as if the decrease with time was linear (it's not — it's geometric).
With these two terms, let's compare to the real thing. A 30-year loan ($N=360$) with a $5\%$ ($\epsilon=\frac{0.05}{12}$) interest rate results in a monthly payment
$$x\approx 0.005368\cdot P$$
while the approximation is
$$x'\approx P\left[\frac{1}{360}+\frac{361}{360}\frac{0.05}{24}\right]=0.004867\cdot P.$$
This is pretty bad! If we only treat the dependence on interest rate as linear, the approximation to the payment amount is way off the mark.
The next term is
$$x_2=\frac{N^2-1}{12N}P\epsilon^2.$$
With this term, the approximation becomes
$$x''\approx 0.005388\cdot P$$
which is much closer.
Thus the payment amount goes up, to good approximation, quadratically with interest rate. Given a desired payment amount (as fraction of principal $P$), the relation can be inverted via the quadratic formula to find the appropriate interest rate:
$$r=1+\frac{36}{N-1}\left[\frac{1}{N+1}\sqrt{\frac{1}{3}\left(4N(N^2-1)\frac{x}{P}-(N-7)(N+1)\right)}-1\right].$$