在进行压力测试时,将 QuantLib 债券定价与 Excel 函数 YIELD 和 PRICE 进行比较
Compare QuantLib bond pricing with Excel functions YIELD and PRICE when doing stress testing
我在 Excel 和 Python Quantlib 中计算了债券价格和压力债券价格(提高收益率)。如下 table 所示,产生了奇怪的结果:基础债券价格在 Excel 和 Quantlib 之间匹配良好,但压力债券价格有更多差距(1.5% 相对差异)。收益率也显示出一些差距。
你能提供一些意见吗?
Excel代码:
Base Bond Price=PRICE("2021-8-19","2025-8-19",4.5%,YIELD("2021-8-19","2025-8-
19",4.5%,95,100,4,0),100,4,0)
Stress Bond Price=PRICE("2021-8-19","2025-8-19",4.5%,YIELD("2021-8-19","2025-8-
19",4.5%,95,100,4,0)+662/10000,100,4,0)
Python代码:
import datetime
import QuantLib as ql
settlement_date = ql.Date(19,8,2021)
valuation_date = ql.Date(19,8,2021)
issue_date = ql.Date(19,8,2021)
maturity_date = ql.Date(19,8,2025)
tenor = ql.Period(4)
calendar = ql.UnitedStates()
business_convention = ql.Following
date_generation = ql.DateGeneration.Backward
end_month = False
face_value = 100
coupon_rate = 450/10000
day_count = ql.Thirty360(ql.Thirty360.USA)
redemption_value = 100
schedule = ql.Schedule(issue_date, maturity_date, tenor, calendar, business_convention, business_convention, date_generation, end_month)
bond = ql.FixedRateBond(settlement_date-valuation_date, face_value, schedule, [coupon_rate], day_count, business_convention, redemption_value, issue_date)
target_price = 95
bond_yield = bond.bondYield(target_price, day_count, ql.Compounded, 4, ql.Date(), 1.0e-8,1000)
bond_price = bond.cleanPrice(bond_yield, day_count, ql.Compounded, 4)
STRESS = 662
stress_bond_yield = bond_yield+STRESS/10000
stress_bond_price = bond.cleanPrice(stress_bond_yield, day_count, ql.Compounded, 4)
excel_base_bond_price = 99.50
excel_stress_bond_price = 75.02971569
print('Base bond price from excel is', excel_base_bond_price )
print('Base bond price from Quantlib is', bond_price)
print('Stressed bond price from excel is',excel_stress_bond_price)
print('Stressed bond price from Quantlib is',stress_bond_price)
您需要添加
ql.Settings.instance().evaluationDate = valuation_date
计算前。如果你不这样做,你将计算今天而不是估值日期的收益率和价格。一旦你这样做了,收益率和价格就会匹配得更好。
我在 Excel 和 Python Quantlib 中计算了债券价格和压力债券价格(提高收益率)。如下 table 所示,产生了奇怪的结果:基础债券价格在 Excel 和 Quantlib 之间匹配良好,但压力债券价格有更多差距(1.5% 相对差异)。收益率也显示出一些差距。
你能提供一些意见吗?
Excel代码:
Base Bond Price=PRICE("2021-8-19","2025-8-19",4.5%,YIELD("2021-8-19","2025-8-
19",4.5%,95,100,4,0),100,4,0)
Stress Bond Price=PRICE("2021-8-19","2025-8-19",4.5%,YIELD("2021-8-19","2025-8-
19",4.5%,95,100,4,0)+662/10000,100,4,0)
Python代码:
import datetime
import QuantLib as ql
settlement_date = ql.Date(19,8,2021)
valuation_date = ql.Date(19,8,2021)
issue_date = ql.Date(19,8,2021)
maturity_date = ql.Date(19,8,2025)
tenor = ql.Period(4)
calendar = ql.UnitedStates()
business_convention = ql.Following
date_generation = ql.DateGeneration.Backward
end_month = False
face_value = 100
coupon_rate = 450/10000
day_count = ql.Thirty360(ql.Thirty360.USA)
redemption_value = 100
schedule = ql.Schedule(issue_date, maturity_date, tenor, calendar, business_convention, business_convention, date_generation, end_month)
bond = ql.FixedRateBond(settlement_date-valuation_date, face_value, schedule, [coupon_rate], day_count, business_convention, redemption_value, issue_date)
target_price = 95
bond_yield = bond.bondYield(target_price, day_count, ql.Compounded, 4, ql.Date(), 1.0e-8,1000)
bond_price = bond.cleanPrice(bond_yield, day_count, ql.Compounded, 4)
STRESS = 662
stress_bond_yield = bond_yield+STRESS/10000
stress_bond_price = bond.cleanPrice(stress_bond_yield, day_count, ql.Compounded, 4)
excel_base_bond_price = 99.50
excel_stress_bond_price = 75.02971569
print('Base bond price from excel is', excel_base_bond_price )
print('Base bond price from Quantlib is', bond_price)
print('Stressed bond price from excel is',excel_stress_bond_price)
print('Stressed bond price from Quantlib is',stress_bond_price)
您需要添加
ql.Settings.instance().evaluationDate = valuation_date
计算前。如果你不这样做,你将计算今天而不是估值日期的收益率和价格。一旦你这样做了,收益率和价格就会匹配得更好。