## The Mortgages¶

Mortgage is an agreement that allows a borrower to borrow an amount *A* upfront, at a rate of interest *‘r’* that stays fixed for the entire duration of the loan. The loan is structured so that entire payable amount has to be payed in equal installments over N months. So, the mortage payments form a (often monthly) series as follows:
$$S = \{M, M, M… M \}~~~~~~~ N terms$$
Now the question is that what should be the monthly installement M, for the borrowed amount A?

### Current Value of Future Payments¶

What should be CURRENT VALUE of each of these future payments? Easy, discount each payment by the time it takes to come by.

$$S_{0} = \{ \frac{M}{1+r}, \frac{M}{(1+r)^2}, \frac{M}{(1+r)^3}… \frac{M}{(1+r)^N} \}$$

Since this series of payments is generated by an amount $A$ payed today, we reason : $$A = \frac{M}{1+r} + \frac{M}{(1+r)^2} + \frac{M}{(1+r)^3}… +\frac{M}{(1+r)^N}$$

### Calculating the Mortgage Payment¶

With a little rearrangement, above formula leads to:

$$ M = \frac{A}{\frac{1}{1+r} + \frac{1}{(1+r)^2} + \frac{1}{(1+r)^3}… +\frac{1}{(1+r)^N}} $$

Note that denominator is a geometric progression whose first term as well as common ratio are equal to $\frac{1}{1+r}$.

Hence this expression can be condensed into a nice relation by using the formula for sum of a geometric progression. But here we will not do so, but rather write a program to evaluate denominator by brute force.

```
###################################################
# Filename : time_value_of_money #
# Purpose : To demonstrate #
# 1. Console input/output #
# 2. Control Structures: while, if-else #
# #
# Author : Niel S. #
# The English Tea Company LLC #
###################################################
# The main function
def main():
keep_running = 'Y'
while (keep_running == 'Y'):
#Prompt user for inputs.
amount = float(input ("Enter total amount: $").strip())
rt_int = float(input ("Enter annunal percentage rate of interest: ").strip())
time_N = int (input ("Enter number of years(minimum =1 ): ").strip())
#Change percentage interest rate to absolute.
rt_int = 0.01 * rt_int
#Convert time and rate of interest to monthly figures.
rt_int = (rt_int)/12.
time_N = time_N*12
#Function call. User's arguments supplied to function parameters.
M = mortgage_installment(A=amount, r=rt_int, N = time_N)
out = 'The monthly payment for this loan = $'+str(M)
print (out)
keep_running = input("Enter 'Y' to keep running: ").strip().upper()
#Print some vertical space
print ('\n'*2)
def mortgage_installment(A, r, N):
'''
Parameters:
A = Total amount of the loan. (+ value only)
N = Number of months in loan term. (+ non zero values only)
r = Immutable rate of interest for loan term (+value only)
Return:
M = Derieved monthly payment.
'''
#If you recieve illegal values of parameters
### return some default indicating an error state.
if (A <= 0): return -1
if (r <= 0): return -1
if (N <= 0): return -1
denominator = 0;
for i in range(0,N):
denominator = denominator + 1./pow((1+r),i+1)
#Finally, monthly installment is:
M = round(A / denominator,0)
return M
if __name__ == '__main__':
main()
```