簡體   English   中英

QuantLib:交換到零息曲線自舉

[英]QuantLib: Swap to zero-coupon curve bootstrapping

我試圖將美國掉期曲線引導成零息曲線(見彭博截圖)。 我有從 1996 年 6 月 21 日到今天的 3M、1Y、2Y、5Y、7Y、10Y 到期日的每日數據(有些日子不見了,請參閱 DataFrame 屏幕截圖)。 我想獲得從 3M 到 10Y 的所有期限的相應零利率。

彭博社 5 年美國掉期截圖

交換曲線數據

這是我寫的一段代碼:

zero_rates = {}
helpers_mat =[]
# loop over time
for t in range(irs.shape[0]):
    # date of pricing (= DataFrame index line by line)
    pricing_date = ql.DateParser.parseFormatted(str(irs.index[t]),'%Y-%m-%d')
    # define evaluation date
    ql.Settings.instance().evaluationDate = pricing_date
    # list of maturities and corresponding swap rates
    for m in irs.columns:
        helpers_mat.append([(m, ql.Months), irs[m][t]])

    # helper function to intput details
    helpers = [ql.SwapRateHelper(
                           ql.QuoteHandle(ql.SimpleQuote(rate/100.0)),
                           ql.Period(*tenor),
                           ql.TARGET(),
                           ql.Semiannual,
                           ql.ModifiedFollowing,
                           ql.Thirty360(),
                           ql.Euribor3M())
                           for tenor, rate in helpers_mat]

    # build zero-coupon curve (cubic spline interpolation)
    zc_curve = ql.PiecewiseCubicZero(pricing_date, helpers, ql.Actual360())
    # pre-allocate
    zero_rate = []
    tenors = []
    # loop over wanted maturities
    for n in np.arange(3, 120+1, 3):
        # maturities
        yrs = n/12.0
        tenors.append(yrs)
        # extract zero-coupon rate
        zc_rate = zc_curve.zeroRate(yrs, ql.Compounded, ql.Annual).rate()
        zero_rate.append(zc_rate*100)
        # pandas export
        zero_rates[t] = pd.DataFrame(np.transpose(list(zip(zero_rate))), columns = list(zip(tenors)), 
        index = [irs.index[t]])

zero_rates = pd.concat(zero_rates, axis=0)

使用該代碼,我得到以下錯誤:

RuntimeError: 不止一種儀器與支柱 1996 年 7 月 25 日

所以我想我對日期有疑問。 我的理解是SwapRateHelper具有掉期固定腿的特點。 但是我在哪里輸入浮動腿的?

浮動腿參數來自索引。 在您的情況下,您似乎正在使用 ql.Euribor3M() ,它與您真正想要的 USD Libor 3M 足夠接近。 區別基本上是錯誤的日歷。

但是,不知道數據的形狀很難找出問題所在。

有許多不同的方法可以構建輔助函數,對於美元曲線,您不妨將 USDLibor 指數用於存款,將 ql.UsdLiborSwapIsdaFixAm 用於掉期。

檢查我根據您的數據制作的這個示例是否有幫助......

import pandas as pd
import QuantLib as ql

data = [
    {'Date': '1996-06-21 00:00:00', '3M': 5.57813, '1Y': 6.19, '2Y': 6.55, '5Y': 7.02, '7Y': 7.17, '10Y': 7.31},
    {'Date': '1996-06-24 00:00:00', '3M': 5.56641, '1Y': 6.18, '2Y': 6.54, '5Y': 6.99, '7Y': 7.14, '10Y': 7.31},
    {'Date': '1996-06-25 00:00:00', '3M': 5.5625, '1Y': 6.155, '2Y': 6.52, '5Y': 6.99, '7Y': 7.15, '10Y': 7.31},
    {'Date': '1996-06-26 00:00:00', '3M': 5.57031, '1Y': 6.14, '2Y': 6.52, '5Y': 6.94, '7Y': 7.1, '10Y': 7.27},
    {'Date': '1996-06-27 00:00:00', '3M': 5.59766, '1Y': 6.16, '2Y': 6.5, '5Y': 6.93, '7Y': 7.09, '10Y': 7.26},
    {'Date': '1996-06-28 00:00:00', '3M': 5.58203, '1Y': 6.075, '2Y': 6.44, '5Y': 6.87, '7Y': 7.03, '10Y': 7.2},
    {'Date': '1996-07-01 00:00:00', '3M': 5.5625, '1Y': 6.03, '2Y': 6.33, '5Y': 6.75, '7Y': 6.92, '10Y': 7.09},
    {'Date': '1996-07-02 00:00:00', '3M': 5.5625, '1Y': 6.05, '2Y': 6.36, '5Y': 6.77, '7Y': 6.93, '10Y': 7.11},
    {'Date': '1996-07-03 00:00:00', '3M': 5.59766, '1Y': 6.11, '2Y': 6.43, '5Y': 6.84, '7Y': 7.0, '10Y': 7.17},
    {'Date': '1996-07-04 00:00:00', '3M': 5.57031, '1Y': 6.07, '2Y': 6.37, '5Y': 6.79, '7Y': 6.95, '10Y': 7.13},
    {'Date': '1996-07-05 00:00:00', '3M': 5.57422, '1Y': 6.075, '2Y': 6.4, '5Y': 6.8, '7Y': 6.96, '10Y': 7.13},
    {'Date': '1996-07-08 00:00:00', '3M': 5.6875, '1Y': 6.33, '2Y': 6.69, '5Y': 7.11, '7Y': 7.27, '10Y': 7.44},
    {'Date': '1996-07-09 00:00:00', '3M': 5.6875, '1Y': 6.31, '2Y': 6.64, '5Y': 7.07, '7Y': 7.23, '10Y': 7.41},
    {'Date': '1996-07-10 00:00:00', '3M': 5.6875, '1Y': 6.28, '2Y': 6.545, '5Y': 6.985, '7Y': 7.145, '10Y': 7.325},
    {'Date': '1996-07-11 00:00:00', '3M': 5.6875, '1Y': 6.22, '2Y': 6.465, '5Y': 6.925, '7Y': 7.085, '10Y': 7.255},
    {'Date': '1996-07-12 00:00:00', '3M': 5.67578, '1Y': 6.17, '2Y': 6.465, '5Y': 6.905, '7Y': 7.055, '10Y': 7.225},
    {'Date': '1996-07-15 00:00:00', '3M': 5.67578, '1Y': 6.19, '2Y': 6.485, '5Y': 6.925, '7Y': 7.075, '10Y': 7.245},
    {'Date': '1996-07-16 00:00:00', '3M': 5.68359, '1Y': 6.22, '2Y': 6.425, '5Y': 6.855, '7Y': 7.025, '10Y': 7.205}
]

calendar = ql.UnitedStates()
zeros = []
deposits = ['3M']
swaps = ['1Y', '2Y', '5Y', '7Y', '10Y']
for row in data:
    # Build Curve for the date
    curve_date = ql.Date(row['Date'][:10], '%Y-%m-%d')
    ql.Settings.instance().evaluationDate = curve_date
    spot_date = calendar.advance(curve_date, 2, ql.Days)
    helpers = ql.RateHelperVector()
    for tenor in deposits:
        index = ql.USDLibor(ql.Period(tenor))
        helpers.append(
            ql.DepositRateHelper(row[tenor] / 100, index)
        )
    for tenor in swaps:
        swap_index = ql.UsdLiborSwapIsdaFixAm(ql.Period(tenor))
        helpers.append(
            ql.SwapRateHelper(row[tenor] / 100, swap_index)
        )
    curve = ql.PiecewiseCubicZero(curve_date, helpers, ql.Actual360())

    # Get Zero Rates
    for tenor in deposits + swaps:
        date = calendar.advance(spot_date, ql.Period(tenor))
        rate = curve.zeroRate(date, ql.Actual360(), ql.Compounded, ql.Annual).rate()
        zeros.append({ 'curve_date': curve_date, 'tenor': tenor, 'zero_rate': rate})

pd.DataFrame(zeros)

定義存款助手時,您有多種選擇。

假設您有以下參數:

tenor = ql.Period('6M')
fixingDays = 2
calendar = ql.SouthAfrica()
convention = ql.ModifiedFollowing
endOfMonth = False
dayCounter = ql.Actual365Fixed()

(i) 創建自定義索引而不是使用可用模板。 在這里,您必須為其提供相關參數。

myindex = ql.IborIndex('MyIndex', tenor, fixingDays, currency, calendar, convention, endOfMonth, dayCounter)
helper = ql.DepositRateHelper(rate, myindex)

(ii) 或者在 DepositHelper 中定義所有這些參數:

helper = ql.DepositRateHelper(rate, tenor, fixingDays, calendar, convention, endOfMonth, dayCounter)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM