剛剛大學畢業(yè)的你獲得了一份滿意的工作,接下來正雄心勃勃地規(guī)劃著自己的未來。這其中自然少不了財富目標,比如怎樣成為百萬富翁呢?本文將帶你用Python分析你的財務情況,以及怎樣才能實現(xiàn)你的理財目標。
收入和支出
假如你工作的起薪是10萬元,但是你需要交納五險一金和個稅,這部分大約占工資的30%。為了制定合理的預算方案,首先需要計算實際到手的工資。
# 設定年收入
salary = 100000
# 假設稅率是30%
tax_rate = 0.30
# 計算稅后收入
salary_after_taxes = salary * (1 - tax_rate)
print("稅后年收入: " + str(round(salary_after_taxes, 2)))
# 計算每月的稅后收入
monthly_takehome_salary = salary_after_taxes / 12
print("每月到手的收入: " + str(round(monthly_takehome_salary, 2)))
稅后年收入: 70000.0
每月到手的收入: 5833.33
知道實際的收入后,我們再來看每月的開支,預算如下:
- 房租:1500/月
- 就餐:平均40/天,一月按30天計算
- 娛樂:500/月
- 未預見的開支:300/月
現(xiàn)在來計算每月的支出和結余。
# 每月的房租
monthly_rent = 1500
# 每月的就餐預算
monthly_food_budget = 40 * 30
# 每月的娛樂預算
monthly_entertainment_budget = 500
# 未預見的開支
monthly_unforeseen_expenses = 300
# 計算總的月度開支
monthly_expenses = monthly_rent + monthly_food_budget \
+ monthly_entertainment_budget + monthly_unforeseen_expenses
print("每月支出: " + str(round(monthly_expenses, 2)))
# 計算每月的結余
monthly_savings = monthly_takehome_salary - monthly_expenses
print("每月結余: " + str(round(monthly_savings, 2)))
每月支出: 3500
每月結余: 2333.33
你的收入和支出并不是一成不變的。隨著工作經(jīng)驗的增長,一般你的工資也會上漲。假設工資以每年5%的速度穩(wěn)定增長,預測未來15年的收入情況(為了簡單起見,這里假設稅率沒變)。
注:下面使用的
np.cumprod()
累積求積函數(shù),以及年增長率到月增長率的換算說明,可參見上一篇文章《用Python分析房屋抵押貸款》
import numpy as np # 導入numpy科學計算包
from matplotlib import pyplot as plt # 導入繪圖工具包
%config InlineBackend.figure_format = 'retina' # 設置圖片清晰度
plt.rcParams['font.sans-serif']=['SimHei'] # 設置中文字體
# 預測未來15年的情況,并換算到月份
forecast_months = 12*15
# 設置工資的年增長率
annual_salary_growth = 0.05
# 換算成工資每月的增長率(注意是復利)
monthly_salary_growth = (1 + annual_salary_growth) ** (1/12) - 1
# 計算工資的累積增長
cumulative_salary_growth_forecast = np.cumprod(np.repeat(1 + monthly_salary_growth, forecast_months))
# 預測工資
salary_forecast = monthly_takehome_salary * cumulative_salary_growth_forecast
# 繪制工資隨時間變化的曲線
plt.plot(salary_forecast, color='green')
plt.xlabel("月份")
plt.ylabel("每月收入")
plt.show()
不僅工資會上漲,由于通貨膨脹,你的開支也會上漲。假設開支的增長速度是每年2.5%,采用上面相同的方法來計算未來15年的開支情況。
# 設置年通貨膨脹率
annual_inflation = 0.025
# 換算成每月的通脹率
monthly_inflation = (1 + annual_inflation) ** (1/12) - 1
# 預測累積的通脹
cumulative_inflation_forecast = np.cumprod(1 + np.repeat(monthly_inflation, forecast_months))
# 預測未來的開支
expenses_forecast = monthly_expenses * cumulative_inflation_forecast
# 繪制開支隨時間變化的曲線
plt.plot(expenses_forecast, color='red')
plt.xlabel("月份")
plt.ylabel("月度支出")
plt.show()
為了方便比較,將上兩幅圖中的收入和支出曲線繪制在一幅圖中。幸運的是你的收入增長速度大于通貨膨脹率,這意味著你每月將有更多的結余。
plt.plot(salary_forecast, color='green', label="每月收入")
plt.plot(expenses_forecast, color='red', label="每月支出")
plt.legend(loc=2)
plt.xlabel("月份")
plt.ylabel("金額")
plt.show()
儲蓄和投資
在上一節(jié)中,我們預測了收入和支出隨時間變化的情況,將每月的收入減去支出,便可算得每月積攢了多少錢。將每月的積蓄累加起來,就得到了總的積蓄。
注:下面使用的
np.cumsum()
函數(shù)進行累積求和,可參見上一篇文章《用Python分析房屋抵押貸款》
# 計算每月的積蓄
savings_forecast = salary_forecast - expenses_forecast
# 計算累積的積蓄
cumulative_savings = np.cumsum(savings_forecast)
# 輸出15年后總的積蓄
final_net_worth = cumulative_savings[-1]
print("15年后總的積蓄為: " + str(round(final_net_worth, 2)) + "元")
# 繪制總積蓄隨時間推移的曲線
plt.plot(cumulative_savings, color='blue')
plt.xlabel("月份")
plt.ylabel("累積的積蓄")
plt.show()
15年后總的積蓄為: 787802.65元
從上面的計算可知,即使你嚴格執(zhí)行了預算計劃,并且工資也保持穩(wěn)定增長,在15年后你的積蓄才打到78萬多,這離你百萬富翁的目標還有一定的差距。這時光靠存錢還不夠,還需要你來進行合理的投資。假如投資的年回報率是7%,那么每月需要投資多少錢才能在15年后使財富增長到100萬呢?
numpy.pmt(rate, nper, pv)
函數(shù)用來計算每月投資額
- rate:每月投資回報率
- nper:總的投資期數(shù)
- pv:現(xiàn)值,這里是0
- fv:未來價值,這里是100萬
# 設置年投資回報率為7%
investment_rate_annual = 0.07
# 換算成每月的投資回報率
investment_rate_monthly = (1 + investment_rate_annual) ** (1/12) -1
# 為了獲得100萬,計算每月需要投資的金額
required_investment_monthly = -np.pmt(rate=investment_rate_monthly, \
nper=forecast_months, pv=0, fv=1000000)
print("你需要連續(xù)15年每月投資 "
+ str(round(required_investment_monthly, 2))
+ " 元才能達到100萬。")
你需要連續(xù)15年每月投資 3214.35 元才能達到100萬。
上面的計算告訴你每月需要投資三千多元才行,但是你一開始的積蓄只有兩千多,負擔不起這樣的投資方案。
# 查看前5個月每月的積蓄
savings_forecast[:5]
array([ 2349.88962846, 2366.52789784, 2383.24850536, 2400.05181645,
2416.9381981 ])
我們可以換一種投資方案,不是每月投資固定的額度,而是按你收入的一定比例來投資。這樣雖然一開始投入的較少,但是隨著收入的增長,投資額也會隨之增長。下面將模擬這一投資方案,假設將結余50%的資金用于投資。
# 設置每月結余中用于投資的比例
monthly_investment_percentage = 0.5
# 計算每月投資額度
investment_deposit_forecast = savings_forecast * monthly_investment_percentage
# 剩余的進入儲蓄賬戶
savings_forecast_new = savings_forecast * (1 - monthly_investment_percentage)
# 繪制每月儲蓄和投資的累積額度
fig = plt.figure(figsize=(12,4.5)) #設置畫布大小
p1 = fig.add_subplot(121) # 添加第一個子圖
plt.plot(investment_deposit_forecast, color='orange', label="投資")
plt.xlabel("月份")
plt.ylabel("每月的投資金額")
p2 = fig.add_subplot(122) # 添加第二個子圖
plt.plot(savings_forecast_new, color='blue', label="儲蓄")
plt.xlabel("月份")
plt.ylabel("每月的儲蓄金額")
plt.show()
上面這兩幅圖一模一樣,因為你將結余的一半用來投資,另一半用來儲蓄。下面讓我們來計算這樣的投資組合帶來的資產凈值。(為簡單起見,這里假設投資的年回報是7%,而儲蓄沒有利息。)
# 設置投資、凈值的初始數(shù)組(每個元素都為0,數(shù)組長度是12*15)
investment_portfolio = np.repeat(0., forecast_months)
net_worth = np.repeat(0., forecast_months)
# 計算累積的儲蓄額
cumulative_savings_new = np.cumsum(savings_forecast_new)
# 循環(huán)計算每個月的投資和結余情況
for i in range(forecast_months):
# 設置前一月投資的總金額
# 如果是第一個月,則前一月投資總額為0
if i == 0:
previous_investment = 0
else:
previous_investment = investment_portfolio[i-1]
# 計算前一月的投資總額在當月的增長
previous_investment_growth = previous_investment*(1 + investment_rate_monthly)
# 將之前的投資總額加上當月的新增的投資額,等于當月的投資總數(shù)
investment_portfolio[i] = previous_investment_growth + investment_deposit_forecast[i]
# 計算當月的凈值,是累積儲蓄存款額加上當月總的投資額
net_worth[i] = cumulative_savings_new[i] + investment_portfolio[i]
# 繪制儲蓄、投資、凈值的隨時間變化的曲線圖
plt.plot(investment_portfolio, color='orange', label="投資")
plt.plot(cumulative_savings_new, color="blue", label="儲蓄")
plt.plot(net_worth, color='green', label="凈值")
plt.xlabel("月份")
plt.ylabel("金額")
plt.legend(loc=2)
plt.show()
我們來查看15年后總的凈值:
print("15年后的凈值:" + str(round(net_worth[-1],2)))
15年后的凈值:1014978.67
按照上面的組合投資方法,即將一半的結余用于投資,另一半用于儲蓄,那么15年后你將達到100萬的目標。
注:本文是DataCamp課程Intro to Financial Concepts using Python的學習筆記。
更多該課程的筆記: