使用 QuantLib 计算 FloatingRateBond 的现金流量
Using QuantLib to compute cash flows for FloatingRateBond with Floor
QuantLib 的新手所以猜测这是一个新手错误。很高兴了解这个强大的库,感谢作者和贡献者!
如果没有下限参数,我可以在没有定价器的情况下为 FloatingRateBond 生成现金流量,所以我不明白为什么包含下限参数需要定价器。我认为增加底限只会为每个固定值提供一个最小值。
想看看是否有人在使用地板时让 FloatingRateBond 现金流发挥作用。而且,如果是这样,如果有人能发现我误入歧途的地方。提前致谢!
我在 windows 上使用通过预打包安装程序安装的 QuantLib 1.8 (QuantLib-Python-1.8.win-amd64-py3.5.msi).
这里是错误发生的地方:
File "C:/src/misc/generate_cashflows.py", line 138, in generate_cashflow
print(cf.amount())
File "C:\lib\site-packages\QuantLib\QuantLib.py", line 8844, in amount
return _QuantLib.CashFlow_amount(self)
RuntimeError: pricer not set
具体代码如下:
ql_first_day, ql_first_month, ql_first_year = first_payment_date.day, first_payment_date.month, first_payment_date.year
ql_first_date = QuantLib.Date(ql_first_day, ql_first_month, ql_first_year)
maturity_month, maturity_day, maturity_year = maturity.month, maturity.day, maturity.year
ql_maturity_date = QuantLib.Date(maturity_day, maturity_month, maturity_year)
ql_issue_day, ql_issue_month, ql_issue_year = issue_date.day, issue_date.month, issue_date.year
q1_settle_date = QuantLib.Date(ql_issue_day, ql_issue_month, ql_issue_year)
fixing_days = 0
calendar = QuantLib.UnitedStates()
ql_settle_date = calendar.adjust(q1_settle_date)
todays_date = calendar.advance(ql_settle_date, -fixing_days, QuantLib.Days)
QuantLib.Settings.instance().evaluationDate = todays_date
ql_schedule = QuantLib.Schedule(ql_settle_date,
ql_maturity_date, QuantLib.Period(ql_frequency_enum),
QuantLib.UnitedStates(),
QuantLib.Following, QuantLib.Following,
QuantLib.DateGeneration.Forward, False, ql_first_date)
ql_forecast_curve = QuantLib.RelinkableYieldTermStructureHandle()
today = datetime.datetime.today()
calc_date = QuantLib.Date(today.day, today.month, today.year)
QuantLib.Settings.instance().evaluationDate = calc_date
day_count = QuantLib.Thirty360()
# setup swaps
calendar = QuantLib.UnitedStates()
swFixedLegFrequency = QuantLib.Annual
swFixedLegConvention = QuantLib.Unadjusted
swFixedLegDayCounter = QuantLib.Thirty360()
swFloatingLegIndex = QuantLib.USDLibor(QuantLib.Period(3, QuantLib.Months))
swap_raw = [
(1, 0.01251),
(2, 0.01505),
(3, 0.01701),
(5, 0.01972),
(7, 0.02158)
]
swap_rates = []
for year, rate in swap_raw:
swap_rates.append(QuantLib.SwapRateHelper(
QuantLib.QuoteHandle(QuantLib.SimpleQuote(rate)),
QuantLib.Period(year, QuantLib.Years),
calendar,
swFixedLegFrequency,
swFixedLegConvention,
swFixedLegDayCounter,
swFloatingLegIndex
))
swap_curve = QuantLib.PiecewiseFlatForward(calc_date, swap_rates, day_count)
ql_forecast_curve.linkTo(swap_curve)
ql_index = QuantLib.USDLibor(period, ql_forecast_curve)
settlement_days = 0
face_amount = 100
ql_bond = QuantLib.FloatingRateBond(settlement_days, #settlementDays
face_amount, # faceAmount
ql_schedule,
ql_index,
QuantLib.Thirty360(),
gearings = [],
spreads = [libor_spread],
caps = [],
floors = [.01]
)
ql_discount_curve = QuantLib.RelinkableYieldTermStructureHandle()
settlement_date = QuantLib.Date(9, 2, 2017)
flatForward = QuantLib.FlatForward(
settlement_date,
.02,
QuantLib.ActualActual(QuantLib.ActualActual.Bond),
QuantLib.Compounded,
QuantLib.Semiannual)
ql_discount_curve.linkTo(flatForward)
bondEngine = QuantLib.DiscountingBondEngine(ql_discount_curve)
ql_bond.setPricingEngine(bondEngine)
for cf in ql_bond.cashflows():
c = QuantLib.as_floating_rate_coupon(cf)
print(cf.amount())
首先是理论:在为带有下限的息票定价时,您不能只从预测曲线中获取预期的 LIBOR 利率,然后取该利率与下限之间的最小值。相反,您需要取利率和底线之间最小值的预期值,不幸的是 E[min(R,F)]
与 min(E[R],F)
不同。所以不,地板不只是提供最低限度;您需要一个不同的公式来估算预期收益。
QuantLib 的含义是,简单的 floating-rate 优惠券可以(并且正在)设置一个默认定价器,它只是从预测曲线中读取利率,但是带有上限或下限的优惠券需要用户指定什么使用和向其提供任何其他所需数据的价格;在您的情况下,这至少意味着波动率期限结构,尽管可以指定更多可选数据;有关详细信息,请参阅 BlackIborCouponPricer
class 的构造函数。
通常,波动率是根据上限和下限的市场报价来引导的,但创建它的过程有点复杂(参见 these tests for an example in C++), I'm not sure that all the needed classes are exported to Python, and you'll be better off asking about it on the QuantLib mailing list。
如果您想验证优惠券是否有效,您可以使用恒定的波动率,如:
volatility = 0.10;
vol = QuantLib.ConstantOptionletVolatility(settlement_days,
calendar,
QuantLib.ModifiedFollowing,
volatility,
day_count)
pricer = QuantLib.BlackIborCouponPricer(
QuantLib.OptionletVolatilityStructureHandle(vol))
QuantLib.setCouponPricer(ql_bond.cashflows(), pricer)
以上应该能让你得到一个结果;但当然我把 15% 的波动率从帽子里拿出来了,它不会给你实际的市场价值...
QuantLib 的新手所以猜测这是一个新手错误。很高兴了解这个强大的库,感谢作者和贡献者!
如果没有下限参数,我可以在没有定价器的情况下为 FloatingRateBond 生成现金流量,所以我不明白为什么包含下限参数需要定价器。我认为增加底限只会为每个固定值提供一个最小值。
想看看是否有人在使用地板时让 FloatingRateBond 现金流发挥作用。而且,如果是这样,如果有人能发现我误入歧途的地方。提前致谢!
我在 windows 上使用通过预打包安装程序安装的 QuantLib 1.8 (QuantLib-Python-1.8.win-amd64-py3.5.msi).
这里是错误发生的地方:
File "C:/src/misc/generate_cashflows.py", line 138, in generate_cashflow
print(cf.amount())
File "C:\lib\site-packages\QuantLib\QuantLib.py", line 8844, in amount
return _QuantLib.CashFlow_amount(self)
RuntimeError: pricer not set
具体代码如下:
ql_first_day, ql_first_month, ql_first_year = first_payment_date.day, first_payment_date.month, first_payment_date.year
ql_first_date = QuantLib.Date(ql_first_day, ql_first_month, ql_first_year)
maturity_month, maturity_day, maturity_year = maturity.month, maturity.day, maturity.year
ql_maturity_date = QuantLib.Date(maturity_day, maturity_month, maturity_year)
ql_issue_day, ql_issue_month, ql_issue_year = issue_date.day, issue_date.month, issue_date.year
q1_settle_date = QuantLib.Date(ql_issue_day, ql_issue_month, ql_issue_year)
fixing_days = 0
calendar = QuantLib.UnitedStates()
ql_settle_date = calendar.adjust(q1_settle_date)
todays_date = calendar.advance(ql_settle_date, -fixing_days, QuantLib.Days)
QuantLib.Settings.instance().evaluationDate = todays_date
ql_schedule = QuantLib.Schedule(ql_settle_date,
ql_maturity_date, QuantLib.Period(ql_frequency_enum),
QuantLib.UnitedStates(),
QuantLib.Following, QuantLib.Following,
QuantLib.DateGeneration.Forward, False, ql_first_date)
ql_forecast_curve = QuantLib.RelinkableYieldTermStructureHandle()
today = datetime.datetime.today()
calc_date = QuantLib.Date(today.day, today.month, today.year)
QuantLib.Settings.instance().evaluationDate = calc_date
day_count = QuantLib.Thirty360()
# setup swaps
calendar = QuantLib.UnitedStates()
swFixedLegFrequency = QuantLib.Annual
swFixedLegConvention = QuantLib.Unadjusted
swFixedLegDayCounter = QuantLib.Thirty360()
swFloatingLegIndex = QuantLib.USDLibor(QuantLib.Period(3, QuantLib.Months))
swap_raw = [
(1, 0.01251),
(2, 0.01505),
(3, 0.01701),
(5, 0.01972),
(7, 0.02158)
]
swap_rates = []
for year, rate in swap_raw:
swap_rates.append(QuantLib.SwapRateHelper(
QuantLib.QuoteHandle(QuantLib.SimpleQuote(rate)),
QuantLib.Period(year, QuantLib.Years),
calendar,
swFixedLegFrequency,
swFixedLegConvention,
swFixedLegDayCounter,
swFloatingLegIndex
))
swap_curve = QuantLib.PiecewiseFlatForward(calc_date, swap_rates, day_count)
ql_forecast_curve.linkTo(swap_curve)
ql_index = QuantLib.USDLibor(period, ql_forecast_curve)
settlement_days = 0
face_amount = 100
ql_bond = QuantLib.FloatingRateBond(settlement_days, #settlementDays
face_amount, # faceAmount
ql_schedule,
ql_index,
QuantLib.Thirty360(),
gearings = [],
spreads = [libor_spread],
caps = [],
floors = [.01]
)
ql_discount_curve = QuantLib.RelinkableYieldTermStructureHandle()
settlement_date = QuantLib.Date(9, 2, 2017)
flatForward = QuantLib.FlatForward(
settlement_date,
.02,
QuantLib.ActualActual(QuantLib.ActualActual.Bond),
QuantLib.Compounded,
QuantLib.Semiannual)
ql_discount_curve.linkTo(flatForward)
bondEngine = QuantLib.DiscountingBondEngine(ql_discount_curve)
ql_bond.setPricingEngine(bondEngine)
for cf in ql_bond.cashflows():
c = QuantLib.as_floating_rate_coupon(cf)
print(cf.amount())
首先是理论:在为带有下限的息票定价时,您不能只从预测曲线中获取预期的 LIBOR 利率,然后取该利率与下限之间的最小值。相反,您需要取利率和底线之间最小值的预期值,不幸的是 E[min(R,F)]
与 min(E[R],F)
不同。所以不,地板不只是提供最低限度;您需要一个不同的公式来估算预期收益。
QuantLib 的含义是,简单的 floating-rate 优惠券可以(并且正在)设置一个默认定价器,它只是从预测曲线中读取利率,但是带有上限或下限的优惠券需要用户指定什么使用和向其提供任何其他所需数据的价格;在您的情况下,这至少意味着波动率期限结构,尽管可以指定更多可选数据;有关详细信息,请参阅 BlackIborCouponPricer
class 的构造函数。
通常,波动率是根据上限和下限的市场报价来引导的,但创建它的过程有点复杂(参见 these tests for an example in C++), I'm not sure that all the needed classes are exported to Python, and you'll be better off asking about it on the QuantLib mailing list。
如果您想验证优惠券是否有效,您可以使用恒定的波动率,如:
volatility = 0.10;
vol = QuantLib.ConstantOptionletVolatility(settlement_days,
calendar,
QuantLib.ModifiedFollowing,
volatility,
day_count)
pricer = QuantLib.BlackIborCouponPricer(
QuantLib.OptionletVolatilityStructureHandle(vol))
QuantLib.setCouponPricer(ql_bond.cashflows(), pricer)
以上应该能让你得到一个结果;但当然我把 15% 的波动率从帽子里拿出来了,它不会给你实际的市场价值...