简体   繁体   English

Quantlib-给一个奇怪的错误

[英]Quantlib - giving a strange error

I am using Quantlib to bootstrap a curve and then get the discounting rate. 我正在使用Quantlib引导曲线,然后获得折现率。

In general the usual steps work fine. 通常,通常的步骤可以正常工作。

However, for the data given below, it throws a strange error. 但是,对于下面给出的数据,它将引发一个奇怪的错误。

Code: 码:

def create_ois_swaps(self, ois_swap_rates, helpers=None):
    ''' Creates a OIS rate helper from incoming OIS rates
        Input:
        ois_swap_rates: list of tuples comprising of (start_date, end_date, rate, label)
    '''
    if self.helpers is None:
        self.helpers = [
            DatedOISRateHelper(start_date, end_date, QuoteHandle(SimpleQuote(rate / 100)), self.ff_local)
            for start_date, end_date, rate in [tuple((fixed_bond.pydate_to_qldate(sd),
                                                      fixed_bond.pydate_to_qldate(ed),
                                                      rate)) for sd, ed, rate, label
                                               in ois_swap_rates if label not in ['ONTN', 'TN']]]
    else:
        self.helpers += [DatedOISRateHelper(start_date,
                                            end_date,
                                            QuoteHandle(SimpleQuote(rate / 100)), self.ff_local)
                         for start_date, end_date, rate in [tuple((fixed_bond.pydate_to_qldate(sd),
                                                                   fixed_bond.pydate_to_qldate(ed),
                                                                   rate)) for sd, ed, rate, label
                                                            in ois_swap_rates if label not in ['ONTN', 'TN']]]
    # for start_date, end_date, rate in ois_swap_rates]
    self.ois_curve_c = PiecewiseLogCubicDiscount(0, self.calendar, self.helpers, Actual365Fixed())
    self.ois_curve_c.enableExtrapolation()

def bootstrap_usd_ois_3M_curve(self,
                               usd_3M_swap_rates,
                               discountCurve,
                               bootStrapMethod=BootStrapMethod.PiecewiseLogCubicDiscount):

    discount_curve = RelinkableYieldTermStructureHandle()
    discount_curve.linkTo(discountCurve)
    self.helpers += [SwapRateHelper(QuoteHandle(SimpleQuote(rate / 100)),
                                    Period(int(label[:-1]), Years),
                                    TARGET(),
                                    Semiannual,
                                    Unadjusted,
                                    Thirty360(Thirty360.BondBasis),
                                    Euribor3M(),
                                    QuoteHandle(),
                                    Period(0, Days),
                                    discount_curve)
                     for sd, ed, rate, label in usd_3M_swap_rates if label not in ['ONTN', 'TN']]
    # for rate, tenor in usd_3M_swap_rates]

    if bootStrapMethod == BootStrapMethod.PiecewiseLogCubicDiscount:
        self.usd_3M_c = PiecewiseLogCubicDiscount(0, TARGET(), self.helpers, Actual365Fixed())
    elif bootStrapMethod == BootStrapMethod.PiecewiseFlatForward:
        self.usd_3M_c = PiecewiseFlatForward(0, TARGET(), self.helpers, Actual365Fixed())

    # Also, we enable extrapolation beyond the maturity of the last helper; that is mostly
    # for convenience as we retrieve rates to plot the curve near its far end.
    self.usd_3M_c.enableExtrapolation()

in my main code, I call the above 2 functions as:- 在我的主要代码中,我将上述两个函数称为:

create the OIS curve 创建OIS曲线

usd_ois.create_ois_swaps(ois_rate_ql)

bootstrap the curve 引导曲线

usd_ois.bootstrap_usd_ois_3M_curve(usd_3M_swap_rates=libor_rate_ql, discountCurve=usd_ois.ois_curve_c, bootStrapMethod=BootStrapMethod.PiecewiseFlatForward)

Dates: 日期:

Curve valuation date: 2017.01.02 曲线评估日期:2017.01.02

for discounting_Rate:- 对于Discounting_Rate:-

start_date: 2017.01.02 end_date: 2018.01.01 dayCount: ACT/360 开始日期:2017.01.02结束日期:2018.01.01天计数:ACT / 360

Error message: 错误信息:

return _QuantLib.YieldTermStructure_forwardRate(self, *args) RuntimeError: negative time (-0.00273973) given` 返回_QuantLib.YieldTermStructure_forwardRate(self,* args)RuntimeError:给定负时间(-0.00273973)

Curve object 曲线对象

curve object Data used: libor and OIS rates used for bootstrapping 曲线对象 使用的数据: 用于引导的libor和OIS速率

State of curve object 曲线对象的状态

state of curve object 曲线对象的状态

Notice I have a 1) discounting OIS curve and a 2) forward 3M curve 注意,我有一个1)贴现OIS曲线和一个2)向前3M曲线

Valuation date is Jan 2nd , 2017 评估日期为2017年1月2日

The call I am making is on the discounting curve as follows:- ois_curve.ois_curve_c.forwardRate(pydate_to_qldate(start_date), pydate_to_qldate(end_date), daycount, Simple).rate() * 100 我正在拨打的电话在折价曲线上如下所示: ois_curve.ois_curve_c.forwardRate(pydate_to_qldate(start_date), pydate_to_qldate(end_date), daycount, Simple).rate() * 100

where start_Date = 2nd Jan, 2017 end_date = 2nd Jan, 2018 其中start_Date = 2017年1月2日end_date = 2018年1月2日

I am running the same code over a range of dates. 我在一段时间内运行相同的代码。 Most of the dates- its successful, but few of the dates- it strangely throws this error 大多数日期-成功,但很少日期-奇怪地抛出此错误

For reference, I'm summarizing here the comments above. 作为参考,我在这里总结上面的评论。 The evaluation date (January 2nd, 2017) is a holiday for the calendar used by the curve; 评估日期(2017年1月2日)是曲线使用的日历的假期; therefore, the curve moves its reference date to the next business day. 因此,曲线将其参考日期移至下一个工作日。 At that point, January 2nd is in the past as far as the curve is concerned and asking for a rate on that date results in an error. 届时,就曲线而言,1月2日已过去,要求该日期的汇率会导致错误。

I agree that, at least, the error should be made more readable. 我同意,至少应该使该错误更具可读性。 I'm not sure that setting the evaluation date to a holiday should necessarily invalidate the curve. 我不确定将评估日期设置为假期是否必然会使曲线无效。 Throwing an error is probably not feasible; 抛出错误可能是不可行的。 we might have different curves using different calendars, and setting the evaluation date to a holiday for one of those should be ok as long as we only use the valid curves. 我们可能会使用不同的日历使用不同的曲线,并且只要我们仅使用有效曲线,就可以将其中之一的评估日期设置为假日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM