在 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?)。不幸的是,我没有在网络上/ 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)
我一直在尝试在 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?)。不幸的是,我没有在网络上/ 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)