簡體   English   中英

在 QuantLib 中處理 CMS Pricer 時出錯

[英]Error dealing with CMS Pricer in QuantLib

我試圖在 QuantLib 中為 CmsRateBond 定價。 這是我運行print(ql.as_coupon(bond.cashflows()[-2]).rate())時發生的錯誤

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
c:\Users\...\question.py in 
----> 74 print(ql.as_coupon(bond.cashflows()[-2]).rate())

~\Anaconda3\lib\site-packages\QuantLib\QuantLib.py in rate(self)
   9694 
   9695     def rate(self):
-> 9696         return _QuantLib.Coupon_rate(self)
   9697 
   9698     def accrualPeriod(self):

RuntimeError: pricer not set

如果我沒記錯的話,我可以通過包含定價器 (ql.CmsCouponPricer?) 來解決問題。 不幸的是,我在 web / python 食譜中沒有發現任何有關 CMS 債券的有用信息。 我在下面復制我的代碼(向量 spot_rates,轉發是從 csv 文件導入的):

import QuantLib as ql

issue_date = ql.Date(4, 4, 2013)
maturity_date = ql.Date(4, 4, 2025)
float_coupon_daycount = ql.Actual360()
float_coupon_period = ql.Period("1Y")
quotedMargin = 0.0195
faceAmount = 100.0
last_floating_rate = -0.000149
settlement_days = 2

# valuation date
calc_date = ql.Date(14, 12, 2020)
ql.Settings.instance().evaluationDate = calc_date

# Discounting curve
discounting_curve = ql.ZeroCurve(
    spot_dates,
    spot_rates,
    ql.ActualActual(),
)
discounting_curve_handle = ql.YieldTermStructureHandle(discounting_curve)

# Forecasting curve
forecasting_curve = ql.ForwardCurve(dates, forwards, float_coupon_daycount)

# Index
index = ql.EuriborSwapIsdaFixA(
    ql.Period("10Y"), ql.YieldTermStructureHandle(forecasting_curve)
)

#Schedule
schedule = ql.Schedule(
    issue_date,
    maturity_date,
    ql.Period("1Y"),
    index.fixingCalendar(),
    ql.ModifiedFollowing,
    ql.ModifiedFollowing,
    ql.DateGeneration.Backward,
    False,
)

# CMS bond
bond = ql.CmsRateBond(
    settlement_days,
    faceAmount,
    schedule,
    index,
    index.dayCounter(),
    ql.Unadjusted,
    fixingDays=index.fixingDays(),
    gearings=[1],
    spreads=[quotedMargin],
    caps=[],
    floors=[0],
)

# add last fixing rate
fixingDates = [
    cf.fixingDate() for cf in map(ql.as_floating_rate_coupon, bond.cashflows()[:-1])
]
missing_fixingDate = list(filter(lambda x: x < calc_date, fixingDates))[-1]
index.addFixing(missing_fixingDate, last_floating_rate)

print(ql.as_coupon(bond.cashflows()[-2]).rate()) # RuntimeError: pricer not set

我正在使用通過 pip 安裝的 QuantLib 1.19。

我想驗證優惠券。 您能幫我並提供測試我的代碼所需的代碼嗎? 謝謝你。

首先,您必須設置一個 cms 定價器。 您可以查看 SWIG 測試( https://github.com/lballabio/QuantLib-SWIG/blob/master/Python/test/cms.py

這是一個簡單的例子:

volQuote = ql.QuoteHandle(ql.SimpleQuote(0.2))
swaptionVol = ql.ConstantSwaptionVolatility(0, ql.TARGET(), ql.ModifiedFollowing, volQuote, ql.Actual365Fixed())
swvol_handle = ql.SwaptionVolatilityStructureHandle(swaptionVol)

mean_reversion = ql.QuoteHandle(ql.SimpleQuote(0.01))
cms_pricer = ql.LinearTsrPricer(swvol_handle, mean_reversion)

然后您可以將該定價器分配給債券息票:

ql.setCouponPricer(bond.cashflows(), cms_pricer)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM