在2021年年初,G购房者向H银行申请金额为500万元、期限为5年的住房按揭贷款,并采用等额本还款规则进行逐月还款, 住房按揭贷款的年利率是化6%,依次计算每月还款的总金额、每月偿还的利息金额、每月偿还的本金金额。
想问等额本金方式下怎么用numpy_financial里的函数进行计算
对于等额本金方式,可以使用以下代码使用numpy_financial库进行计算:
望采纳🥰🥰🥰
import numpy_financial as npf
P = 500 * 10 ** 4 # 贷款总金额,单位:元
n = 5 * 12 # 还款期限,单位:月
r = 0.06 # 年利率
p = P / (n) # 每月还款本金
# 计算每月偿还利息和本金
interest_payment = [(P - (i * p)) * r / 12 for i in range(n)]
principal_payment = [p] * n
# 计算每月还款额
monthly_payment = npf.pmt(r / 12, n, p)
# 输出结果
print(f"每月还款额:{monthly_payment:.2f} 元")
for i in range(n):
print(f"第{i+1}个月,偿还利息:{interest_payment[i]:.2f} 元,偿还本金:{principal_payment[i]:.2f} 元")
上述代码中,首先定义了贷款总金额P、还款期限n和年利率r。然后,根据等额本金的计算公式,计算了每月还款本金p,以及每月偿还的利息和本金。接着,使用numpy_financial库的pmt函数计算每月还款额。最后,输出了每月还款额以及每个月的偿还利息和本金。
等额本金应该比较好算,直接贴现公式计算就好了。可以不借助库应该也能写出来,不过网上也有很多用该库的代码
#房贷等额本金算法
def averageCapital(principal,principalRatio,anualInterestRate,month):
#principal表示房价总额,principalRatio表示房贷百分比,anualInterestRate表示房贷年利率,month表示房贷月份
monthlyPayment = np.zeros(month) #初始化每月还款金额
cont = 0
mortgage = np.around(principal*principalRatio,2) #计算需要贷款的金额
mortgageIntoTenThousand = np.around(mortgage/10000,2) #将贷款总金额转化为万元
print("贷款:",mortgageIntoTenThousand,"万元") #输出结果
print("年利率:",anualInterestRate,"%")
print("按揭月数:",month,"月")
for i in range(0,month):
monthlyPayment[i] = np.around((mortgage/month)+((mortgage)*monthlyInterestRate(anualInterestRate)) - (i*(mortgage/month)*monthlyInterestRate(anualInterestRate)),2)
#每月还款金额
cont += np.around(monthlyPayment[i],2)
print("第",i+1,"月应还款:",monthlyPayment[i],"元") #输出结果
totalPayment = np.around(sum(monthlyPayment),2) #计算还款总额
print("还款总额:",totalPayment,"元")
return totalPayment
#计算每月利率
def monthlyInterestRate(anualInterestRate):
return (anualInterestRate/12)/100 #年利率/12
#直接
averageCapital(500*10000,1,0.06/12,12*5)
可以试试这个