開始使用免費開始

預測價格 vs. 實際價格 I

以存續期間來預測在不同殖利率下的債券價格,並將這些預測價格與實際價格比較,是視覺化檢驗存續期間準確度的好方法。

在這個練習中,你會先計算債券的存續期間,以及在不同殖利率下的債券價格。下一個練習中,你會用存續期間推得預測價格並繪製其差異。

你要分析的債券為期限 10 年、每年付息 5%,到期殖利率 5% 的債券。

numpynumpy_financialpandasmatplotlib 已分別以 npnpfpdplt 匯入。

本練習屬於課程

使用 Python 進行債券評價與分析

檢視課程

練習說明

  • 找出該債券的存續期間與美元存續期間。
  • 建立從 0 到 10、間隔為 0.1 的債券殖利率陣列,並將此陣列轉為名為 bond_yieldpandas DataFrame。
  • 新增欄位 price,其內容為各殖利率水準下的債券價格。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Price a 10 year bond with 5% coupon and 5% yield, reprice at higher and lower yields
price = ____
price_up = ____
price_down = ____

# Find the duration and dollar duration of the bond
duration = ____
dollar_duration = ____ * ____ * ____

# Create an array of yields from 0 to 10 in steps of 0.1, convert to DataFrame
bond_yields = np.arange(____, ____, ____)
bond = pd.DataFrame(____, columns=['bond_yield'])

# Add a column called price with the bond price for each yield level
bond['price'] = -npf.pv(rate=bond['bond_yield'] / 100, ____, ____, ____)
編輯並執行程式碼