我一直在尝试使用QuantLib(Python)建立债券曲线,但是我获得的合理数量的证券的拟合度确实很差(直线)。也许我需要校准参数(simplexLambda或迭代次数或精度)以使其起作用,但是我还没有找到解决方案。
我已经包含了创建一些债券,然后使用SvenssonFitting()和FittedBondDiscountCurve()生成收益曲线的代码。
import QuantLib as ql
from matplotlib import pyplot as plt
ql_date = ql.Date(1,7,2019)
BOND_METADATA = [[99.40625, '2017-07-17', '2020-07-15', 0.015],
[101.4101563, '2018-07-16', '2021-07-15', 0.02625],
[99.59765625, '2017-06-30', '2022-06-30', 0.0175],
[102.7695313, '2018-07-02', '2023-06-30', 0.02625],
[100.2695313, '2017-06-30', '2024-06-30', 0.02],
[104.265625, '2018-07-02', '2025-06-30', 0.0275],
[96.3125, '2016-08-15', '2026-08-15', 0.015],
[101.171875, '2017-08-15', '2027-08-15', 0.0225],
[106.1328125, '2018-08-15', '2028-08-15', 0.02875],
[108.3125, '2018-11-15', '2028-11-15', 0.03125],
[116.9609375, '2009-02-17', '2039-02-15', 0.035],
[133.1171875, '2009-08-17', '2039-08-15', 0.045],
[123, '2010-08-16', '2040-08-15', 0.03875],
[120.8984375, '2011-08-15', '2041-08-15', 0.0375],
[103.2265625, '2012-08-15', '2042-08-15', 0.0275],
[109.71875, '2014-08-15', '2044-08-15', 0.03125],
[92.7265625, '2016-08-15', '2046-08-15', 0.0225],
[107.546875, '2018-08-15', '2048-08-15', 0.03],
[115.46875, '2018-11-15', '2048-11-15', 0.03375]]
bond_helpers = []
for i,(price,issue_date,maturity_date,coupon) in enumerate(BOND_METADATA):
face_value = 100.
settlement_days = 1
issue_date = ql.Date(issue_date,"%Y-%m-%d")
maturity_date = ql.Date(maturity_date,"%Y-%m-%d")
schedule = ql.Schedule(issue_date,
maturity_date,
ql.Period(6,ql.Months),
ql.UnitedStates(),
ql.Unadjusted,
ql.Unadjusted,
ql.DateGeneration.Backward,
False)
security = ql.FixedRateBond(settlement_days,
face_value,
schedule,
[coupon],
ql.ActualActual()
)
bond_helpers.append(ql.FixedRateBondHelper(ql.QuoteHandle(ql.SimpleQuote(price)),
security.settlementDays(),
security.notional(),
schedule,
[security.nextCouponRate()],
security.dayCounter(),
ql.Following))
guess = []
simplexLambda = 5
accuracy = 1e-8
numIter = 100000
method = ql.SvenssonFitting()
yield_curve = ql.FittedBondDiscountCurve(ql_date,
bond_helpers,
ql.ActualActual(),
method,
accuracy,
numIter,
guess,
5)
#---------------------------------------------------------
t0 = yield_curve.referenceDate()
x = [(t0+i).to_date() for i in range(2500)]
y = [yield_curve.forwardRate(t0+i,t0+i+1,ql.ActualActual(),ql.Compounded,ql.Semiannual).rate() for i in range(2500)]
plt.plot(x,y,color='orange')
我得到的拟合看起来像一条直线,它不应该给出输入的产量。可以将图片发布为支持文档。