Loan Calculator. Stage 4/4

Differentiate payment

Report a typo

Description

Finally, let's add to our calculator the capacity to compute differentiated payments. We’ll do this for the type of repayment where the loan principal is reduced by a constant amount each month. The rest of the monthly payment goes toward interest repayment and it is gradually reduced over the term of the loan. This means that the payment is different each month. Let’s look at the formula:

Dm=Pn+i(PP(m1)n)D_m = \dfrac{P}{n} + i \cdot \left( P - \dfrac{P \cdot (m-1)}{n} \right)

Where:

DmD_m = mmth differentiated payment;

PP = the loan principal;

ii = nominal interest rate. This is usually 1/12 of the annual interest rate and is a float value, not a percentage. For example, if our annual interest rate = 12%, then i = 0.01.

nn = number of payments. This is usually the number of months in which repayments will be made.

mm = current repayment month.

In this stage, the user has to provide more inputs, so your program should be able to parse new command-line arguments. Let's add the --type one, so now the calculator should be run with 4 arguments:

python creditcalc.py --type=diff --principal=1000000 --periods=10 --interest=10

The --type argument is indicating the type of payment: "annuity" or "diff" (differentiated). It must always be provided in any run.

Also, it is possible that the user will make a mistake in the provided input. The program should not fail, but inform the user, that he has provided "Incorrect parameters".

  • --type is specified neither as "annuity" nor as "diff" or not specified at all:

    > python creditcalc.py --principal=1000000 --periods=60 --interest=10
    Incorrect parameters
  • For --type=diff, the payment is different each month, so we can't calculate months or principal, therefore a combination with --payment is invalid:

    > python creditcalc.py --type=diff --principal=1000000 --interest=10 --payment=100000
    Incorrect parameters
  • Our loan calculator can't calculate the interest, so it must always be provided:

    > python creditcalc.py --type=annuity --principal=100000 --payment=10400 --periods=8
    Incorrect parameters
  • For differentiated payments you will need 4 out of 5 parameters (excluding payment), and the same is true for annuity payments (the user will be calculating the number of payments, the payment amount, or the loan principal). Thus, you should also display an error message when fewer than four parameters are provided:

    > python creditcalc.py --type=annuity --principal=1000000 --payment=104000
    Incorrect parameters
  • Negative values should not be provided:

    > python creditcalc.py --type=diff --principal=30000 --periods=-14 --interest=10
    Incorrect parameters

Objectives

In this stage, you are going to implement the following features:

  • Calculation of differentiated payments. To do this, the user can run the program specifying interest, number of monthly payments, and loan principal.

  • Ability to calculate the same values as in the previous stage for annuity payment (principal, number of monthly payments, and monthly payment amount).

  • Handling of invalid parameters. It's a good idea to show an error message if the user enters invalid parameters.

  • The only thing left is to compute the overpayment: the amount of interest paid over the whole term of the loan.

Examples

The greater-than symbol followed by a space (> ) represents the user input. Note that this is not part of the input.

Example 1: calculating differentiated payments

> python creditcalc.py --type=diff --principal=1000000 --periods=10 --interest=10
Month 1: payment is 108334
Month 2: payment is 107500
Month 3: payment is 106667
Month 4: payment is 105834
Month 5: payment is 105000
Month 6: payment is 104167
Month 7: payment is 103334
Month 8: payment is 102500
Month 9: payment is 101667
Month 10: payment is 100834

Overpayment = 45837

In this example, the user wants to take a loan with differentiated payments. You know the principal, the count of periods, and interest, which are 1,000,000, 10 months, and 10%, respectively.

The calculator should calculate payments for all 10 months. Let’s look at the formula above. In this case:

P=1000000P = 1000000
n=10n = 10
i= interest12100%=10%12100%=0.008333...i = \dfrac{\text{ interest} }{ 12 \cdot 100\% } = \dfrac { 10\% }{12 \cdot 100\% } = 0.008333...

Now let’s calculate the payment for the first month:

D1=Pn+i(PP(m1)n)=100000010+0.008333...(10000001000000(11)10)=108333.333...D_1 = \dfrac{P}{n} + i \cdot \left(P - \dfrac{ P \cdot (m-1) }{ n } \right)=\dfrac{ 1000000 }{ 10 } + 0.008333... \cdot \left( 1000000 - \dfrac{ 1000000 \cdot (1-1) }{ 10 } \right) = 108333.333...

The second month (m=2m = 2):

D2=Pn+i(PP(m1)n)=100000010+0.008333...(10000001000000(21)10)=107500D_2 = \dfrac{P}{n} + i \cdot \left(P - \dfrac{ P \cdot (m-1) }{ n } \right)=\dfrac{ 1000000 }{ 10 } + 0.008333... \cdot \left( 1000000 - \dfrac{ 1000000 \cdot (2-1) }{ 10 } \right) = 107500

The third month (m=3m = 3):

D3=Pn+i(PP(m1)n)=100000010+0.008333...(10000001000000(31)10)=106666.666...D_3 = \dfrac{P}{n} + i \cdot \left(P - \dfrac{ P \cdot (m-1) }{ n } \right)=\dfrac{ 1000000 }{ 10 } + 0.008333... \cdot \left( 1000000 - \dfrac{ 1000000 \cdot (3-1) }{ 10 } \right) = 106666.666...

And so on. You can see other monthly payments above.

Your loan calculator should output monthly payments for every month as in the first stage. Also, round up all floating-point values.

Finally, your loan calculator should add up all the payments and subtract the loan principal so that you get the overpayment.

Example 2: calculate the annuity payment for a 60-month (5-year) loan with a principal amount of 1,000,000 at 10% interest

> python creditcalc.py --type=annuity --principal=1000000 --periods=60 --interest=10
Your annuity payment = 21248!
Overpayment = 274880

Example 3: fewer than four arguments are given

> python creditcalc.py --type=diff --principal=1000000 --payment=104000
Incorrect parameters.

Example 4: calculate differentiated payments given a principal of 500,000 over 8 months at an interest rate of 7.8%

> python creditcalc.py --type=diff --principal=500000 --periods=8 --interest=7.8
Month 1: payment is 65750
Month 2: payment is 65344
Month 3: payment is 64938
Month 4: payment is 64532
Month 5: payment is 64125
Month 6: payment is 63719
Month 7: payment is 63313
Month 8: payment is 62907

Overpayment = 14628

Example 5: calculate the principal for a user paying 8,722 per month for 120 months (10 years) at 5.6% interest

> python creditcalc.py --type=annuity --payment=8722 --periods=120 --interest=5.6
Your loan principal = 800018!
Overpayment = 246622

Example 6: calculate how long it will take to repay a loan with 500,000 principal, monthly payment of 23,000, and 7.8% interest

> python creditcalc.py --type=annuity --principal=500000 --payment=23000 --interest=7.8
It will take 2 years to repay this loan!
Overpayment = 52000
Write a program
# write your code here

___

Create a free account to access the full topic