使用python计算复利

您所在的位置:网站首页 复利计息定义 使用python计算复利

使用python计算复利

2024-07-15 18:09| 来源: 网络整理| 查看: 265

目录

复利公式

利率值选取

代码

计算结果

数据展示

总结

复利公式

相信大家一定都听说过复利,复利公式非常简单 本息和=本金*(1+年利率)^年数 。

这个公式到底有多厉害呢,我们进行一个简单的计算。

利率值选取

一般我们存银行定期一年年利率为2.1%,住房公积金贷款利息为2.9%(这个是交给银行的哈哈),商业贷款利息为4.9%(你买房不用住房公积金的话就是按这个利率算),一般玩股票基金年利率有8%的话就算高收益了,如果每年10%的年利率那么你就是股神,如果有人告诉你他一年投资收益率为20%,那么你就要小心了,这个人十有八九是觊觎你口袋里的钱哈哈。

代码

下面直接上代码啦

""" 计算复利公式 """ import matplotlib.pyplot as plt #加载字体 import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["KaiTi"] mpl.rcParams["font.serif"] = ["KaiTi"] a = 200000 #这里是本金 r1 = 0.021 #这里是年利率 r2 = 0.031 r3 = 0.041 r4 = 0.051 r5 = 0.08 #这是很高的利率了,一般投资能到每年8%就是非常好了 r6 = 0.10 #这应该是超级投资大牛的战绩 x = [] y1 = [] y2 = [] y3 = [] y4 = [] y5 = [] y6 = [] #填入数据 for i in range(1, 41): x.append(i) y1.append(a*(1+r1)**i) y2.append(a*(1+r2)**i) y3.append(a*(1+r3)**i) y4.append(a*(1+r4)**i) y5.append(a*(1+r5)**i) y6.append(a*(1+r6)**i) filename1 = "利率"+str(r1)+".txt" filename2 = "利率"+str(r2)+".txt" filename3 = "利率"+str(r3)+".txt" filename4 = "利率"+str(r4)+".txt" filename5 = "利率"+str(r5)+".txt" filename6 = "利率"+str(r6)+".txt" with open(filename1, "w", encoding="utf-8") as f1: for i in range(1, 40): f1.write("第"+str(i)+"年的本息和为:"+str(y1[i-1]).split('.')[0] + '.' + str(y1[i-1]).split('.')[1][:2]+"\n") with open(filename2, "w", encoding="utf-8") as f2: for i in range(1, 40): f2.write("第"+str(i)+"年的本息和为:"+str(y2[i-1]).split('.')[0] + '.' + str(y2[i-1]).split('.')[1][:2]+"\n") with open(filename3, "w", encoding="utf-8") as f3: for i in range(1, 40): f3.write("第"+str(i)+"年的本息和为:"+str(y3[i-1]).split('.')[0] + '.' + str(y3[i-1]).split('.')[1][:2]+"\n") with open(filename4, "w", encoding="utf-8") as f4: for i in range(1, 40): f4.write("第"+str(i)+"年的本息和为:"+str(y4[i-1]).split('.')[0] + '.' + str(y4[i-1]).split('.')[1][:2]+"\n") with open(filename5, "w", encoding="utf-8") as f5: for i in range(1, 40): f5.write("第"+str(i)+"年的本息和为:"+str(y5[i-1]).split('.')[0] + '.' + str(y5[i-1]).split('.')[1][:2]+"\n") with open(filename6, "w", encoding="utf-8") as f6: for i in range(1, 40): f6.write("第"+str(i)+"年的本息和为:"+str(y6[i-1]).split('.')[0] + '.' + str(y6[i-1]).split('.')[1][:2]+"\n") #绘图 title = "本金为"+str(a)+"的时候的复利金额" plt.title(title) plt.plot(x, y1, "b^-", label="利率为"+str(r1)) plt.plot(x, y2, "k^-", label="利率为"+str(r2)) plt.plot(x, y3, "r^-", label="利率为"+str(r3)) plt.plot(x, y4, "m^-", label="利率为"+str(r4)) plt.plot(x, y5, "m-", label="利率为"+str(r6)) plt.xlabel("存储年限") plt.ylabel("本息和") #显示标签 plt.legend() #显示格子 plt.grid() #保存图片 plt.savefig("复利图.PNG") plt.show() 计算结果

年利率为2.1%时候的计算结果

第1年的本息和为:204199.99 第2年的本息和为:208488.19 第3年的本息和为:212866.45 第4年的本息和为:217336.64 第5年的本息和为:221900.71 第6年的本息和为:226560.63 第7年的本息和为:231318.40 第8年的本息和为:236176.09 第9年的本息和为:241135.79 第10年的本息和为:246199.64 第11年的本息和为:251369.83 第12年的本息和为:256648.60 第13年的本息和为:262038.22 第14年的本息和为:267541.02 第15年的本息和为:273159.38 第16年的本息和为:278895.73 第17年的本息和为:284752.54 第18年的本息和为:290732.34 第19年的本息和为:296837.72 第20年的本息和为:303071.31 第21年的本息和为:309435.81 第22年的本息和为:315933.96 第23年的本息和为:322568.58 第24年的本息和为:329342.52 第25年的本息和为:336258.71 第26年的本息和为:343320.14 第27年的本息和为:350529.87 第28年的本息和为:357890.99 第29年的本息和为:365406.70 第30年的本息和为:373080.24 第31年的本息和为:380914.93 第32年的本息和为:388914.14 第33年的本息和为:397081.34 第34年的本息和为:405420.05 第35年的本息和为:413933.87 第36年的本息和为:422626.48 第37年的本息和为:431501.64 第38年的本息和为:440563.17 第39年的本息和为:449815.00 第40年的本息和为:459261.11

可以看到第34年的时候翻一番了,但是你要考虑到通货膨胀等各种因素,实际上这种投资方式很不划算,但是稳定。

年利率为5.1%的计算结果

第1年的本息和为:210200.0 第2年的本息和为:220920.19 第3年的本息和为:232187.13 第4年的本息和为:244028.67 第5年的本息和为:256474.13 第6年的本息和为:269554.31 第7年的本息和为:283301.58 第8年的本息和为:297749.96 第9年的本息和为:312935.21 第10年的本息和为:328894.91 第11年的本息和为:345668.55 第12年的本息和为:363297.64 第13年的本息和为:381825.82 第14年的本息和为:401298.94 第15年的本息和为:421765.19 第16年的本息和为:443275.21 第17年的本息和为:465882.25 第18年的本息和为:489642.24 第19年的本息和为:514614.00 第20年的本息和为:540859.31 第21年的本息和为:568443.14 第22年的本息和为:597433.74 第23年的本息和为:627902.86 第24年的本息和为:659925.91 第25年的本息和为:693582.13 第26年的本息和为:728954.82 第27年的本息和为:766131.51 第28年的本息和为:805204.22 第29年的本息和为:846269.63 第30年的本息和为:889429.39 第31年的本息和为:934790.28 第32年的本息和为:982464.59 第33年的本息和为:1032570.28 第34年的本息和为:1085231.37 第35年的本息和为:1140578.17 第36年的本息和为:1198747.66 第37年的本息和为:1259883.79 第38年的本息和为:1324137.86 第39年的本息和为:1391668.89 第40年的本息和为:1462644.00

我们可以看到第14年就翻一番了,这是差不多是银行吃的商业贷款的利息(比商业贷款的4.9%高了0.2个百分点),还是非常高的

年利率为8%的计算结果

第1年的本息和为:216000.0 第2年的本息和为:233280.00 第3年的本息和为:251942.40 第4年的本息和为:272097.79 第5年的本息和为:293865.61 第6年的本息和为:317374.86 第7年的本息和为:342764.85 第8年的本息和为:370186.04 第9年的本息和为:399800.92 第10年的本息和为:431784.99 第11年的本息和为:466327.79 第12年的本息和为:503634.02 第13年的本息和为:543924.74 第14年的本息和为:587438.72 第15年的本息和为:634433.82 第16年的本息和为:685188.52 第17年的本息和为:740003.61 第18年的本息和为:799203.89 第19年的本息和为:863140.21 第20年的本息和为:932191.42 第21年的本息和为:1006766.74 第22年的本息和为:1087308.08 第23年的本息和为:1174292.72 第24年的本息和为:1268236.14 第25年的本息和为:1369695.03 第26年的本息和为:1479270.64 第27年的本息和为:1597612.29 第28年的本息和为:1725421.27 第29年的本息和为:1863454.97 第30年的本息和为:2012531.37 第31年的本息和为:2173533.88 第32年的本息和为:2347416.59 第33年的本息和为:2535209.92 第34年的本息和为:2738026.72 第35年的本息和为:2957068.85 第36年的本息和为:3193634.36 第37年的本息和为:3449125.11 第38年的本息和为:3725055.12 第39年的本息和为:4023059.53 第40年的本息和为:4344904.29

差不多第9年就翻一番了,如果你有20万,并且保持每年8%的骄人战绩,那么40年后泥浆拥有430多玩的资产,所以这应该是我们每个人的奋斗目标,哈哈哈

年利率为10%的时候的计算结果

第1年的本息和为:220000.00 第2年的本息和为:242000.00 第3年的本息和为:266200.00 第4年的本息和为:292820.00 第5年的本息和为:322102.00 第6年的本息和为:354312.20 第7年的本息和为:389743.42 第8年的本息和为:428717.76 第9年的本息和为:471589.53 第10年的本息和为:518748.49 第11年的本息和为:570623.34 第12年的本息和为:627685.67 第13年的本息和为:690454.24 第14年的本息和为:759499.66 第15年的本息和为:835449.63 第16年的本息和为:918994.59 第17年的本息和为:1010894.05 第18年的本息和为:1111983.46 第19年的本息和为:1223181.80 第20年的本息和为:1345499.98 第21年的本息和为:1480049.98 第22年的本息和为:1628054.98 第23年的本息和为:1790860.48 第24年的本息和为:1969946.53 第25年的本息和为:2166941.18 第26年的本息和为:2383635.30 第27年的本息和为:2621998.83 第28年的本息和为:2884198.72 第29年的本息和为:3172618.59 第30年的本息和为:3489880.45 第31年的本息和为:3838868.49 第32年的本息和为:4222755.34 第33年的本息和为:4645030.88 第34年的本息和为:5109533.97 第35年的本息和为:5620487.36 第36年的本息和为:6182536.10 第37年的本息和为:6800789.71 第38年的本息和为:7480868.68 第39年的本息和为:8228955.55 第40年的本息和为:9051851.11

如果你能做到年利率10%并且保持这个状态的话,你绝对是股神中的股神了哈哈。

最后放一张图表大家感受一下

数据展示

我们可以使用pandas库对数据进行展示

""" 这个文件用于展示复利数据 """ import pandas as pd #读取6个文件提取信息 data = [] filename = ["利率0.021.txt", "利率0.031.txt", "利率0.041.txt", "利率0.051.txt", "利率0.08.txt", "利率0.1.txt"] for i in range(0, filename.__len__()): d = [] #用于存储数据 with open(filename[i], "r", encoding="utf-8") as f: for line in f.readlines(): s = line.split(":")[1] s = s.split("\n")[0] d.append(s) #将复利数据加入到列表中 data.append(d) #到这里data就存储了所有我们需要的数据 frame = {} for i in range(0, data.__len__()): frame.update({str(filename[i].split(".")[0])+"."+str(filename[i].split(".")[1]): data[i]}) #添加入字典 #下面开始展示数据 idx = [] for i in range(1, 41): idx.append("第"+str(i)+"年") df = pd.DataFrame(frame, index=idx) print(df) 总结

复利是很厉害,不过这也看利率的,利率高才能在短期内看到效果。通过今天的计算,我确定了我的投资目标,年利率8%哈哈哈,开玩笑的。最后还是提醒各位:入市有风险,投资需谨慎

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3