## Calculation principle and Java implementation of mortgage loan

sunfulv 2020-11-09 23:48:43
calculation principle java implementation mortgage

## Number part (6) Mortgage Calculator-- Mortgage calculator

#### Title Description ：

Mortgage Calculator – Calculate the monthly payments of a fixed term mortgage over given Nth terms at a given interest rate. Also figure out how long it will take the user to pay back the loan.

#### Topic translation ：

Mortgage calculator —— At a given interest rate , Calculate the fixed term mortgage loan in N Monthly repayment of . At the same time, calculate how long it takes for the user to repay the loan .

1. What is a mortgage loan
A mortgage is a loan that provides private assets as a guarantee for debt , It's mostly a mortgage loan from Ba when buying real estate .

2. The type of mortgage
There are many types of mortgages , The type of mortgage is defined by the following factors .

• The interest rate (interest) ： It is divided into fixed rate and floating rate
• Time limit (Term) ： Mortgage loans usually have a maximum repayment period
• The amount and frequency of repayment (Payment amount and frequency) ： Specify the time interval between two repayments and the number of repayments required in each period
3. Repayment method
Mortgage loans are usually paid in installments , With a fixed exchange rate , Set a repayment period , And then pay a certain amount on time every month .
There are two common repayment methods ： Equal principal and interest repayment and equal principal repayment
The monthly repayment amount of the two repayment schemes is calculated as follows ：
Suppose the annual interest rate of the loan is r, The repayment period is Y year , The loan principal is P, The monthly repayment amount is A
The monthly interest rate of the loan $$R =r/12$$, The repayment period is $$N=12Y$$

• Repayment of equal principal and interest ：
Equal principal and interest repayment refers to when repaying , The total amount of repayment per month is the same . The monthly payment of principal and interest is variable
Suppose that t Months after repayment , The remaining total repayment amount is $$p(t)$$.

\begin{aligned} &p(0)=P\\ &p(1)=p(0)(1+R)-A=P(1+R)-A\\ &p(2)=P(1)(1+R)-A=[P(1+R)-A](1+R)-A=P(1+R)^2-(1+R)A-A\\ &...\\ &p(t)=P(1+R)^t-A(1+R)^{t-1}-A(1+R)^{t-2}-...-A(1+R)-A\\ \end{aligned}

We can get the balance after the monthly payment $$p(t)$$ The expression of ：

\begin{aligned} p(t)&=P(1+R)^t-A\sum_{i=0}^{t-1}(1+R)^i\\ &=P(1+R)^t-A\frac{1-(1+R)^t}{1-（1+R)}\\ &=P(1+R)^t-A\frac{(1+R)^t-1}{R} \end{aligned}

The number of repayment periods we have given is N, in other words $$p(N)=0$$, We can work out the monthly repayment amount A.
By the equation

\begin{aligned} P(n)=P(1+R)^N-A\frac{(1+R)^N-1}{R}=0\\ \end{aligned}

You can get

\begin{aligned} A&=\frac{PR(1+R)^N}{(1+R)^N-1}\\ \end{aligned}

in other words , If the same amount of principal and interest is used to repay , The monthly amount of repayment is $$\frac{PR(1+R)^N}{(1+R)^N-1}$$

Although the amount of repayment per month is the same , The interest and principal repayments vary from month to month
The first t+1 The interest you need to pay back for each month $$i(t+1)$$, Is the remaining repayment amount before the repayment of the month $$p(t)$$ Multiply by the monthly interest rate R

\begin{aligned} i(t+1)&=p(t)R\\ &=PR(1+R)^t-A(1+R)^t+A\\ &=(PR-A)(1+R)^t+A\\ &=((PR-A)(1+R)^{t-1}+A)(1+R)-A(1+R)+A\\ &=i(t)(1+R)-AR\\ \end{aligned}

because $$(PR-A)<0$$, therefore $$i(t)$$ It's about t The minus function of , That is to say, in the amount of repayment each month , The proportion of interest is reduced , And the proportion of principal is rising

• Equal principal repayment ：
In the way of equal principal repayment , The monthly repayment principal is the same , But the interest paid each month is different , So the total amount of repayment per month changes .
You need to pay back the principal every month pr Divide the total principal by the total number of repayment months .

\begin{aligned} pr = \frac{P}{N} \end{aligned}

You need to pay interest every month $$pi(t)$$ = ( The principal - The sum of the principal returned )* Monthly interest rate

\begin{aligned} pi(t)& = (P-pr(t-1))R\\ &=-prAt+(A+P)R\\ &=-\frac{PR}{N}t+(\frac{P}{N}+P)R\\ \end{aligned}

You can see , Interest paid each month $$pi(t)$$ It's about t The minus function of , It shows that the interest paid every month is gradually decreasing . As the amount of principal paid back each month remains unchanged , So the total amount of loan repayments per month is decreasing .

#### Program realization

User input loan exchange rate , The total amount of the loan , The repayment time limit and the choice repayment way .
The program outputs the total amount the user needs to repay each month, as well as the amount of principal and interest to be repaid .

import java.util.Scanner;
public class MortgageCalculator{
public static void mortgageCalcute(double P,double interest,int Y,int type){
// Input parameter total loan P, lending rate interest, Repayment period Y, Repayment type type(0 It means the same amount of principal and interest ,1 Means equal principal repayment )
switch(type){
case 0:
equalLoanPayment(P,interest,Y);
break;
case 1:
equalPrincipalPayment(P,interest,Y);
break;
}
}
public static void equalLoanPayment(double P,double interest,int Y){ // Equal principal and interest repayment calculation function
int N = Y*12;
double R = interest/12;
double A = P*R*Math.pow(1+R,N)/(Math.pow(1+R,N)-1);
System.out.printf(" Monthly repayment of principal and interest %7.2f\n",A*10000);
double[] pi = new double[N];
pi = P*R;
System.out.printf(" The first 1 Months of interest to be paid :%8.2f The first 1 The principal to be paid back in six months is :%7.2f\n",pi*10000,(A-pi)*10000);
for(int i=1;i<N;i++){
pi[i] = pi[i-1]*(1+R)-A*R;
System.out.printf(" The first %d Months of interest to be paid :%7.2f The first %d The principal to be paid back in six months is :%7.2f\n",i+1,pi[i]*10000,i+1,(A-pi[i])*10000);
}
}
public static void equalPrincipalPayment(double P,double interest,int Y){ // Equal principal repayment calculation function
int N = Y*12; // The total month of repayment
double R = interest/12; // The monthly interest rate for repayment
double A = P*1.0/N; // You need to pay back the principal every month
System.out.printf(" The principal to be repaid each month %7.2f\n",A*10000);
double[] pi = new double[N+1];
for(int i=1;i<=N;i++){
pi[i] = -P*R*1.0/N*i+(P/N+P)*R;
System.out.printf(" The first %d Months of interest to be paid :%7.2f. The first %d The principal and interest to be paid back for six months :%7.2f\n",i+1,pi[i]*10000,i+1,(pi[i]+A)*10000);
}
}
public static void main(String[] args){
//equalPrincipalPayment(45.4, 3.25/100, 15);
Scanner sc = new Scanner(System.in);
System.out.println(" Choose the repayment method : 0 Equal principal and interest ,1 Equal principal ");
int PaymentType = sc.nextInt();
System.out.println(" Enter the total amount of repayment ( Company : ten thousand ), Annual interest rate of repayment （ percentage ） And repayment period , Space off ");
double Payment = sc.nextDouble();
double interest = sc.nextDouble();
int years = sc.nextInt();
sc.close();
System.out.println(" The total amount of repayment :"+Payment+" Annual interest rate of repayment :"+interest+"% "+" Repayment period :"+years+" year ");
mortgageCalcute(Payment,interest/100,years,PaymentType);
}
}