1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > LPR和固定利率房贷的动态差值计算

LPR和固定利率房贷的动态差值计算

时间:2019-02-19 00:10:22

相关推荐

LPR和固定利率房贷的动态差值计算

以下是源代码。兄弟姐妹们自己跑一下,心里就有底了。

# -*- coding: utf-8 -*-# @Time : 20/2/27 11:14# @Author : Jay Lam# @File : LPR_Calculator.py# @Software: PyCharmimport mathimport randomimport numpy as np# LPR计算def LPRCalculator():currentLPR = round(float(input("输入更改合同时的LPR利率(%):")), 2)currentInterest = round(float(input("输入更改合同时的固定利率(%):")), 2)totalMonth = int(input("输入贷款总月数:"))residueMonth = int(input("输入剩余贷款月数:"))paymentPlan = int(input("输入还款方式。等额本金输入0,等额本息输入1:"))rangeLimit = int(input("输入是否限制未来LPR利率预测。不限制0,减少输入-1,增加输入1:"))totalLoan = round(float(input("输入贷款总额(元):")), 2)bp = currentInterest - currentLPR # 基点计算changeTimes = math.floor((totalMonth - residueMonth) / 12) # 一年允许改变一次利率if paymentPlan == 0:oldTotalPayment = averageCapital(totalLoan, 1, currentInterest, totalMonth)[0]oldResidueAmount = oldTotalPayment - averageCapital(totalLoan, 1, currentInterest, totalMonth)[1][0:(totalMonth - residueMonth)].sum()print(oldResidueAmount)print("LPR和原计划支出总额差为:",round(LPR(changeTimes, bp, rangeLimit, totalLoan, residueMonth, currentLPR)[0] - oldResidueAmount, 2),"元")elif paymentPlan == 1:oldTotalPayment = averageCapitalPlusInterest(totalLoan, 1, currentInterest, totalMonth)[0]oldResidueAmount = averageCapitalPlusInterest(totalLoan, 1, currentInterest, totalMonth)[1]*residueMonthprint("LPR和原计划支出总额差为:",round(LPR(changeTimes, bp, rangeLimit, totalLoan, residueMonth, currentLPR)[1] - oldResidueAmount, 2),"元")# 随机预测LPRdef LPR(changeTimes, bp, rangeLimit, totalLoan, resMonth, currentLPR):plan0 = []plan1 = []if rangeLimit == 0:for i in range(1, changeTimes):actualRatio = random.random() + bpcurrentYearPayment0 = averageCapital(totalLoan, 1, actualRatio, resMonth)[1][0:12].sum()currentYearPayment1 = averageCapitalPlusInterest(totalLoan, 1, actualRatio, resMonth)[1] * 12# 等额本息法直接每个月乘以12获得年度支出值plan0.append(currentYearPayment0)plan1.append(currentYearPayment1)actualTotalPayment0 = np.array(plan0).sum()actualTotalPayment1 = np.array(plan1).sum()elif rangeLimit == 1:actualRatio = randAscending(changeTimes, currentLPR + 0.15, currentLPR - 0.15) # 未来变化幅度不超过签约时LPR±0.15for i in range(0, len(actualRatio)):currentYearPayment0 = averageCapital(totalLoan, 1, actualRatio[i], resMonth)[1][0:12].sum()currentYearPayment1 = averageCapitalPlusInterest(totalLoan, 1, actualRatio[i], resMonth)[1] * 12plan0.append(currentYearPayment0)plan1.append(currentYearPayment1)actualTotalPayment0 = np.array(plan0).sum()actualTotalPayment1 = np.array(plan1).sum()elif rangeLimit == -1:actualRatio = randDecending(changeTimes, currentLPR + 0.15, currentLPR - 0.15)for i in range(0, len(actualRatio)):currentYearPayment0 = averageCapital(totalLoan, 1, actualRatio[i], resMonth)[1][0:12].sum()currentYearPayment1 = averageCapitalPlusInterest(totalLoan, 1, actualRatio[i], resMonth)[1] * 12plan0.append(currentYearPayment0)plan1.append(currentYearPayment1)actualTotalPayment0 = np.array(plan0).sum()actualTotalPayment1 = np.array(plan1).sum()return round(actualTotalPayment0, 2), round(actualTotalPayment1, 2)# 生成升序随机数序列def randAscending(changetimes, uplimit, lowlimit):se = []for _ in range(changetimes):se.append(round(random.uniform(lowlimit, uplimit), 4))se.sort(reverse=False)return se# 生成降序随机数序列def randDecending(changetimes, uplimit, lowlimit):se = []for _ in range(changetimes):se.append(round(random.uniform(lowlimit, uplimit), 4))se.sort(reverse=True)return se# 等额本息法def averageCapitalPlusInterest(principal, principalRatio, anualInterestRate, month):# principal表示房价总额,principalRatio表示房贷百分比,anualInterestRate表示房贷年利率,month表示房贷月份mortgage = np.around(principal * principalRatio, 2) # 计算需要贷款的金额monthlyPayment = np.around(mortgage * averageCapitalPlusInterestRate(month, monthlyInterestRate(anualInterestRate)),2) # 计算每月应还款金额totalInt = np.around(totalInterest(mortgage, monthlyPayment, month), 2)mortgageIntoTenThousand = np.around(mortgage / 10000, 2) # 将贷款总金额转化为万元print("贷款:", mortgageIntoTenThousand, "万元") # 输出结果print("年利率:", anualInterestRate, "%")print("按揭月数:", month, "月")print("每月应还:", monthlyPayment, "元") # 输出结果print("需付利息:", totalInt, "元")totalPayment = np.around(monthlyPayment * month, 2) # 还款总额return totalPayment, monthlyPayment# 等额本息法计算每月利率def monthlyInterestRate(anualInterestRate):return (anualInterestRate / 12) / 100 # 年利率/12# 等额本息法计算比例系数def averageCapitalPlusInterestRate(month, monthlyInterestRate):R = monthlyInterestRateN = monthI = R * math.pow(1 + R, N) / (math.pow(1 + R, N) - 1)return Idef totalInterest(mortgage, monthlyPayment, month):return monthlyPayment * month - mortgage# 等额本金法def averageCapital(principal, principalRatio, anualInterestRate, month):# principal表示房价总额,principalRatio表示房贷百分比,anualInterestRate表示房贷年利率,month表示房贷月份monthlyPayment = np.zeros(month) # 初始化每月还款金额cont = 0mortgage = 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 * monthlyInterestRate2(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, monthlyPayment# 等额本金法计算每月利率def monthlyInterestRate2(anualInterestRate):return (anualInterestRate / 12) / 100 # 年利率/12if __name__ == "__main__":#print(LPR(25, 0.833, -1, 2730000, 300, 4.9))#print(averageCapitalPlusInterest(2000000,1,4.3,360))LPRCalculator()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。