
本文针对使用Python QuantLib进行固定利率债券定价时,出现债券价格计算为零的问题进行深入分析和解决。文章首先定位问题原因在于结算日期的计算,并详细解释了由于节假日导致结算日期延后,最终与到期日重合,从而导致价格为零的情况。同时,提供了修正方案,并通过示例代码演示了如何正确设置评估日期和结算日期,从而获得合理的债券价格。
在使用Python QuantLib进行债券定价时,可能会遇到计算出的债券价格为零的情况,这通常是由于一些细节设置不当造成的。本文将以一个实际案例出发,分析问题的原因,并提供解决方案。
问题分析:结算日期与评估日期
在给定的代码中,一个关键问题在于评估日期(evaluation date)的设置。QuantLib需要明确指定评估日期,否则会默认使用当前日期。如果评估日期晚于债券的到期日,或者结算日期与到期日重合,那么债券的价格自然会变为零。
立即学习“Python免费学习笔记(深入)”;
代码示例与调试
为了更好地理解问题,可以在代码中添加一些调试信息,例如:
import QuantLib as ql
# 债券信息和设置
settlementDays = 0
settlementDate = ql.Date('2023-02-18', '%Y-%m-%d')
effectiveDate = ql.Date('2019-08-21', '%Y-%m-%d')
terminationDate = ql.Date('2023-02-21', '%Y-%m-%d')
faceAmount = 100
coupon = 0.0625
frequency = ql.Period(2)
paymentConvention = ql.Thirty360(ql.Thirty360.ISMA)
calendar = ql.UnitedStates(ql.UnitedStates.NYSE)
# 评估日期
ql.Settings.instance().evaluationDate = ql.Date(18, 2, 2023) # 设置评估日期
# schedule
schedule = ql.Schedule(
effectiveDate,
terminationDate,
frequency,
calendar,
ql.Unadjusted,
ql.Unadjusted,
ql.DateGeneration.Backward,
False
)
# 定价曲线
key_term_tenor = [0, 1, 3, 6, 12, 24, 36, 60, 84, 120, 240, 360, 1200] # month
key_term_interest = [0, 0.049206, 0.049206, 0.050475, 0.050166, 0.046579, 0.043151, 0.040502, 0.039244, 0.038166, 0.040554, 0.038661, 0.038661]
key_term_spread = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
spot_dates = [ql.Date(18, 2, 2023) + ql.Period(round(tenor), ql.Months) for tenor in key_term_tenor] # Use the evaluation date
spot_rates = [x + y for x, y in zip(key_term_interest, key_term_spread)]
spot_curve = ql.ZeroCurve(
spot_dates,
spot_rates,
paymentConvention,
calendar,
ql.Linear(),
ql.Compounded,
ql.Annual
)
# 债券
pricing_curve = ql.YieldTermStructureHandle(spot_curve)
bond = ql.FixedRateBond(
settlementDays,
faceAmount,
schedule,
[coupon],
paymentConvention
)
bond.setPricingEngine(ql.DiscountingBondEngine(pricing_curve))
print(bond.cleanPrice())
print(bond.dirtyPrice())
# 调试信息
print(bond.settlementDays())
print(ql.Settings.instance().evaluationDate)
print(bond.settlementDate())运行上述代码,你会发现bond.settlementDate()输出的日期是February 21st, 2023,这是因为February 18th, 2023是星期六,February 19th, 2023是星期日,而February 20th, 2023是美国总统日,属于银行假日。因此,结算日期被推迟到了February 21st, 2023,与债券的到期日重合,导致价格为零。
解决方案
要解决这个问题,可以尝试以下方法:
修改后的代码示例:
import QuantLib as ql
# 债券信息和设置
settlementDays = 0
settlementDate = ql.Date('2023-02-17', '%Y-%m-%d') # Modified settlement date
effectiveDate = ql.Date('2019-08-21', '%Y-%m-%d')
terminationDate = ql.Date('2023-02-21', '%Y-%m-%d')
faceAmount = 100
coupon = 0.0625
frequency = ql.Period(2)
paymentConvention = ql.Thirty360(ql.Thirty360.ISMA)
calendar = ql.UnitedStates(ql.UnitedStates.NYSE)
# 评估日期
ql.Settings.instance().evaluationDate = ql.Date(17, 2, 2023) # 设置评估日期
# schedule
schedule = ql.Schedule(
effectiveDate,
terminationDate,
frequency,
calendar,
ql.Unadjusted,
ql.Unadjusted,
ql.DateGeneration.Backward,
False
)
# 定价曲线
key_term_tenor = [0, 1, 3, 6, 12, 24, 36, 60, 84, 120, 240, 360, 1200] # month
key_term_interest = [0, 0.049206, 0.049206, 0.050475, 0.050166, 0.046579, 0.043151, 0.040502, 0.039244, 0.038166, 0.040554, 0.038661, 0.038661]
key_term_spread = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
spot_dates = [ql.Date(17, 2, 2023) + ql.Period(round(tenor), ql.Months) for tenor in key_term_tenor] # Use the evaluation date
spot_rates = [x + y for x, y in zip(key_term_interest, key_term_spread)]
spot_curve = ql.ZeroCurve(
spot_dates,
spot_rates,
paymentConvention,
calendar,
ql.Linear(),
ql.Compounded,
ql.Annual
)
# 债券
pricing_curve = ql.YieldTermStructureHandle(spot_curve)
bond = ql.FixedRateBond(
settlementDays,
faceAmount,
schedule,
[coupon],
paymentConvention
)
bond.setPricingEngine(ql.DiscountingBondEngine(pricing_curve))
print(bond.cleanPrice())
print(bond.dirtyPrice())
# 调试信息
print(bond.settlementDays())
print(ql.Settings.instance().evaluationDate)
print(bond.settlementDate())通过将评估日期设置为February 17th, 2023,并相应调整定价曲线的节点日期,就可以得到一个合理的债券价格。
Z-Spread的计算
至于Z-Spread的计算,可以使用二分法或其他优化算法,找到使 spread-adjusted-bond-price 等于目标价格的 spread 值。QuantLib本身并没有直接计算Z-Spread的函数,需要自行实现。
总结与注意事项
在使用Python QuantLib进行债券定价时,需要特别注意以下几点:
通过仔细检查这些细节,可以避免债券定价出现错误,并获得准确的结果。
以上就是Python QuantLib债券定价问题:价格为零的排查与解决的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号