在QuantLib中处理CMS Pricer时出错

2024-05-19 02:12:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在试图用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

如果我没有弄错的话,我可以通过包含一个pricer(ql.cmscoponpricer?)来解决这个问题。不幸的是,我在web/python食谱中没有找到任何关于CMS债券的有用信息。 我将代码复制到下面(从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

我想核实一下优惠券。你能帮我一下并提供测试代码所需的代码吗? 多谢各位


Tags: inselfdateindexrateaslastprint
1条回答
网友
1楼 · 发布于 2024-05-19 02:12:44

首先,你必须设置一个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)

相关问题 更多 >

    热门问题