累積付款與房屋淨值
你每個月都按時繳房貸,但多年下來,很難看出自己實際擁有了多少房屋,以及總共支付了多少利息。
使用 np.cumsum() 將所有利息支付累加起來,同時也將所有本金償還隨時間累加,以觀察你的持有比例如何隨時間變化。
回想一下,np.cumsum() 會執行隨時間的累積總和。它會回傳一系列逐步累加的結果,而不是只有單一數值。
前一題的 principal_paid、interest_paid、home_value 與 down_payment_percent 已可使用。
本練習屬於課程
Python 金融概念入門
練習說明
- 使用
np.cumsum()針對已償還的本金計算隨時間變化的cumulative_home_equity。 - 以相同方式計算
cumulative_interest_paid。 - 計算隨時間變化的房屋淨值百分比(別忘了把頭期款也加進去!)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
import numpy as np
# Calculate the cumulative home equity (principal) over time
cumulative_home_equity = ____
# Calculate the cumulative interest paid over time
cumulative_interest_paid = ____
# Calculate your percentage home equity over time
cumulative_percent_owned = ____ + (____/____)
print(cumulative_percent_owned)
# Plot the cumulative interest paid vs equity accumulated
plt.plot(cumulative_interest_paid, color='red')
plt.plot(cumulative_home_equity, color='blue')
plt.legend(handles=[interest_plot, principal_plot], loc=2)
plt.show()