龙空技术网

还在头疼每月房贷还款,这个房贷计算机让你一目了然

华为云开发者联盟 19154

前言:

目前你们对“贷款等额本金利息计算器”大致比较着重,咱们都想要知道一些“贷款等额本金利息计算器”的相关内容。那么小编同时在网上网罗了一些有关“贷款等额本金利息计算器””的相关资讯,希望朋友们能喜欢,朋友们快快来学习一下吧!

本文分享自华为云社区《房贷计算器-从原理、计算到提前还款和可视化-云社区-华为云》,作者: 蜉蝣与海 。

摘要:最近各地楼市震荡不断,不少银行纷纷降息,随后更是引发了一波提前还款的大潮。通过楼市小程序上贷款计算器等工具人们可以很容易的了解每期还款本金、不同还款方式的利息差异、提前还款节省利息等问题。了解这些工具的计算原理,可以做到心中有数,临危不慌。本文已发布至华为云生态社区AI Gallery,文中涉及所有代码可以直接通过页面进入云上Code Lab运行,欢迎开发者前往体验。

前言

最近各地楼市震荡不断,2022年12月份以来不少银行纷纷降息,随后更是引发了一波提前还款的大潮。不少地区楼市相关的微信小程序也自带了贷款计算器、提前还款计算器等工具,通过这些工具人们可以很容易的了解每期还款本金、等额本金/本息的利息差异、提前还款节省利息的问题。

了解这些计算工具的相关原理,可以做到心中有数,临危不慌。

注:

本文对应代码和脚本发布至华为云生态社区AI Gallery:贷款计算器-从原理、公式到提前还款和可视化→AI Gallery_Notebook详情_开发者_华为云,欢迎开发者前往体验,文中涉及所有代码可以直接通过页面进入Model Arts Code Lab运行。使用该脚本稍加修改后即可尝试开发一个适合自身地区政策的贷款计算&提前还款小程序。本文只是研究贷款生成、提前还贷方面的相关计算原理,不构成任何投资理财方面的建议。先来看个使用工具计算提前还款的效果

先来看个使用工具计算提前还款的效果,再来探讨相关原理。

贷款87.5万,利率4.9%还20年,使用等额本息的方式,在第13个月提前还10万,使用月供不变年限缩短方式,在CodeLab中运行下述程序后可以计算提前还款节省利息,和微信上提前还款小程序计算结果一致:

a, b, c, d, e = extraPaidWithFixedPaid(12 * 20, 875000, 0.049, False, [(13, 100000)])drawDiagramExtraPaid(12 * 20, a, b, d, e)drawTableExtraPaid(12 * 20, a, b, c, d, e)[10:20]

如图,缩短了40个月(3年4个月)账期,次月还款额以及节省利息等都与小程序计算一致。

如何计算利息背景:等额本金和等额本息的共同点

了解过贷款的小伙伴都知道,贷款有等额本金和等额本息这两种方式,前者每月还款的本金相同,利息逐月递减;后者每月还款额相同,刚开始还款时利息还的多,后面本金还的逐渐增多。参考网上讨论利息计算的诸多文章,两个模型理论上,都有下列共同特点:

利息按月利率计算,一月一期按期还款情况下当月应还利息只由未还完的本金决定每月还款额除了未还本金产生的全部利息外,剩下的金额应该全部用于偿还本金

像最近部分银行提出的先息后本(先还利息若干年,最后一次性偿还本金)则不符合这个条件。

还款额的计算

知乎文章为什么买房贷款,最好选择等额本金?中提到了一个例子:

前阵子,院长有位朋友在惠州买了套120平米的房,总价125万左右,大约贷了87.5万。

办房贷的时候,他听从销售的建议,选了【等额本息】的还款方式。每个月固定还5726.39元。这个还款额度在他的承受范围之内,因此就选了。

那假如选择等额本金呢?第一个月要还的金额为7218.75元,此后每个月少还14.89元,直至20年后还完。

通过描述可知,贷款87.5万,贷20年,等额本息每月还款5726.39元,等额本金首月还款7218.75元。假设文中的贷款未使用公积金,计算时利率为固定利率,根据网上的贷款计算器可知此时的贷款年利率为4.9%。

以这个例子为例,简单说明等额本金和等额本息的计算方法:

首先贷20年,按月分期,贷款为

20∗12=240期。

年利率4.9%,月利率为

0.049÷12=0.004983 即0.4083%。

等额本金 情况下:

每月应还本金=总本金÷期数

每月应还利息=剩余本金×月利率

每月还款额=每月应还本金+每月应还利息

在这个例子中:

每月应还本金为

875000÷240=3645.83元首月应还利息为

875000×0.4083元首月应还:

3645.83+3572.92=7218.75元。第2月剩余本金为

875000−3645.83=871354.17元。第2月应还利息为

871354.17×0.4083元。第2月应还:

3645.83+3558.03=7203.86元。

将这段逻辑抽象为代码有:

import matplotlib.pyplot as pltimport numpy as npdef averageCapital(months, principal, rate):    month_rate = rate / 12    monthly_capital = principal / months    interests = [0] * months    capitals = [0] * months    left_principal = [0] * months    left_principal[0] = principal    total_payment = [0] * months    for i in range(0, months):        interests[i] = left_principal[i] * month_rate        capitals[i] = monthly_capital        total_payment[i] = monthly_capital + interests[i]        if i + 1 < months:            left_principal[i + 1] = left_principal[i] - monthly_capital    return capitals, interests, total_payment

为了便于查看再封装一个打印成表格的函数:

import pandas as pddef drawTable(months, fn, *args, **kwargs):    capitals, interests, total_payment = fn(months, *args, **kwargs)    paid_capital = [0] * months    paid_interests = [0] * months    paid_capital[0] = capitals[0]    paid_interests[0] = interests[0]    for x in range(1, months):        paid_capital[x] = paid_capital[x - 1] + capitals[x]        paid_interests[x] = paid_interests[x - 1] + interests[x]    origin = pd.DataFrame([total_payment, capitals, interests, paid_capital, paid_interests])    return pd.DataFrame(origin.values.T, columns=['还款额','还款本金','还款利息','已还本金','已还利息'], index=np.arange(1, months + 1))

我们运行一下知乎上的例子,看看头几年还款的本金、利息等:

pd.options.display.float_format = '{:.2f}'.formatdrawTable(12 * 20, averageCapital, 875000, 0.049)[0:10]

可以看到和文中描述一致,使用微信房小团小程序,也可以打印出一致的结果。

等额本息 的计算方法有些复杂,参考用Python深度解读房贷利率文中的解法,设A为本金,第i个月月末所欠银行本金为Ai,每月所还贷款总额为X,月利率为β, 则有:

由于最后一期时剩余本金为0,可反解得:

这里m为总期数(在刚刚的例子中,m=240)。而后就可以使用与等额本金计算中类似的逻辑,从第一期所还利息开始,反推每期的利息与本金。具体代码如下:

def averageCapitalPlusInterest(months, principal, rate):    month_rate = rate / 12    monthly_payment = principal * month_rate * (1 + month_rate) ** months / ((1 + month_rate) ** months - 1)    interests = [0] * months    capitals = [0] * months    left_principal = [0] * months    left_principal[0] = principal    total_payment = [0] * months    for i in range(0, months):        total_payment[i] = monthly_payment        interests[i] = left_principal[i] * month_rate        capitals[i] = total_payment[i] - interests[i]        if i + 1 < months:            left_principal[i + 1] = left_principal[i] - capitals[i]    return capitals, interests, total_payment

我们运行一下知乎上的例子,看看等额本息模式下第8年附近,到底还了多少利息和本金:

drawTable(12 * 20, averageCapitalPlusInterest, 875000, 0.049)[90:100]

可以看到第96期(第8年年终)时,本金还了25万,但利息已经还了近30万了,和之前文中例子的数据是可以对得上的。

还款可视化

刚刚我们已经将还款的各项数据以表格的形式打印。此外我们还可以借助python的能力,打印还款的柱状图。

import numpy as npdef printStatistics(capitals, interests, total_payment, months):    print("总本金:" + str(np.sum(capitals)))    print("总利息:" + str(np.sum(interests)))    print("总利息/总本金" + str(np.sum(interests)/np.sum(capitals)))    print("首月还款 %.2f 末月还款: %.2f" % (total_payment[0], total_payment[months - 1]))def drawDiagram(months, fn, *args, **kwargs):    capitals, interests, total_payment = fn(months, *args, **kwargs)    printStatistics(capitals, interests, total_payment, months)    month_array = np.arange(1, months + 1, 1)    height = interests    plt.bar(month_array, capitals, width=0.2, align='center', color='red')    plt.bar(month_array, interests, width=0.2, align='center', color='blue', bottom=capitals)    plt.show()

再跑一下知乎的例子,绘制等额本金和等额本息的还款柱状图:

drawDiagram(12 * 20, averageCapital, 875000, 0.049)

如图,蓝色是所还利息,红色是所还本金。可以看出本金每月不变,利息逐月递减的特征。

等额本息情况下:

drawDiagram(12 * 20, averageCapitalPlusInterest, 875000, 0.049)

也能看出所绘图形和等额本息的含义基本一致。

另外部分城市可以公积金贷款,以杭州为例,目前杭州公积金充足情况下可贷50w-60w,这里考虑一下公积金的情况:

def averageCapitalWithPublicFund(months, principal1, rate1, principal2, rate2):    a, b, c = averageCapital(months, principal1, rate1)    a1, b1, c1 = averageCapital(months, principal2, rate2)    return np.sum([a,a1],axis=0).tolist(), np.sum([b,b1],axis=0).tolist(), np.sum([c,c1],axis=0).tolist()
drawTable(12 * 20, averageCapitalWithPublicFund, 700000, 0.041, 300000, 0.031)[0:10]

这里算了下商贷70w(利率4.1%),公积金贷30w(利率3.1%)下组合贷款的情况,和微信小程序房小团的计算是一致的。

提前还款相关原理

再来讨论下提前还款。如果知乎文中买房的那位,在贷款1年后提前还款10w会怎样呢?了解一点背景知识的朋友,都知晓提前还款分两种情况:

年限不变,月供减少年限缩短,月供不变

现在分情况讨论,并给出计算函数。

注:notebook中所有计算结果均在微信房小团小程序上得到互相验证。

年限不变,月供减少

这种情况下,相当于在提前还款月之后重新做了一次贷款。我们首先对刚刚的计算函数进行一定的简化,抽象一下公共的部分。

def normalPaid(months, principal, rate, capitalAveraged):    month_rate = rate / 12    monthly_capital = principal / months    monthly_payment = principal * month_rate * (1 + month_rate) ** months / ((1 + month_rate) ** months - 1)    interests = [0] * months    capitals = [0] * months    left_principal = [0] * months    left_principal[0] = principal    total_payment = [0] * months    for i in range(0, months):        interests[i] = left_principal[i] * month_rate        if capitalAveraged:            capitals[i] = monthly_capital            total_payment[i] = monthly_capital + interests[i]        else:            total_payment[i] = monthly_payment            capitals[i] = total_payment[i] - interests[i]        if i + 1 < months:            left_principal[i + 1] = left_principal[i] - capitals[i]    return capitals, interests, total_payment
drawTable(12 * 20, normalPaid, 875000, 0.049, False)[10:14]
drawTable(12 * 20, normalPaid, 875000, 0.049, True)[10:14]

可以看到抽象出公共结构后,前后的计算结果并没有发生变化。

考虑年限不变提前还款的情况,这里将每次提前还款的时间和金额组成python的元组,若干个(账期,还款金额)元组组成一个list输入函数。函数首先计算正常情况下的还款信息,而后根据提前还款信息,修改提前还款日的剩余本金,并从各个提前还款日重新计算剩余还款。

def extraPaidWithFixedPeriod(months, principal, rate, capitalAveraged, extraPaidList :list):    capitals, interests, total_payment = normalPaid(months, principal, rate, capitalAveraged)    extraPaidList.sort(key=lambda x:x[0])    originCapital, originInterests, originTotal = capitals.copy(), interests.copy(), total_payment.copy()    left_principal = [0] * months    left_principal[0] = principal    for x in range(0,months):        if x < months - 1:            left_principal[x + 1] = left_principal[x] - capitals[x]    def normalPaidOffset(left_months, principal, rate, capitalAveraged, offset):        month_rate = rate / 12        monthly_capital = left_principal[offset] / left_months        monthly_payment = left_principal[offset] * month_rate * (1 + month_rate) ** left_months / ((1 + month_rate) ** left_months - 1)        for i in range(0, left_months):            interests[offset + i] = left_principal[offset + i] * month_rate            if capitalAveraged:                capitals[offset + i] = monthly_capital                total_payment[offset + i] = monthly_capital + interests[offset + i]            else:                total_payment[offset + i] = monthly_payment                capitals[offset + i] = total_payment[offset + i] - interests[offset + i]            if i == 0:                print("次月还款 %.2f" % total_payment[offset + i])            if offset + i + 1 < months:                left_principal[offset + i + 1] = left_principal[offset + i] - capitals[offset + i]        return    for x,y in extraPaidList:        capitals[x] = capitals[x] + y        left_principal[x + 1] = left_principal[x] - capitals[x]        total_payment[x] = capitals[x] + interests[x]        print("当月需还 %.f 剩余本金 %.f" %(total_payment[x], left_principal[x + 1]))        normalPaidOffset(months - x - 1, left_principal[x + 1], rate, capitalAveraged, x + 1)    printStatistics(originCapital, originInterests, originTotal, months)    print("")    printStatistics(capitals, interests, total_payment, months)    print("节省利息 %.2f" % (np.sum(originInterests) - np.sum(interests)))    return capitals, interests, total_payment, originTotal, originInterests

再定义几个函数对提前还款节省的利息进行可视化。

def drawDiagramExtraPaid(months, capitals, interests, originalTotal, originalInterests, showOriginTotal=True):    month_array = np.arange(1, months + 1, 1)    capital_with_origin_interest = [0] * months    height = interests    for x in range(1, months):        capital_with_origin_interest[x] = capitals[x] + originalInterests[x]    l1 = plt.bar(month_array, originalTotal if showOriginTotal else capital_with_origin_interest, width=0.2, align='center', color='yellow')    l2 = plt.bar(month_array, capitals, width=0.2, align='center', color='red')    l3 = plt.bar(month_array, interests, width=0.2, align='center', color='blue', bottom=capitals)    # plt.legend(handles = [l1, l2,l3], labels = ['每月少还' if showOriginTotal else '节省利息', '本金','利息'], loc = 'best',fontsize=20)    plt.ylim(0, (capitals[0]+interests[0])*1.1)    plt.show()def drawTableExtraPaid(months, capitals, interests, total_payment, originalTotal, originalInterests):    paid_capital = [0] * months    paid_interests = [0] * months    saved_money = [0] * months    paid_capital[0] = capitals[0]    paid_interests[0] = interests[0]    for x in range(1, months):        paid_capital[x] = paid_capital[x - 1] + capitals[x]        paid_interests[x] = paid_interests[x - 1] + interests[x]        saved_money[x] = saved_money[x - 1] + (originalInterests[x] - interests[x] )    origin = pd.DataFrame([total_payment, capitals, interests, paid_capital, paid_interests,saved_money])    return pd.DataFrame(origin.values.T, columns=['还款额','还款本金','还款利息','已还本金','已还利息','累计节省'], index=np.arange(1, months + 1))

通过参数showOriginTotal的取值,可以分别绘制每月少还的钱与当月节省利息的情况。下面分别绘制了等额本金和等额本息情况下,87.5万贷20年,在第一年还10万后还款和利息的变化情况。

a, b, c, d, e = extraPaidWithFixedPeriod(12 * 20, 875000, 0.049, True, [(13,100000)])drawDiagramExtraPaid(12 * 20, a, b, d, e)drawDiagramExtraPaid(12 * 20, a, b, d, e, False)drawTableExtraPaid(12 * 20, a, b, c, d, e)[10:20]
a, b, c, d, e = extraPaidWithFixedPeriod(12 * 20, 875000, 0.049, False, [(13,100000)])drawDiagramExtraPaid(12 * 20, a, b, d, e)drawDiagramExtraPaid(12 * 20, a, b, d, e, False)drawTableExtraPaid(12 * 20, a, b, c, d, e)[10:20]

可以很方便地看出节省利息在每个月还款额中的比重。

月供不变,年限缩短

这种情况下提前还款导致后续每个月产生的利息少了,但是月供没变,相当于后续每个月额外多还了本金。但是在各类提前还款计算器的计算中,月供并不是和之前相同的,经过反复的计算后和网上的贷款计算器结果最终一致,发现各类提前还款计算器隐含了下列约束:

提前还款相当于用剩余本金新做一个贷款。“月供”不是真的不变。而是通过缩短年限方式,使得新贷款首月月供尽可能和当前月供相当。如果是等额本金模式,新贷款首月月供中,偿还本金并未增多,需要略低于上月偿还本金,等额本息模式则无此约束。

想想这个逻辑也有道理,如果真的“月供不变”,那么等额本金模式下提前还款后,后续每个月偿还的本金都会比新做贷款的偿还的本金多,相当于后续每个月都在提前还款,后续每个月月供本金就不能称为“等额”了。

我们下面先写个求解首月月供的函数,以及通过缩短年限逼近上月月供总额和月供本金的函数。而后计算“月供不变,年限缩短”模式下节省的具体利息。

def getFirstPaid(months, principal, rate, capitalAveraged):    month_rate = rate / 12    monthly_capital = principal / months    monthly_payment = principal * month_rate * (1 + month_rate) ** months / ((1 + month_rate) ** months - 1)    interests1 = principal * month_rate    if capitalAveraged:        return monthly_capital + interests1, monthly_capital    else:        return monthly_payment, monthly_payment - interests1
def getLeftMonths(leftMonthsMax, capitalPaidMax, paidMax, leftPrincipal, rate, capitalAveraged):    lastPaid, lastCapitalPaid, lastMonths = 0, 0, 0    for i in range(leftMonthsMax, 1, -1):        paid, capitalPaid = getFirstPaid(i, leftPrincipal, rate, capitalAveraged)        if paid > paidMax or (capitalAveraged and capitalPaid > capitalPaidMax):            return lastMonths, lastPaid, lastCapitalPaid        else:            lastPaid, lastCapitalPaid, lastMonths = paid, capitalPaid, i
def extraPaidWithFixedPaid(months, principal, rate,                           capitalAveraged, extraPaidList: list):    capitals, interests, total_payment = normalPaid(        months, principal, rate, capitalAveraged)    extraPaidList.sort(key=lambda x: x[0])    originCapital, originInterests, originTotal = capitals.copy(), interests.copy(), total_payment.copy()    left_principal = [0] * months    left_principal[0] = principal    for x in range(0, months):        if x < months - 1:            left_principal[x + 1] = left_principal[x] - capitals[x]    def normalPaidOffset(left_months, principal, rate,                         capitalAveraged, offset, left_months2):        month_rate = rate / 12        monthly_capital = left_principal[offset] / left_months        monthly_payment = left_principal[offset] * month_rate * (1 + month_rate) ** left_months / ((1 + month_rate) ** left_months - 1)        for i in range(0, left_months):            interests[offset + i] = left_principal[offset + i] * month_rate            if capitalAveraged:                capitals[offset + i] = monthly_capital                total_payment[offset + i] = monthly_capital + interests[offset + i]            else:                total_payment[offset + i] = monthly_payment                capitals[offset + i] = total_payment[offset + i] - interests[offset + i]            if i == 0:                print("次月还款 %.2f" % total_payment[offset + i])            if offset + i + 1 < months:                left_principal[offset + i + 1] = left_principal[offset + i] - capitals[offset + i]        for i in range(left_months, left_months2):            interests[offset + i] = 0            capitals[offset + i] = 0            total_payment[offset + i] = 0        return    realMonth = months    for x, y in extraPaidList:        capitalParam = capitals[x]        capitals[x] = capitals[x] + y        left_principal[x + 1] = left_principal[x] - capitals[x]        total_payment[x] = capitals[x] + interests[x]        maxMonth, maxPaid, maxPaidCapital = getLeftMonths(months - x - 1, capitalParam, total_payment[x - 1], left_principal[x + 1], rate, capitalAveraged)        normalPaidOffset(maxMonth, left_principal[x + 1], rate, capitalAveraged, x + 1, months - x - 1)        realMonth = x + 1 + maxMonth        print("当月需还 %.2f 剩余本金 %.2f 下月需还:%.2f  原本剩余账期:%d,当前剩余账期:%d, 账期缩短:%d" %(total_payment[x], left_principal[x + 1],total_payment[x + 1], months - x - 1,maxMonth, months - x - 1 - maxMonth))    printStatistics(originCapital, originInterests, originTotal, months)    print("")    printStatistics(capitals, interests, total_payment, realMonth)    print("节省利息 %.2f" % (np.sum(originInterests) - np.sum(interests)))    return capitals, interests, total_payment, originTotal, originInterests
a, b, c, d, e = extraPaidWithFixedPaid(12 * 20, 875000, 0.049, True, [(13, 100000)])drawDiagramExtraPaid(12 * 20, a, b, d, e)drawTableExtraPaid(12 * 20, a, b, c, d, e)[10:20]
a, b, c, d, e = extraPaidWithFixedPaid(12 * 20, 875000, 0.049, False, [(13, 100000)])drawDiagramExtraPaid(12 * 20, a, b, d, e)drawTableExtraPaid(12 * 20, a, b, c, d, e)[10:20]

可以看出,虽然缩短年限的本质也是重新做一次贷款,但确实可以节省很多利息。

小结

本文初稿写于华为云AI-Gallery贷款计算器-从原理、公式到提前还款和可视化,通过页面进入CodeLab可以直接在界面上调整参数进行房贷利息、提前还款等相关计算,计算过程原理直观,配合可视化方便理解,欢迎开发者前往体验。

整篇文章带大家了解了不同房贷贷款方式的差异,以及对房贷利息计算、提前还款的原理做了较为细致的剖析和数据可视化。后续在面对贷款利息计算的问题时,可以直面原理、心中有数、临危不慌。

参考资料

[1]用Python深度解读房贷利率()

[2]为什么买房贷款,最好选择等额本金?

[3]杭州房小团微信小程序-贷款计算

[4]杭州房小团微信小程序-提前还款

点击下方,第一时间了解华为云新鲜技术~

华为云博客_大数据博客_AI博客_云计算博客_开发者中心-华为云

#华为云开发者联盟#

标签: #贷款等额本金利息计算器 #贷款等额本金利息计算器app #等额本金房贷计算器2020 #等额本金房贷计算器2022最新版本 #等额本金房贷计算器2022年最新版