天天看點

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