Python
使用 QuantLib Python 計算收益率曲線票面利率
我想使用 QuantLib Python 來計算掉期曲線的票面利率。
以下程式碼是我到目前為止所做的:
from QuantLib import * # global data calendar = TARGET() todaysDate = Date(6,November,2001); Settings.instance().evaluationDate = todaysDate settlementDate = Date(8,November,2001); # market quotes deposits = { (1,Weeks): 0.0382, (1,Months): 0.0372, (3,Months): 0.0363, (6,Months): 0.0353, (9,Months): 0.0348, (1,Years): 0.0345 } swaps = { (2,Years): 0.037125, (3,Years): 0.0398, (5,Years): 0.0443, (10,Years): 0.05165, (15,Years): 0.055175 } # convert them to Quote objects for n,unit in deposits.keys(): deposits[(n,unit)] = SimpleQuote(deposits[(n,unit)]) for n,unit in swaps.keys(): swaps[(n,unit)] = SimpleQuote(swaps[(n,unit)]) # build rate helpers dayCounter = Actual360() settlementDays = 2 depositHelpers = [ DepositRateHelper(QuoteHandle(deposits[(n,unit)]), Period(n,unit), settlementDays, calendar, ModifiedFollowing, False, dayCounter) for n, unit in [(1,Weeks),(1,Months),(3,Months), (6,Months),(9,Months),(1,Years)] ] fixedLegFrequency = Annual fixedLegTenor = Period(1,Years) fixedLegAdjustment = Unadjusted fixedLegDayCounter = Thirty360() floatingLegFrequency = Semiannual floatingLegTenor = Period(6,Months) floatingLegAdjustment = ModifiedFollowing swapHelpers = [ SwapRateHelper(QuoteHandle(swaps[(n,unit)]), Period(n,unit), calendar, fixedLegFrequency, fixedLegAdjustment, fixedLegDayCounter, Euribor6M()) for n, unit in swaps.keys() ] # term structure handles discountTermStructure = RelinkableYieldTermStructureHandle() forecastTermStructure = RelinkableYieldTermStructureHandle() # term-structure construction helpers = depositHelpers + swapHelpers depoSwapCurve = PiecewiseFlatForward(settlementDate, helpers, Actual360()) ref_date = depoSwapCurve.referenceDate() yc_day_count = depoSwapCurve.dayCounter() tenor = Period(10, Years) # 10Y Swap nominal = 1000000 maturity = calendar.advance(settlementDate,10,Years) fixedRate = 0.04 floatingLegFrequency = Semiannual spread = 0.0 fixingDays = 2 index = Euribor6M(forecastTermStructure) floatingLegDayCounter = index.dayCounter() fixedSchedule = Schedule(settlementDate, maturity, fixedLegTenor, calendar, fixedLegAdjustment, fixedLegAdjustment, DateGeneration.Forward, False) floatingSchedule = Schedule(settlementDate, maturity, floatingLegTenor, calendar, floatingLegAdjustment, floatingLegAdjustment, DateGeneration.Forward, False) swap = VanillaSwap(VanillaSwap.Receiver, nominal, fixedSchedule, fixedRate, fixedLegDayCounter, floatingSchedule, index, spread, floatingLegDayCounter) swap.setPricingEngine(swapEngine) discountTermStructure.linkTo(depoSwapCurve) forecastTermStructure.linkTo(depoSwapCurve) print('original instrument par rate:') print(swaps[(10, Years)].value()) print print('calculated swap par rate:') print(swap.fairRate()) print print('calculated yield curve par rate:') print(depoSwapCurve.forwardRate(ref_date, calendar.advance(ref_date, tenor), yc_day_count, Compounded ).rate())
從上面的程式碼我們可以看到,它
swap.fairRate()
返回的掉期利率與用於構造收益率曲線對象的原始掉期利率非常相似。這就是我要的。但是,這種計算掉期票面利率的方法非常麻煩,因為我不僅要計算 10Y 的掉期票面利率,還要計算 11Y、12Y、13Y 等的掉期票面利率……這意味著我必須創建一個單獨的 VanillaSwap對象為 11Y、12Y、13Y 等…我計算掉期票面利率的第二種方法是
depoSwapCurve.forwardRate(...)
。但是,從上面的程式碼中我們可以看到,forwardRate(...)
函式返回的值與用於構造收益率曲線對象的原始掉期利率有很大的不同。我
forwardRate(...)
是否正確使用該函式來計算票面利率?或者有沒有更好的方法來計算收益率曲線對象的票面利率?
不,您必須創建不同的交換。
forwardRate(start, end)
從頭到尾返回利率,中間不支付息票(Compounding
慣例意味著年利息是再投資,而不是還清)。為了讓這個過程不那麼麻煩,我建議你定義一個函式
parRate
,它接受 start 和 end 日期,創建相應的交換,並返回其公平率。一旦你有了它,呼叫它應該只比呼叫複雜一點forwardRate
。