### Mortgage Calculator

See screenshots at the bottom of the page

### Order Description

Buying a house is one of the biggest financial transactions that many people ever undertake. Even a small difference in interest rates or monthly payments can make a large difference in how much you ultimately pay for your mortgage. However, these details are often buried in masses of paperwork--so it pays to be able to calculate the long-term cost of a mortgage yourself.

In a mortgage, the bank lends you a certain amount of principal to purchase a house at a certain interest rate. Every month, the amount you owe (the balance) first increases due to interest: 1/12 of the interest rate times the current balance. Then the balance decreases due to your monthly payment.

For example, suppose you borrow \$100,000\$100,000 at 5%5% annual interest, with \$500\$500 monthly payments. In the first month, the interest increases the balance by \$416.67\$416.67, and then your payment reduces it by \$500\$500, for a remaining balance of \$99,916.67\$99,916.67. In the second month, the interest charge is \$416.32\$416.32, and the remaining balance is \$99,832.99\$99,832.99.

If you continue this process, you get an amortization table like this:

month     payment     interest     balance

1          500          416.67      99916.67

2          500          416.32      99832.99

3          500          415.97      99748.96

4          500          415.62      99664.58

5          500          415.27      99579.85

6          500          414.92      99494.76

7          500          414.56      99409.32

8          500          414.21      99323.53

9          500          413.85      99237.38

10         500          413.49      99150.87

You can also compute the total amount of time and money to pay off the mortgage. In this example, it takes 35 years and 11 months, and the total amount paid is approximately \$215,458.84\$215,458.84.

🎯 Create a mortgage calculator that takes as input the principal loan amount, interest rate, and monthly payment. As output, your calculator should generate an amortization table, and compute how many years and months it took to pay off the mortgage, and report the total amount of payments over that time.

Things to consider when implementing the calculator

1. For dollar values, only display two digits of precision after the decimal point.
• You can assume that the bank will not round internally!
• You must not round internally either!)
The final payment will almost certainly be smaller than the others, so be careful to check for that case so you don't end up with a negative balance. The final balance must be 0. If the monthly payment is too small, the balance will go up every month!
• If this happens, the program should stop and display an appropriate error message.
• Manually stopping your code in this case is not considered a solution -- your submitted code must detect the problem itself, and not enter an infinite loop.
If you accidentally create an infinite loop during development and testing, interrupt or restart the kernel. See the Kernel menu up top. Use the modulus or %% operator to separate the years and months. Your program should only display the columns requested, and the rows should be formatted to appear under the appropriate heading. Because this is an iPython notebook, which is inherently interactive, don't use the input function to make your code interactive. Instead, set the values of the input (such as the interest rate) as variables defined at the top of your code. Alternatively, if you write one or more functions, you can set the input as the arguments of your function(s).

Problem 2(b). Testing.

When writing complicated code, it is always a good idea to test your code on simple examples.

🎯 Use the following test cases to make sure your code matches calculations you can perform by hand. (Of course, if it does not match, please fix your code.)🎯 For each test case, print the results.

For each, use \$500\$500 for the principal, and an annual interest rate of 5%5%.

1. Monthly Payment: \$100\$100 (Total paid should be \$506.35\$506.35.)
2. Monthly Payment: \$500\$500 (Total paid should be \$502.09\$502.09.)
3. Monthly Payment: \$1\$1 (Code should display a suitable error message and quit gracefully.)

Problem 2(c). Calculator Analysis.

🎯 Answer the following questions in a markdown cell.

1. Describe in your own words how the mortgage calculator works. What are the steps you are computing? What are you keeping track of in each iteration?
2. Suppose you had a mortgage with a principal of \$250,000\$250,000, an interest rate of 4%4%, and a monthly payment of \$1000\$1000.
• How long would it take you to pay it off?
• How much would you have paid in total?
• Print the amortization schedule.