Quantlib
Quantlib 自然三次樣條收益率曲線
在 Quantlib python 中是否有使用自然三次樣條插值作為產量曲線的範例?我可以從SWIG 文件中看到插值已公開,但不確定如何使用它。
我可以看到在piecewiseyieldcurve文件中公開了一些插值方法。是我應該使用的嗎?
QuantLib 有幾種收益率曲線的插值方法。以下是葡萄牙政府債券幫助您入門的一些方法範例。
import QuantLib as ql import pandas as pd pgbs = pd.DataFrame( {'maturity': ['15-06-2020', '15-04-2021', '17-10-2022', '25-10-2023', '15-02-2024', '15-10-2025', '21-07-2026', '14-04-2027', '17-10-2028', '15-06-2029', '15-02-2030', '18-04-2034', '15-04-2037', '15-02-2045'], 'coupon': [4.8, 3.85, 2.2, 4.95, 5.65, 2.875, 2.875, 4.125, 2.125, 1.95, 3.875, 2.25, 4.1, 4.1], 'px': [102.532, 105.839, 107.247, 119.824, 124.005, 116.215, 117.708, 128.027, 115.301, 114.261, 133.621, 119.879, 149.427, 159.177]}) calendar = ql.TARGET() today = calendar.adjust(ql.Date(19, 12, 2019)) ql.Settings.instance().evaluationDate = today bondSettlementDays = 2 bondSettlementDate = calendar.advance( today, ql.Period(bondSettlementDays, ql.Days)) frequency = ql.Annual dc = ql.ActualActual(ql.ActualActual.ISMA) accrualConvention = ql.ModifiedFollowing convention = ql.ModifiedFollowing redemption = 100.0 instruments = [] for idx, row in pgbs.iterrows(): maturity = ql.Date(row.maturity, '%d-%m-%Y') schedule = ql.Schedule( bondSettlementDate, maturity, ql.Period(frequency), calendar, accrualConvention, accrualConvention, ql.DateGeneration.Backward, False) helper = ql.FixedRateBondHelper( ql.QuoteHandle(ql.SimpleQuote(row.px)), bondSettlementDays, 100.0, schedule, [row.coupon / 100], dc, convention, redemption) instruments.append(helper) params = [bondSettlementDate, instruments, dc] methods = { 'logLinearDiscount': ql.PiecewiseLogLinearDiscount(*params), 'logCubicDiscount': ql.PiecewiseLogCubicDiscount(*params), 'linearZero': ql.PiecewiseLinearZero(*params), 'cubicZero': ql.PiecewiseCubicZero(*params), 'linearForward': ql.PiecewiseLinearForward(*params), 'splineCubicDiscount': ql.PiecewiseSplineCubicDiscount(*params), } pgbs.index = pd.to_datetime(pgbs.maturity) for method in methods: pgbs[method] = pgbs.maturity.apply( lambda x: methods[method].zeroRate( ql.Date(x, '%d-%m-%Y'), dc, ql.Compounded, frequency ).rate()*100 ) pgbs
除了插值之外,您還可以查看 FittedBondDiscountCurve 類,其中有多種擬合方法(CubicBSplinesFitting、SimplePolynomialFitting、NelsonSiegelFitting、SvenssonFitting、ExponentialSplinesFitting)