Why am I not getting total mortgage equal to $P \frac{r (1+r)^{n}}{(1 + r)^{n} - 1}$? For a computer science exercise I had to make a simple mortgage calculator. Given the principial, annual interest rate and period in years the program should return the monthly payment. I was not familiar with mortgage calculation, but after doing some research most of the resources pointed to this popular formula:
$$M = P \frac{r (1+r)^{n}}{(1 + r)^{n} - 1}$$
I personally didn't find this a very intuitive formula. Out of curiosity I tried to use a more intuitive approach based on the basic principles of a how a fixed rate loan should work.
So basically every month you pay percentage interest and that gets substracted from the principal, leaving you with a smaller principal and therefore a little bit less interest to pay.
If P is your principal and R is your annual interest rate (in decimal form) and r = (R/12) your monthly interest rate, then it would make sense to me to say that
$$(1 - r) * P$$
is the principal left after the first payment.
If we know the total amount of payments (12 months for every year), let's call this n, we can write something like this: $$(1 - r)^{n}* P$$
and this should equal to the remaining principal after n payments. For a 30 year loan, there would be 360 monthly payments and this formula should give us the amount of principal left after consistently paying r interest monthly.
If we substract this remainder from the total principal, we get the total amount of interest paid over the period. The sum of the interest paid and the original principal would be the total cost of the loan.
For the sake of having one uniform monthly payment, you could divide the total cost by the amount of payments.
This made sense to me, but this is always several dollars off from the calculation done with standard formula I've given in the beginning.
Take this example.
Principal = $100000, Annual Interest Rate = 3.92%, Period of loan = 30 years.
Using the standard formula we get a monthly payment of 472.81 dollars, but with my approach we get $470.02386.
I'm assuming that my way of thinking is not totally wrong, but there is clearly something off. I am not disputing the correctness of the standard formula as I have seen the proof for it. I just would like for someone to point out why my approach wouldn't work or how to correct to it.
Many thanks in advance.
 A: Your formula amounts to:
$$M = P \frac{2-(1-r)^n}{n}. \tag{1}$$
For the sake of clarity, $r$ is the effective monthly rate of interest, and $n$ is the number of months in the loan.  Note that we immediately run into trouble if $r > 1$, which is certainly possible for especially usurious loans (e.g., a very short term payday loan could easily have effective annual rates of interest exceeding $300\%$).  Practicality aside, the mathematics must remain valid even when the rate of interest is especially high.  But in your case, if we set $r = 3$, then $(1-r)^n = (-2)^n$, which would be positive if $n$ is even and negative if $n$ is odd.  This would suggest your periodic payment $M$ would be negative if the interest rate is high enough and there were an even number of payment periods!
Here is a more realistic example, in which we might let $r = 0.05$ and $n = 100$.  Then on a principal of $P = 100000$, the correct formula gives $M = 5038.31$, whereas yours is much smaller, only $M = 1994.08$.  As you can see, the deviation is substantial.
So now that we know that this formula cannot be mathematically correct, why does this model you propose fail?  The problem is that you are confusing different time values of money, and not taking into account the compounding of interest.
In particular, when you say that after $n$ months, the amount of principal remaining is $(1-r)^n P$, this makes no sense, because it means that the loan is never extinguished; the quantity $(1-r)^n$ for any $0 < r < 1$ and $n > 0$ is always positive.  For example, if $r = 0.0001$ and $n = 1000$, $(1-r)^n \approx 0.904833$, meaning that your formula would say that over $90\%$ of the principal is still remaining after $1000$ payments.
Instead, what you should write is an equation of value that matches the present value of the periodic payments made in the future, against the present value of the principal of the loan.  What I mean by this is that if $r$ is the monthly rate, then a payment of $M$ made $t$ months into the future would have the present value $$M(1+r)^{-t}.$$  Then the sum of these "discounted" payments must equal the total principal of the loan.
Alternatively, we can say that if $M$ is the monthly payment, then after the first month has passed, the loan has accrued interest of $rP$, hence the total outstanding balance is $P + rP = (1+r)P$, and you pay off $M$ of this, so the remaining balance is $(1+r)P - M$.  Then another month passes, at which point the outstanding balance has increased by a factor of $1+r$, and you pay off another month:
$$(1+r)((1+r)P - M) - M = (1+r)^2P - (1+r)M - M.$$
And then after the third month, the same process happens:  accumulate interest on the outstanding balance, then subtract the payment $M$:
$$(1+r)((1+r)^2 P - (1+r)M - M) - M = (1+r)^3 P - (1+r)^2 M - (1+r) M - M.$$
So after $n$ payments, we get
$$(1+r)^n P - (1+r)^{n-1} M - (1+r)^{n-2} M - \cdots - (1+r) M - M.$$
And if, after the $n^{\rm th}$ payment, we have no balance left over, this gives us the equation of value that lets us compute what $M$ needs to be in order for the loan to be paid off.
A: As saulspatz points out, you have a very fundamental misunderstanding about the finance of amortized mortgage payments. (Your principal isn't reduced by $r$ percent each month.) But precisely for that reason, it’s surprising that your answer is so close to the correct answer. Let’s see why mathematically.
Let $M$ be the monthly payment, $R$ the annual interest rate, $r:=R/12$, $P$ the principal, and $n$ the number of monthly payments.
Your formula is
$$M_{you}=\frac{P+\big(1-(1-r)^n\big)P}{n}=P\cdot\color{blue}{\frac{2-(1-r)^n}{n}}$$
whereas the correct formula is
$$M_{correct}=P\cdot\color{red}{\frac{r(1+r)^n}{(1+r)^n-1}}$$
The question is thus why
$$\color{blue}{\frac{2-(1-r)^n}{n}}\approx\color{red}{\frac{r(1+r)^n}{(1+r)^n-1}}$$
When $n$ is big and $r$ is small, $(1+r)^n\approx 1+rn$ (to first order, using the binomial theorem), so
$$\color{red}{\frac{r(1+r)^n}{(1+r)^n-1}}\approx\frac{r(1+rn)}{rn}=\frac{1+rn}{n}$$
and
$$\color{blue}{\frac{2-(1-r)^n}{n}}\approx\frac{2-(1-rn)}{n}=\frac{1+rn}{n}$$
That explains why you're close, despite a fundamental error.
A: You have a basic misunderstanding.  You say that after one payment, the principal remaining should be $P(1-r)$, that is, that the interest is applied to reduce the principal.  This is the reverse of how things actually work.  Each month, you incur an interest charge, and that's the first thing you must pay.  The interest is subtracted from your payment, and any remainder is applied to reduce the principal.  If you didn't make a payment, or made only a partial payment that didn't cover the full interest charge, then the loan payment would, in theory, increase.  (I say "in theory" because I'm not sure how it's treated in law, but as far as the computation goes, and the amount you will owe in the future, it's exactly how this will work.  Legally, the classification as interest or principal may be different, but the amount of money will be the same.)
