程式

使用 Quantlib Python 的存款零利率

  • August 9, 2021

我使用 QuantLib Python 從存款和債券中建構了一條零曲線。

以下是我的程式碼:

import QuantLib as ql

valuationDate = ql.Date(30, 6, 2021)
ql.Settings.instance().evaluationDate = valuationDate
calendar = ql.NullCalendar()
dayConvention = ql.Actual360()
businessConvention = ql.Following
couponFrequency = ql.Semiannual

depositRates = [1.03, 1.13, 1.43]
depositMaturities = ['91D', '182D', '364D']
depositHelpers = [ql.DepositRateHelper(ql.QuoteHandle(ql.SimpleQuote(r/100.0)), 
                   ql.Period(m), 
                   2,
                   calendar, 
                   businessConvention,
                   True,
                   dayConvention) for r, m in zip(depositRates, depositMaturities)]

bondPrices = [98.452, 99.955, 100.68, 94.251, 89.1395]
bondMaturities = ['3Y', '5Y', '10Y', '15Y', '20Y']
bondCoupons = [1.85, 3.10, 4.45, 4.17, 4.17]
bondHelpers = []
for r, m, z in zip(bondPrices, bondMaturities, bondCoupons):
                   terminationDate = valuationDate + ql.Period(m)
                   schedule = ql.Schedule(valuationDate, terminationDate, ql.Period(couponFrequency), calendar, businessConvention, businessConvention, ql.DateGeneration.Forward, True)
                   bondHelper = ql.FixedRateBondHelper(ql.QuoteHandle(ql.SimpleQuote(r)), 2, 100, schedule, [z/100], dayConvention, businessConvention)
                   bondHelpers.append(bondHelper)
               
rateHelpers = depositHelpers + bondHelpers

yieldCurve = ql.PiecewiseLogCubicDiscount(valuationDate, rateHelpers, dayConvention)
yieldCurve.enableExtrapolation()

dates = [date for date in yieldCurve.dates()]
zeroRates = []

for d in dates[1:]:
   years = dayConvention.yearFraction(valuationDate, d)
   zeroRate = yieldCurve.zeroRate(d, dayConvention, ql.Compounded, couponFrequency).rate()
   zeroRates.append(zeroRate * 100)

但是,對於存款,由於這些已經是零息票,我希望零利率與插入depositHelpers列表中的面值利率完全相等。

不幸的是,情況並非如此,如下所示:

for i in range(0, 3, 1):
   print('Par rate:', depositRates[i], '; Zero rate:', zeroRates[i])

結果如下:

Par rate: 1.03 ; Zero rate: 1.0305594755884773
Par rate: 1.13 ; Zero rate: 1.1285118046763465
Par rate: 1.43 ; Zero rate: 1.4224679299821297

有人可以解釋我做錯了什麼嗎?非常感謝。

我剛剛更改了程式碼的最後一部分並刪除了債券,因為它們不會影響第一利率。

問題是您的零利率與您的存款相比有 2 天的額外時間。您可以通過將輸入從固定日期更改為基於日曆的相對日期(它將基於評估日期)來更改曲線參考日期以匹配您的工具。

rateHelpers = depositHelpers #+ bondHelpers

yieldCurve = ql.PiecewiseLinearZero(2, calendar, rateHelpers, dayConvention)
yieldCurve.enableExtrapolation()

for helper in depositHelpers:
   dt = helper.maturityDate()
   zeroRate = yieldCurve.zeroRate(dt, dayConvention, ql.Simple).rate()
   print(f"{dt.ISO()}, {zeroRate:.6%}, {helper.quote().value():.6%}")

2021-10-01, 1.030000%, 1.030000%

2021-12-31, 1.130000%, 1.130000%

2022-07-01, 1.430000%, 1.430000%

引用自:https://quant.stackexchange.com/questions/66359