Mathematical Modeling - Home Loan Problem - Python Implementation

software:

python3.10 version

content:

The total commercial loan for a house is 1.3 million yuan, with a term of 30 years and an annual interest rate of 5.8%.

  1. Please use the two repayment methods of equal principal and interest and equal principal to calculate the monthly interest, principal, repayment amount and total repayment.
  2. Under the equal principal and interest repayment method, change the repayment period to one quarter and one year (the corresponding r is also changed), and calculate the total repayment under the two new periods respectively. And explain whether the shorter the period, the smaller the total repayment amount?

Modeling:

Let the loan amount be x0, the monthly interest rate is r, the annual interest rate is R, the number of loan months is n, and the number of loan years is N

R=R/12,n=Nx12

Equal principal and interest repayment model:
monthly repayment: a=xr(1+r)n/((1+r)n-1)
total repayment: A1=na=n xr(1+r)n/(( 1+r)n-1)
Total interest: s=A1-x

Equal principal repayment model:
Monthly repayment: x=x/n+x*(1-(k-1)/n)rk=1,2,….,n
Total repayment: A2=x+xr *(n+1)/
2Total interest: s=A2-x
 


python code:

(1) Monthly

# 贷款额为x0,月利率为r,年利率为R,贷款月数为n,贷款年数为N
# x0 = int(input("请输入贷款额(元):"))
# R = float(input("请输入年利率(如5.8%,输入:0.058):"))
# N = int(input("请输入贷款年数(年):"))
# r = R / 4
# n = N * 4

# test data
x0=1300000
R=0.058
N=30
r = R / 12
n = N * 12

# 月均还款(本金+利息)
a = x0 * r * pow((1 + r), n) / (pow((1 + r), n) - 1)

# 还款利息总和
Y = n * x0 * r * pow((1 + r), n) / (pow((1 + r), n) - 1) - x0


print("-----等额本息计算-----")

# 还款总额
A1 = n * a
print("第1个月,利息:%s,本金:%s,还款金额(本金+利息):%s,还款总额:%s" % (x0 * r, a - x0 * r, a, A1))
# 第2 - n个月还款利息
for t in range(2, n + 1):
    ci = (x0 * r - a) * pow((1 + r), (t - 1)) + a       # 每月的利息
    bi = a - ci                                         # 每月的本金
    print("第%d个月,利息:%s,本金:%s,还款金额(本金+利息):%s,还款总额:%s" % (t, ci, bi, a, A1))

print("-----等额本金计算-----")

# 每月应还本金
d = x0 / n
A2 = 0
for m in range(1, n + 1):
    f = (x0 - d * (m - 1)) * r  # 每月应还利息
    g = d + f                   # 每月应还本金
    A2 = A2 + g                 # 已还款金额

    print("第%d个月,利息:%s,本金:%s,还款金额(本金+利息):%s" % (m, f, d, g))
print("还款总额:%s" % A2)

(2) Every quarter

#test data
x0=1300000
R=0.058
N=30
r = R / 4
n = N * 4
# 月均还款(本金+利息)
a = x0 * r * pow((1 + r), n) / (pow((1 + r), n) - 1)
# 还款利息总和
Y = n * x0 * r * pow((1 + r), n) / (pow((1 + r), n) - 1) - x0
print("-----等额本息计算-----")
# 还款总额
A1 = n * a
print("第1年,利息:%s,本金:%s,还款金额(本金+利息):%s,还款总额:%s" % (x0 * r, a - x0 * r, a, A1))
# 第2 - n个月还款利息
for t in range(2, n + 1):
    ci = (x0 * r - a) * pow((1 + r), (t - 1)) + a       # 每月的利息
    bi = a - ci                                         # 每月的本金
    print("第%d年,利息:%s,本金:%s,还款金额(本金+利息):%s,还款总额:%s" % (t, ci, bi, a, A1)

 every year

#test data
x0=1300000
R=0.058
N=30
r = R / 1
n = N * 1

print("-----等额本息计算-----")
# 月均还款(本金+利息)
a = x0 * r * pow((1 + r), n) / (pow((1 + r), n) - 1)
# 还款利息总和
Y = n * x0 * r * pow((1 + r), n) / (pow((1 + r), n) - 1) - x0
# 还款总额
A1 = n * a
print("第1年,利息:%s,本金:%s,还款金额(本金+利息):%s,还款总额:%s" % (x0 * r, a - x0 * r, a, A1))
# 第2 - n个月还款利息
for t in range(2, n + 1):
    ci = (x0 * r - a) * pow((1 + r), (t - 1)) + a       # 每月的利息
    bi = a - ci                                         # 每月的本金
    print("第%d年,利息:%s,本金:%s,还款金额(本金+利息):%s,还款总额:%s" % (t, ci, bi, a, A1))


Output result:

Equal installments of principal and interest

 Monthly repayment amount: 7627.789494946575 yuan

Total repayment: 2746004.2181807673 yuan

Equivalent principal:

 The first month's repayment amount: 9894.4444444444445 yuan

The second month's repayment amount: 3628.5648148148143 yuan

Total repayment: 2434141.666666667

(2)

The cycle is a quarter:

Total repayment: 2750911.7763269176

The period is one year: 

Total repayment: 2772940.7758005406

Q: Does it indicate whether the shorter the period, the smaller the total repayment amount?

Answer: Compare:

Total monthly repayment: 2746004.2181807673 yuan

Quarterly total repayment: 2750911.7763269176 yuan

Total annual repayment: 2772940.7758005406 yuan

The comparison found that the shorter the period, the greater the total repayment

cannot be explained.

Guess you like

Origin blog.csdn.net/qq_21402983/article/details/124235629