loan python实现

import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

def averageCapitalInterest(monthRate, total, monthNum):
    x = total * math.pow(1 + monthRate, monthNum) * monthRate / (math.pow(1 + monthRate, monthNum) - 1)
    debtRest = [0] * (monthNum + 1)
    refundPerMonth = [x] * (monthNum + 1)
    refundInterestPerMonth = [0] * (monthNum + 1)
    refundCapitalPerMonth = [0] * (monthNum + 1)

    debtRest[0] = total
    refundPerMonth[0] = 0
    for monthSeq in range(1, monthNum + 1, 1):
        debtRest[monthSeq] = total * math.pow(1 + monthRate, monthSeq) - x * (math.pow(1 + monthRate, monthSeq) - 1) / monthRate
        refundInterestPerMonth[monthSeq] = debtRest[monthSeq - 1] * monthRate
        refundCapitalPerMonth[monthSeq] = x - refundInterestPerMonth[monthSeq]

    totalInterest = sum(refundInterestPerMonth)
    totalRefund = total + totalInterest
    capitalRest = debtRest

    assert((totalRefund-1<sum(refundPerMonth) and totalRefund+1>sum(refundPerMonth)) == True)

    return [totalRefund, totalInterest, debtRest, capitalRest, refundPerMonth, refundInterestPerMonth, refundCapitalPerMonth]

def averageCapital(monthRate, total, monthNum):
    debtRest = [0] * (monthNum + 1)
    refundPerMonth = [0] * (monthNum + 1)
    refundInterestPerMonth = [0] * (monthNum + 1)
    refundCapitalPerMonth = [total/monthNum] * (monthNum + 1)

    debtRest[0] = total
    refundCapitalPerMonth[0] = 0
    for monthSeq in range(1, monthNum + 1, 1):
        debtRest[monthSeq] = debtRest[monthSeq-1] - refundCapitalPerMonth[monthSeq]
        refundInterestPerMonth[monthSeq] = debtRest[monthSeq-1] * monthRate
        refundPerMonth[monthSeq] = refundCapitalPerMonth[monthSeq] + refundInterestPerMonth[monthSeq]

    totalInterest = sum(refundInterestPerMonth)
    totalRefund = total + totalInterest
    capitalRest = debtRest

    assert((totalRefund-1<sum(refundPerMonth) and totalRefund+1>sum(refundPerMonth)) == True)

    return [totalRefund, totalInterest, debtRest, capitalRest, refundPerMonth, refundInterestPerMonth, refundCapitalPerMonth]


if __name__ == "__main__":
    monthRate = 0.049*0.9/12
    total = 1800000
    monthNum = 12 * 30

    investRate = math.pow(1+0.06,1/12) -1
    # investRate = 0.08/12

    [totalRefund, totalInterest, debtRest, capitalRest, refundPerMonth, refundInterestPerMonth,refundCapitalPerMonth] = averageCapitalInterest(monthRate, total, monthNum)
    [totalRefund1, totalInterest1,debtRest1, capitalRest1, refundPerMonth1, refundInterestPerMonth1, refundCapitalPerMonth1] = averageCapital(monthRate, total, monthNum)
    print("等额本息:总共还款:"+str(totalRefund)+" 其中利息:"+str(totalInterest))
    print("等额本金:总共还款:" + str(totalRefund1) + " 其中利息:" + str(totalInterest1))
    print("等额本息比等额本金多付利息:" + str(totalRefund -totalRefund1))

    cashDiff = [0]*(monthNum + 1)
    investProfit= [0]*(monthNum + 1)
    for i in range(1, monthNum+1, 1):
        cashDiff[i] = refundPerMonth1[i] - refundPerMonth[i]
    for i in range(1, monthNum, 1):
        investProfit[i] = cashDiff[i] * math.pow(1+investRate,monthNum-i)
    print("等额本息月供比等额本金多出的现金投资收益为:" + str(sum(investProfit)))
    print("等额本息比等额本金,在还完贷款后:" + str(sum(investProfit)-(totalRefund -totalRefund1)))

    for i in range(1,monthNum+1,1):
        print("第"+str(i) +"月 等额本息:月供:"+str(refundPerMonth[i])+" 每月还本金:"+str(refundCapitalPerMonth[i])+" 剩余未还本金:"+str(debtRest[i]))
        print("第" + str(i) + "月 等额本金:月供:" + str(refundPerMonth1[i]) + " 每月还本金:" + str(refundCapitalPerMonth1[i]) + " 剩余未还本金:" + str(debtRest1[i]))


    pic = r"E:\loan.png"
    xmajorLocator = MultipleLocator(6)  # 将x主刻度标签设置为20的倍数
    xmajorFormatter = FormatStrFormatter('%d')  # 设置x轴标签文本的格式
    xminorLocator = MultipleLocator(3)  # 将x轴次刻度标签设置为的倍数

    ymajorLocator = MultipleLocator(50000)  # 将y轴主刻度标签设置为的倍数
    ymajorFormatter = FormatStrFormatter('%1.1f')  # 设置y轴标签文本的格式
    yminorLocator = MultipleLocator(25000)  # 将此y轴次刻度标签设置为的倍数

    x = np.linspace(0, monthNum, monthNum+1)
    plt.figure(1)
    ax1 = plt.subplot(111)
    plt.plot(x, debtRest, 'r')
    plt.plot(x, debtRest1, 'b')

    # 设置主刻度标签的位置,标签文本的格式
    ax1.xaxis.set_major_locator(xmajorLocator)
    ax1.xaxis.set_major_formatter(xmajorFormatter)
    ax1.yaxis.set_major_locator(ymajorLocator)
    ax1.yaxis.set_major_formatter(ymajorFormatter)
    # 显示次刻度标签的位置,没有标签文本
    ax1.xaxis.set_minor_locator(xminorLocator)
    ax1.yaxis.set_minor_locator(yminorLocator)
    ax1.xaxis.grid(True, which='major')  # x坐标轴的网格使用主刻度
    ax1.yaxis.grid(True, which='major')  # y坐标轴的网格使用次刻度

    plt.legend(["debtRestCI", "debtRest"], loc=0)
    plt.xlabel("month")
    plt.ylabel("amount")
    plt.xlim(0, monthNum)

    plt.figure(2)
    ax2 = plt.subplot(111)
    plt.plot(x, refundPerMonth, 'r')
    plt.plot(x, refundCapitalPerMonth, 'm')
    plt.plot(x, refundPerMonth1, 'c')
    plt.plot(x, refundCapitalPerMonth1, 'g')

    xmajorLocator = MultipleLocator(6)  # 将x主刻度标签设置为20的倍数
    xmajorFormatter = FormatStrFormatter('%d')  # 设置x轴标签文本的格式
    xminorLocator = MultipleLocator(3)  # 将x轴次刻度标签设置为的倍数
    ymajorLocator = MultipleLocator(500)  # 将y轴主刻度标签设置为的倍数
    ymajorFormatter = FormatStrFormatter('%1.1f')  # 设置y轴标签文本的格式
    yminorLocator = MultipleLocator(100)  # 将此y轴次刻度标签设置为的倍数

    # 设置主刻度标签的位置,标签文本的格式
    ax2.xaxis.set_major_locator(xmajorLocator)
    ax2.xaxis.set_major_formatter(xmajorFormatter)
    ax2.yaxis.set_major_locator(ymajorLocator)
    ax2.yaxis.set_major_formatter(ymajorFormatter)
    # 显示次刻度标签的位置,没有标签文本
    ax2.xaxis.set_minor_locator(xminorLocator)
    ax2.yaxis.set_minor_locator(yminorLocator)
    ax2.xaxis.grid(True, which='major')  # x坐标轴的网格使用主刻度
    ax2.yaxis.grid(True, which='major')  # y坐标轴的网格使用次刻度

    plt.legend(["refundPerMonthCI", "refundCapitalPerMonthCI", "refundPerMonth", "refundCapitalPerMonth"], loc = 0)
    plt.xlabel("month")
    plt.ylabel("amount")
    plt.xlim(0, monthNum)

    plt.show()
    plt.close(1)
    plt.close(2)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行精准营销的实现可以分为以下几个步骤: 1. 数据清洗和准备:首先需要对银行客户数据进行清洗和准备,包括删除重复数据、处理缺失值、处理异常值等。 2. 特征工程:在数据准备好之后,需要进行特征工程,即从客户数据提取有用的特征。这些特征可以包括客户的年龄、性别、收入水平、持卡情况、贷款情况等。 3. 模型选择和训练:选择合适的机器学习模型进行训练,可以使用逻辑回归、决策树、随机森林等模型。 4. 模型评估和优化:对训练好的模型进行评估,比较不同模型的性能,选择最优模型,并进行参数调整和优化。 5. 预测和营销:使用训练好的模型对新客户进行预测,预测客户是否会购买银行产品或服务。根据预测结果,制定相应的营销策略,比如向预测为购买客户发送推广信息、提供更优惠的产品或服务等。 在 Python ,可以使用 pandas 库进行数据清洗和特征工程,使用 scikit-learn 库进行模型选择、训练和评估。以下是一个简单的银行营销预测模型的示例代码: ```python import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score # 读取数据 data = pd.read_csv('bank_data.csv') # 特征工程 X = data[['age', 'income', 'card', 'loan']] y = data['buy'] # 数据集划分 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 模型训练 model = LogisticRegression() model.fit(X_train, y_train) # 模型评估 y_pred = model.predict(X_test) print('Accuracy:', accuracy_score(y_test, y_pred)) # 预测新客户 new_customer = pd.DataFrame({'age': [35], 'income': [50000], 'card': [1], 'loan': [0]}) print(model.predict(new_customer)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值