Have you really thought about it?

The loan will be repaid sooner or later. ------ Love Jueluo. Edit. Shrimp


Preface

Yesterday, we used Python to simply calculate the comparison between renovation loans and home loans. Our conclusion is that renovation loans will have higher interest rates than home loans in the same period. We also obtained a simple estimate of the annual interest rate of renovation loans. Our preliminary conclusion is that renovation loans are not cost-effective.

The only student who was admitted to Tsinghua University in the same class of high school said that if the renovation loan is repaid early, it is actually quite cost-effective to pay off in 2-3 years. So, have we really thought about our model? As a financial dick, we have to fight to the end with the bank's data analyst.
Can we really not squeeze the wool? Can I use a renovation loan to repay my mortgage?


In order to simplify the problem, our objective function is redefined

1. Remodel the objective function

In view of the fact of lack of money, we really need to borrow 300,000 from the bank for daily fooling around. One is a mortgage with an interest rate of 5.39%, with equal principal and interest; the other is a renovation loan with a monthly interest rate of 0.28%, with the principal repaid in installments. Both can be repaid in advance without additional costs, and the decoration loan can be loaned for up to 5 years.
In order to facilitate the comparison between the two, we assume that the mortgage of 300,000 is also a five-year term (because our mortgage must be much greater than 300,000, and 300,000 cannot be bought in a toilet in Shenzhen), and the monthly repayment amount of the mortgage is 13,681.42, which may be A little more money per month than the renovation loan, and the extra impact will not be considered when comparing the two.
Therefore, we can know by simply thinking about it. Assuming that we settle the loan after only one month, how much do we need to repay the mortgage? How much do I need to repay the renovation loan?
Assuming that we settled the loan after only one month, the total repayment of the mortgage was 30,1347.5; the total repayment amount of the decoration loan was 300,840, and the difference between the repayment of the decoration loan and the mortgage (ChaE): 507.5 yuan less.
WHAT? Didn’t it mean that mortgage interest rates are low? How can the renovation loan still be less repaid? In other words, if I used the renovation loan to repay the mortgage at the very beginning, and settled the loan in one month, the bank would make me 507.5 yuan less profit.
The calculation formula is derived as follows:
Suppose the total number of repayment periods is m, and the monthly repayment amount of the mortgage is X yuan, the calculation formula for the remaining principal of the
Insert picture description here
mortgage is: the monthly interest rate of the mortgage is β.
Therefore, we settle the mortgage after the m period, and the total repayment amount is: m×X + Am, the
repayment amount T of the renovation loan per period.
T = Loan principal (B0) / number of repayment periods (5×12) + loan principal (B0) × monthly interest rate
. The remaining principal of the renovation loan after m period is: the remaining principal of the
loan (Bm) = the initial principal of the loan Gold (B0)-The initial principal of the loan (B0)/(5×12)×m
B0 is 300,000. Therefore, after the m period, we settle the decoration loan, and the total repayment amount is: m×T + Bm
Therefore, the difference function between the two is:
ChaE = (m×T + Bm) – (m×X + Am)

What's the matter? If you think about it carefully, you will know that the decoration loan is calculated because of the initial principal B0 used when calculating the interest. Assuming that you pay back to the last month, your principal is almost gone, but your interest is still the original principal of the loan. 300,000 to calculate, definitely suffer a loss. Therefore, the further the renovation loan is, the higher the annualized interest rate. I drew a picture to show the specific trend of change.

Second, draw the model diagram

1. The change in interest rates of renovation loans converted into housing loans over the same period

I drew a graph to show the change in interest rates from renovation loans to housing loans over the same period. (Longer code, attached at the end) It
Insert picture description here
can be seen that before the 49th period, the interest rate of the renovation loan is lower than the interest rate of the mortgage. After the 49th period, the renovation loan will be higher than the housing loan.
So, do we need to hold the renovation loan to the 49 most cost-effective? The answer is NO, NO, NO.
Although the first installment loan interest rate is low, which is equivalent to 3.36% of the mortgage interest rate, but because of the short loan time, the interest difference between the two is only 507.5 yuan, and the second period is 995.38, which is higher than the first period. So the best month to gather wool is not the first period, nor is it the 49th period.
Continue to analyze our balance function ChaE.

2. Function graph of repayment balance

Set the number of periods m as an independent variable, and the resulting balance function is shown in the figure below. The relationship between the difference function ChaE and the number of prepayment periods m is as follows: the
Insert picture description here
blue histogram is the difference. It can be seen that before the 50th installment, the decoration loan is more favorable than the mortgage. After the 50th installment, the decoration loan is more expensive than the mortgage.
The place where we can save the most money is in 25 months, which can save 6594.35 yuan in interest. So my classmate from Tsinghua High School is correct.
And what is the annual interest rate of the corresponding renovation loan conversion for 25 months? This is another nonlinear equation solution, we need to use fsolve in scipy.optimize to solve these high-dimensional equations.

3. The interest rate of the best month of repayment

Use fsolve in scipy.optimize to solve the annual interest rate of
Insert picture description here
renovation loan conversion at 25 months. The annual interest rate of renovation loan conversion is 4.12%, which can save 1.2% compared with 5.39% housing loan. Therefore, closing the loan in 25 months can prevent the bank from taking away our 6K wool.


in conclusion

The decoration loan can really be used to repay the mortgage for 25 months! ! !
The result is quite unexpected, so when we lament that money is too much to spend, smart people can always see the operable space at any time.
Thanks to my high school classmates at Tsinghua University, let us take a serious look at this math problem in reality again. I also took out my POS machine and prepared for Dididi. . .

Knowledge Graph

For Python knowledge, this article uses:
1. Twinx with the same coordinate X and two graphs drawn on the left and right Y axes;
2. The Y axis is displayed as a% sign, and the format is set through set_yticklabels
3. Add arrows to the diagram And notes to reveal important information

Three, the code

1. The code of the interest rate change graph of the renovation loan converted into the housing loan over the same period

A graph was drawn to show the change in interest rates of renovation loans transformed into housing loans over the same period.
code show as below:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
from scipy.optimize import fsolve
#定义基本参数
benJin=300000     #本金30万
nianXian=5      #贷款年限
zongYueShu=nianXian*12  #贷款总月份数
FangDaiNianXian=5      #贷款年限
FangDaiZongYueShu=FangDaiNianXian*12  #贷款总月份数

#房贷计算公式,输入年利率,输出每月还款额
def fangDaiJiSuan(fangDaiLiLv,tiQianHuanKuanYue):
    yueLiLv = fangDaiLiLv / 12  # 月利率
    meiYueHuanKuan = (benJin * yueLiLv * (1 + yueLiLv) ** FangDaiZongYueShu) / ((1 + yueLiLv) ** FangDaiZongYueShu - 1) #每月还款额
    print('每月房贷还款额度为:', meiYueHuanKuan)
    shiJiGuiHuanE = meiYueHuanKuan*tiQianHuanKuanYue   #实际归还额
    shengYuBenJin = benJin * ((1 + yueLiLv) ** tiQianHuanKuanYue) - meiYueHuanKuan * (
                (1 + yueLiLv) ** tiQianHuanKuanYue - 1) / yueLiLv    #剩余本金
    print('房贷剩余本金:', shengYuBenJin)
    fangDaiZongHuan = shiJiGuiHuanE + shengYuBenJin  # 房贷总还款金额
    print('房贷总计还款额为:', fangDaiZongHuan)

    return fangDaiZongHuan

#信用卡利息计算公式,输入月利率,输出每月还款额
def xinYongKaJiSuan(yueLiXi,tiQianHuanKuanYue):
    meiYueliXi = benJin * yueLiXi  # 信用卡月利息
    meiYueHuanBenJin = benJin / zongYueShu  # 信用卡每月归还本金
    meiYueHuanKuanZongE = meiYueliXi + meiYueHuanBenJin  # 信用卡每月归还额
    xinYongKaShiJiGuiHuan = meiYueHuanKuanZongE * tiQianHuanKuanYue   #信用卡实际还款
    xinYongKaShengYuBenJin = benJin - meiYueHuanBenJin * tiQianHuanKuanYue    #剩余本金的
    print('信用卡每月还款额:', meiYueHuanKuanZongE)
    print('信用卡剩余本金:', xinYongKaShengYuBenJin)
    huanKuanZongE = meiYueHuanKuanZongE * tiQianHuanKuanYue + xinYongKaShengYuBenJin  #信用卡总计还款金额
    print('信用卡总计还款额为:', huanKuanZongE)

    return huanKuanZongE

#定义差额函数,也就是求非线性方程的解,输入房贷年利率,输出房贷和信用卡的还款差额
def chaE(x):
    ##信用卡利息参数
    yueLiXi = 0.0028  # 信用卡月利率
    return fangDaiJiSuan(x,tiQianHuanKuanYue)-xinYongKaJiSuan(yueLiXi,tiQianHuanKuanYue)

#主函数
n = 60      #贷款总月份数
tiQianHuanKuanYueList = np.arange(1,n+1,1)   #提前还款月份
solveList = []     #求解得到的利率列表
for i in np.arange(1,n+1,1):
    tiQianHuanKuanYue = tiQianHuanKuanYueList[i-1]
    solve = fsolve(chaE,[0.5])  #求解房贷年利率
    solveList.append(solve.tolist())
    #打印求解结果
    print(solve)
    #验证结果是否真的是方程的解
    print(np.isclose(chaE(solve), [0.0],atol=1e-3))  #检验解是否是符合方程组的近似解
print(solveList)

plt.rcParams['font.sans-serif']=['SimHei']  #解决中文乱码
plt.rcParams['axes.unicode_minus'] = False
#ax = plt.figure(111)
solveList2 = np.array(solveList).reshape((60,1)).tolist()
print('solveList2) 为')
print(solveList2)
df = pd.DataFrame(solveList2,index=tiQianHuanKuanYueList)
print(df)
# you get ax from here
ax = df.plot()
type(ax)  # matplotlib.axes._subplots.AxesSubplot

# 为了让Y轴显示为百分数
vals = ax.get_yticks()
ax.set_yticklabels(['{:,.2%}'.format(x) for x in vals])
plt.xlabel('5年期贷款提前归还月份')
plt.ylabel('转换成同期房贷利率')
fangdaililv = [0.0537]*n
print(fangdaililv)
plt.plot(tiQianHuanKuanYueList,fangdaililv)    #房贷利率
fmt = '%.0f%%' # Format you want the ticks, e.g. '40%'
yticks = mtick.FormatStrFormatter(fmt)
vals = ax.get_yticks()   #设置Y轴的格式
ax.set_yticklabels(['{:,.2%}'.format(x) for x in vals])
plt.legend(('装修贷转换利率','房贷利率'))
plt.grid()
tem = solveList2[24][0]   #取最佳的还款年限的利率
tem=round(tem,4)*100
print('tem')
print(tem)
string = '最佳还款月在25月,转换利率为:' + str(tem)+'%'  #图示标注信息
print(string)
#添加箭头说明
plt.annotate(string,xy=(25,solveList2[24][0]),xytext=(25*0.7,solveList2[24][0]*1.1), arrowprops=dict(arrowstyle="->",connectionstyle = "arc3,rad=.1"))
#寻找最佳还款月份的利率
a=[25]*20
b=np.arange(0,0.06,0.003)
b=b.reshape((20,1))
plt.plot(a,b)
plt.ylim(0.02,0.065)
plt.show()

For Python knowledge, this article uses:
1. The Y-axis is displayed as the% sign, and the format is set through set_yticklabels
2. Arrows and notes are added to the diagram to reveal important information

2. Generating graphical code of balance function

Draw a generating diagram of the balance function.
code show as below:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#定义基本参数
benJin=300000     #本金30万
FangDaiNianXian=5      #贷款年限
tiQianHuanKuanYue = np.arange(1,60,1)   #提前还款月份
FangDaiZongYueShu=FangDaiNianXian*12  #贷款总月份数
##房贷计算参数
fangDaiLiLv=0.0539    #房贷利率
yueLiLv=fangDaiLiLv/12   #月利率
##房贷计算公式
meiYueHuanKuan=(benJin*yueLiLv*(1+yueLiLv)**FangDaiZongYueShu)/((1+yueLiLv)**FangDaiZongYueShu-1)
print('每月房贷还款额度为:',meiYueHuanKuan)
shiJiGuiHuanE = meiYueHuanKuan*tiQianHuanKuanYue   #实际归还额
shengYuBenJin = benJin*((1+yueLiLv)**tiQianHuanKuanYue) - meiYueHuanKuan*((1+yueLiLv)**tiQianHuanKuanYue-1)/yueLiLv
print('房贷剩余本金:',shengYuBenJin)
fangDaiZongHuan=meiYueHuanKuan*tiQianHuanKuanYue + shengYuBenJin    #房贷总还款金额
print('房贷总计还款额为:',fangDaiZongHuan)

XinYongKaNianXian = 5
zongYueShu=XinYongKaNianXian*12  #贷款总月份数
##信用卡利息计算参数
yueLiXi = 0.0028      #信用卡月利率
meiYueliXi=benJin*yueLiXi    #信用卡月利息
meiYueHuanBenJin = benJin/zongYueShu     #信用卡每月归还本金
meiYueHuanKuanZongE = meiYueliXi+meiYueHuanBenJin   #信用卡每月归还额
xinYongKaShiJiGuiHuan = meiYueHuanKuanZongE*tiQianHuanKuanYue
xinYongKaShengYuBenJin = benJin - meiYueHuanBenJin*tiQianHuanKuanYue
print('信用卡每月还款额:',meiYueHuanKuanZongE)
print('信用卡剩余本金:',xinYongKaShengYuBenJin)
huanKuanZongE = meiYueHuanKuanZongE*tiQianHuanKuanYue + xinYongKaShengYuBenJin   #信用卡总计还款金额
print('信用卡总计还款额为:',huanKuanZongE)

#两种贷款归还金额差额
chaE= huanKuanZongE-fangDaiZongHuan   #信用卡比房贷多还款的金额
print('信用卡比房贷多还款金额:',chaE)
print('差额最大出现在:',np.argmin(chaE))
#画图参数
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(tiQianHuanKuanYue,chaE,color='deepskyblue',label='left')
ax1.set_ylabel('装修贷与房贷差额(元)')
plt.xlabel('5年期贷款提前归还月份')
string = '最大的差额出现在25月,金额为:' + str(round(chaE[24],2))
plt.annotate(string,xy=(25,chaE[24]),xytext=(25*0.7,chaE[24]*1.1), arrowprops=dict(arrowstyle="->",connectionstyle = "arc3,rad=.1"))

plt.grid()
ax2 = ax1.twinx()
ax2.scatter(tiQianHuanKuanYue,fangDaiZongHuan,c='blue')
ax2.scatter(tiQianHuanKuanYue,huanKuanZongE,c='red',marker='*')

ax2.set_title('装修贷和房贷对比图')
plt.rcParams['font.sans-serif']=['SimHei']  #解决中文乱码
plt.rcParams['axes.unicode_minus'] = False
plt.legend(('房贷','装修贷'))
plt.ylabel('还款总额(元)')
plt.grid()
#plt.subplots_adjust(left=0.01)
plt.show()

For Python knowledge, this article uses:
1. Twinx with the same coordinate X and two graphs on the left and right Y axes;

Guess you like

Origin blog.csdn.net/weixin_43290383/article/details/112050945