天天看点

PythonNote017---计算房贷还款

等额本息还款

  选择等额本息还款方式时,每个月还款金额是固定的,此时每个月还款的利息和本金都是变化的。介绍几个定义:

  • 年利率,银行约定的贷款年利率,如果商贷4.9%
  • 月利率= 年利率/12
  • 代还本金=总本金-已还本金
  • 当期应还利息 = 代还本金​

    ​*​

    ​月利率
  • 当期应还本金 = 固定额 - 当期应还利息

第一期时:

  • 1st应还利息 = 总本金​

    ​*​

    ​月利率
  • 1st应还本金 = 固定额 - 当期应还利息

    第二期时:

  • 2nd应还利息 = (总本金-第一期已还本金)​

    ​*​

    ​月利率
  • 2nd应还本金 = 固定额 - 第二期应还利息

所以,需要求这个固定额,直接看百度百科的证明:​​https://baike.baidu.com/item/%E7%AD%89%E9%A2%9D%E6%9C%AC%E6%81%AF%E8%BF%98%E6%AC%BE%E6%B3%95#3​​

PythonNote017---计算房贷还款

等额本金

等额本金即每个月还款本金相同,如贷款360w,30年,那么每个月还款本金均为360w/30/12=1w,每个月应还利息和等额本息计算方式一致,不多说。

Python计算

写个简单的函数,输出每期还款数据

import pandas as pd
pd.set_option('display.max_rows', 500) # 打印最大行数
pd.set_option('display.max_columns', 500) # 打印最大列数
def RepaymentCalculator(Loans, Year, YearRate, Type="等额本息"):
    """
    Loans:贷款总额
    Year: 贷款期限,单位年
    YearRate:贷款年利率
    Type: 还款方式,"等额本金"or"等额本息"
    """
    Month = Year * 12  #贷款总月数
    MonthRate = YearRate / 12
    if (Type == "等额本息"):
        # 先求每个月固定还款额
        FixedPayment = (Loans * MonthRate * ((1 + MonthRate)**Month)) / (
            (1 + MonthRate)**Month - 1)
    elif (Type == "等额本金"):
        FixedPayment = Loans / Month

    repayMonthIndex = []
    repayMonthPrincipal = []
    repayMonthInterest = []
    unpayPrincipal = []
    #剩余本金
    UnpaidPrincipal = Loans
    if (Type == "等额本息"):
        for i in range(Month):
            # 本月代还本金
            unpayPrincipal.append(UnpaidPrincipal)
            repayMonthIndex.append(i + 1)
            # 先计算当月利息
            thisMonthInterest = UnpaidPrincipal * MonthRate
            repayMonthInterest.append(thisMonthInterest)
            # 再计算当月本金
            thisMonthPrincipal = FixedPayment - thisMonthInterest
            repayMonthPrincipal.append(thisMonthPrincipal)
            # 最后更新代还本金
            UnpaidPrincipal = UnpaidPrincipal - thisMonthPrincipal
    elif (Type == "等额本金"):
        for i in range(Month):
            # 本月代还本金
            unpayPrincipal.append(UnpaidPrincipal)
            repayMonthIndex.append(i + 1)
            # 先计算当月利息
            thisMonthInterest = UnpaidPrincipal * MonthRate
            repayMonthInterest.append(thisMonthInterest)
            # 再计算当月本金,等额本金不变哦
            thisMonthPrincipal = FixedPayment
            repayMonthPrincipal.append(thisMonthPrincipal)
            # 最后更新代还本金
            UnpaidPrincipal = UnpaidPrincipal - thisMonthPrincipal
    # 生成dataframe
    res = pd.DataFrame({
        "还款期数": repayMonthIndex,
        "未还本金": unpayPrincipal,
        "还款本金": repayMonthPrincipal,
        "还款利息": repayMonthInterest
    })
    res["还款总额"] = res["还款本金"]+res["还款利息"]
    # 调整小数位数
    res = res.round(2)
    return      

假设商贷100w,贷款30年,年利率4.9%,选择等额本息的还款方式,那么每个月需要还多少钱呢~

df = RepaymentCalculator(Loans=1000000, Year=30, YearRate=0.049, Type="等额本息")
df.head()      
还款期数 未还本金 还款本金 还款利息 还款总额
1 1000000.00 1223.93 4083.33 5307.27
1 2 998776.07 1228.93 4078.34 5307.27
2 3 997547.13 1233.95 4073.32 5307.27
3 4 996313.18 1238.99 4068.28 5307.27
4 5 995074.20 1244.05 4063.22 5307.27

可以看到每个月固定还款5307.27,因为未还本金越来越少,每一期的还款利息也越来越少

def alreadyRepay(repayYear,df):
    """
    repayYear:已还年数
    """
    alreadyRepayMonths = repayYear *12
    # 先筛选子集  
    subDf = df.query("还款期数<=%s"%alreadyRepayMonths)
    # 计算对应指标  
    ## 剩余还款期数
    unRepayMonths = df.shape[0] - alreadyRepayMonths
    ## 未还本金  
    unRepayPrincipal = df.还款本金.sum() - subDf.还款本金.sum()
    ## 已还本金
    alreadyRepayPrincipal= subDf.还款本金.sum()
    ## 已还利息 
    alreadyRepayInterest= subDf.还款利息.sum()
    print("====================== 已还%s期======================"%alreadyRepayMonths)
    print("剩余还款期数: "+str(unRepayMonths)+"\n"+"剩余未还本金: "+str(unRepayPrincipal)+"\n"+"已还本金: "+str(alreadyRepayPrincipal
)+"\n"+"已还利息: "+str(alreadyRepayInterest)+"\n")      
alreadyRepay(5,df)      
====================== 已还60期======================
剩余还款期数: 300
剩余未还本金: 916977.14
已还本金: 83022.9
已还利息: 235413.12      
alreadyRepay(10,df)      
====================== 已还120期======================
剩余还款期数: 240
剩余未还本金: 810958.17
已还本金: 189041.87
已还利息: 447830.19