收支情況模擬構(gòu)建思路:
1璃饱、構(gòu)建稅收函數(shù)
2、構(gòu)建專項扣除函數(shù)
3肪康、構(gòu)建社保函數(shù)
4荚恶、構(gòu)建獎金函數(shù)
5撩穿、構(gòu)建10年(即120個月)的專項扣除后和社保扣除后的稅收函數(shù)
6谒撼、構(gòu)建支出函數(shù)
7食寡、構(gòu)建花唄使用情況模型
8、每次取不同的隨機數(shù)進行模擬
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings
1廓潜、構(gòu)建稅收函數(shù)
def tax(x):
if x<=5000:
return 0
elif x<=(5000+3000):
return (x-5000)*0.03
elif x<=(5000+3000+4000):
return 3000*0.03+(x-5000-3000)*0.1
elif x<=(5000+3000+4000+13000):
return 3000*0.03+4000*0.1+(x-5000-3000-4000)*0.2
elif x<=(5000+3000+4000+13000+10000):
return 3000*0.03+4000*0.1+13000*0.2+(x-5000-3000-4000-13000)*0.25
elif x<=(5000+3000+4000+13000+10000+20000):
return 3000*0.03+4000*0.1+13000*0.2+10000*0.25+(x-5000-3000-4000-13000-10000)*0.3
elif x<=(5000+3000+4000+13000+10000+20000+25000):
return 3000*0.03+4000*0.1+13000*0.2+10000*0.25+20000*0.3+(x-5000-3000-4000-13000-10000-20000)*0.35
elif x<=(5000+3000+4000+13000+10000+20000+25000+100000):
return (x-5000)*0.45
2抵皱、構(gòu)建專項扣除函數(shù)
ched:子女教育,每個子女按1000元/月扣除,ched_mount:子女教育數(shù)量
mortgage:首套房貸利息辩蛋,按1000元/月扣除呻畸,=1則為有,=0則為沒有
rent:住房租金悼院,按1500元/月扣除伤为,=1則為有,=0則為沒有
support:贍養(yǎng)老人据途,獨生子女按2000元/月绞愚,非獨生子女每人1000元/月扣除
only:是否獨生,獨生=2颖医,非獨生=1
def deduction(ched,ched_mount,mortgage,rent,support,only):
if mortgage==1:
return 1000*ched_mount+1000+support*1000*only
else:
return 1000*ched_mount+rent*1500+support*1000*only
3爽醋、構(gòu)建社保函數(shù)
def insurance(s):#s為繳費基數(shù)
insurance_i=pd.DataFrame({
'養(yǎng)老保險':s*0.08,
'醫(yī)療保險':s*0.02,
'失業(yè)保險':s*0.01
},index = [0])
insurance_i['社保扣除']=insurance_i['養(yǎng)老保險']+insurance_i['醫(yī)療保險']+insurance_i['失業(yè)保險']
return int(insurance_i['社北慵梗扣除'])
4蚂四、構(gòu)建獎金函數(shù)
def bouns(a):
return pd.Series(np.random.normal(loc=a,scale=200,size=120))
5、構(gòu)建120個月的專項扣除后和社蹦奶担扣除后的稅收函數(shù)
def final_tax(s,s1,a,ched,ched_mount,mortgage,rent,support,only):
df_i=pd.DataFrame({
'月薪':[s for i in range(120)],
'專項扣除':[deduction(ched,ched_mount,mortgage,rent,support,only) for i in range(120)],
'社彼煸扣除':[insurance(s1) for i in range(120)],
'獎金':bouns(a)
})
df_i['計稅部分']=df_i['月薪']+df_i['獎金']
df_i['稅收']=df_i['計稅部分'].apply(lambda x:tax(x))
df_i['凈收入']=df_i['月薪']+df_i['獎金']-df_i['稅收']-df_i['社保扣除']
return df_i
出圖:前12個月收入柱狀圖
plt.title('前12個月收入柱狀圖')
df2['凈收入'].iloc[:12].plot(kind='Bar',stacked=True,alpha=0.6,color='Red')
plt.grid()
plt.figure(figsize=(12,4))
6晌杰、構(gòu)建支出函數(shù)
假設(shè)總支出共分為五部分:房租水電(在3000左右跷睦,上下浮動200),家用(2000)肋演,基本生活支出(3000左右抑诸,上下浮動500),交際交通(2500左右爹殊,上下浮動800)蜕乡,日用品化妝品(2000左右,上下浮動500)
def expense():
df_2=pd.DataFrame({
'房租水電':np.random.normal(loc=3000,scale=200,size=120),
'家用':[2000 for i in range (120)],
'基本支出':np.random.normal(loc=3000,scale=500,size=120),
'交通交際':np.random.normal(loc=2500,scale=800,size=120),
'日用支出':np.random.normal(loc=2000,scale=500,size=120)
})
df_2['總支出']=df_2['房租水電']+df_2['家用']+df_2['基本支出']+df_2['交通交際']+df_2['日用支出']
return df_2
出圖:前12個月支出直方圖
result[['房租水電','家用','基本支出','交通交際','日用支出']].iloc[:12].plot(kind='Bar',figsize=(8,6),
stacked=True,colormap='Reds_r',alpha=0.6)
plt.title('前12個月支出直方圖')
plt.grid()
7梗夸、構(gòu)建花唄使用情況模型,假設(shè)不使用分期
#制作樣本數(shù)據(jù)(收入层玲、支出、盈余、負(fù)債)
income=df2['凈收入'].tolist()
exp=expense()['總支出'].tolist()
saving=[0 for i in range(120)]
debt=[0 for i in range(120)]
#構(gòu)建收支模型
def case():
month=[]#存放月份
data=[]#存放收支數(shù)據(jù)
for i in range(119):
money=income[i]+saving[i]-exp[i]-debt[i]
if money<-10000:
print('第%i個月花唄也救不了我了辛块,我要吃土了E吓伞!润绵!'%i)
else:
if money>0:
saving[i+1]=income[i]+saving[i]-exp[i]-debt[i]
debt[i+1]=0
else:
saving[i+1]=0
debt[i+1]=-(income[i]+saving[i]-exp[i]-debt[i])
month.append(i+1)
data.append([income[i],exp[i],saving[i+1],debt[i+1]])
result=pd.DataFrame(data,columns=['月收入','月支出','盈余','負(fù)債'],index=month)
return result
出圖:前12個月收支盈虧情況
case().iloc[:12].plot(kind='Bar',figsize=(7,5),stacked=True,colormap='Reds',alpha=0.6)
plt.title('前12個月收支盈虧情況')
plt.grid()
8线椰、每次取不同的支出隨機數(shù)進行模擬并出圖
case_a=[]
for i in range(10):
print('正在進行第%i次模擬'%i)
income=df2['凈收入'].tolist()
exp=expense()['總支出'].tolist()
saving=[0 for i in range(120)]
debt=[0 for i in range(120)]
result_a=case()
case_a.append(result_a)
result_a.iloc[:12].plot(kind='Bar',figsize=(10,8),stacked=True,colormap='Reds',alpha=0.6)
plt.title('第%i次模擬,前12個月收支盈虧情況'%i)
plt.grid()
以上即為此次模擬全過程尘盼,快來預(yù)測看看吧~