Re: error in building yieldtermstructure using T-Bills, T-Notes, and T-Bond

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/error-in-building-yieldtermstructure-using-T-Bills-T-Notes-and-T-Bond-tp14209p14210.html

Hello,
    this usually points to a data problem; the bracketing error means
that, given the previous nodes, the curve can't find a value of the
5-years forward that gives the bond price you quoted. What is the
error message you're getting? (I mean, with xMin_ and the other
variables filled in). That will give you the range that the curve is
trying.  You can:
- check that you're allowing negative rates.  This is the default in
the most recent release, but you might be using an older one.  You can
check ql/userconfig.hpp if you're using Windows or ql/config.hpp on
other platforms; in either case, QL_NEGATIVE_RATES should be defined.
You can also see it from the error message; if negative rates are not
enabled, it will show 0 as the lower bound.
- you can also try bootstrapping the curve without the 5-years node,
enable extrapolation (after the curve is built, or it will give you
the second error you got--you were trying to modify a curve which
didn't exist yet) and try to price the 5-years bond.  This might give
you an idea of how far your quote is from what the curve so far is
implying; if they're too different, it might be likely that the 5-year
node alone can't bring the price to the quoted one, no matter how the
bootstrap process tries to move it.

Luigi


On Mon, Apr 15, 2013 at 5:06 AM, hudsoncity <[hidden email]> wrote:

> Hi, I am new to QuantLib. I am trying to build Treasury Curve using
> piecewiseYieldCurve by using T-Bills, T-Notes, and T-Bond. I am pretty much
> copied the code from bonds.cpp. It doesn't give the error with T-Bills plus
> 2 yr and 3yr term T-Notes. But once adding longer term T-Note or T-Bond, it
> gives error at line 202 of solver1d.hpp:
>             QL_REQUIRE(fxMin_*fxMax_ < 0.0,
>                        "root not bracketed: f["
>                        << xMin_ << "," << xMax_ << "] -> ["
>                        << std::scientific
>                        << fxMin_ << "," << fxMax_ << "]");
>
> Here is the code:
> int main(int, char* []) {
>         try{
>                 boost::timer timer;
>         std::cout << std::endl;
>
>                 Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);;
>                 Date settlementDate(6, April, 2013);
>                 cout << "Is BD:" << calendar.isBusinessDay( settlementDate ) << std::endl
> ;
>                 cout << "Is Holiday :" << calendar.isHoliday( settlementDate ) <<
> std::endl ;
>                 cout << "Is Weekend :" << calendar.isWeekend( Saturday ) << std::endl;
>                 cout << "Is Last BD :" << calendar.isEndOfMonth( settlementDate) <<
> std::endl;
>
>                 settlementDate = calendar.adjust(settlementDate);
>
>                 Integer fixingDays = 1;
>         Natural settlementDays = 1;
>
>         Date todaysDate = calendar.advance(settlementDate, -fixingDays,
> Days);
>         // nothing to do with Date::todaysDate
>         Settings::instance().evaluationDate() = todaysDate;
>
>         std::cout << "Today: " << todaysDate.weekday()
>         << ", " << todaysDate << std::endl;
>
>         std::cout << "Settlement date: " << settlementDate.weekday()
>         << ", " << settlementDate << std::endl;
>
>                  // ZC rates for the short end
>                  // use the quote in wsj:
> http://online.wsj.com/mdc/public/page/2_3020-treasury.html#treasuryB
>          Rate TB4WKsQuote=0.045;
>          Rate TB13WKsQuote=0.065;
>          Rate TB26WKSQuote=0.095;
>                  Rate TB52WKSQuote=0.130;
>
>                  //pointer to the quote
>          boost::shared_ptr TB4WKsRate(new SimpleQuote(TB4WKsQuote));
>          boost::shared_ptr TB13WKsRate(new SimpleQuote(TB13WKsQuote));
>          boost::shared_ptr TB26WKSRate(new SimpleQuote(TB26WKSQuote));
>                  boost::shared_ptr TB52WKSRate(new SimpleQuote(TB52WKSQuote));
>
>                  //Treasury securities use actual/actual day count convention
>                  DayCounter zcBondsDayCounter = ActualActual();
>                  Date d1 (1,Oct ,2012);
>                 Date d2=d1 +2* Months ;
>                 std :: cout << " Days Between d1/ d2:" <<zcBondsDayCounter.dayCount (d1
> ,d2) << std :: endl ;
>                 std :: cout << " Year Fraction d1 /d2:"
> <<zcBondsDayCounter.yearFraction (d1 ,d2) << std :: endl ;
>
>                 //pointer to the instrument
>          boost::shared_ptr<RateHelper> zc4WK(new DepositRateHelper(
>                  Handle(TB4WKsRate),
>                  4*Weeks, fixingDays,
>                  calendar, ModifiedFollowing,
>                  true, zcBondsDayCounter));
>          boost::shared_ptr<RateHelper> zc13WK(new DepositRateHelper(
>                  Handle(TB13WKsRate),
>                  13*Weeks, fixingDays,
>                  calendar, ModifiedFollowing,
>                  true, zcBondsDayCounter));
>          boost::shared_ptr<RateHelper> zc26WK(new DepositRateHelper(
>                  Handle(TB26WKSRate),
>                  26*Weeks, fixingDays,
>                  calendar, ModifiedFollowing,
>                  true, zcBondsDayCounter));
>          boost::shared_ptr<RateHelper> zc52WK(new DepositRateHelper(
>                  Handle(TB52WKSRate),
>                  52*Weeks, fixingDays,
>                  calendar, ModifiedFollowing,
>                  true, zcBondsDayCounter));
>
>                  //set up the on the run bond
>                          Real redemption = 100.0;
>
>         const Size numberOfBonds = 6;
>
>         Date issueDates[] = {
>                 Date (1, April, 2013),
>                 Date (15, March, 2013),
>                 Date (1, April, 2013),
>                 Date (1, April, 2013),
>                 Date (15, March, 2013),
>                                 Date (15, March, 2013)
>         };
>
>         Date maturities[] = {
>                 Date (31, March, 2015),
>                 Date (15, March, 2016),
>                 Date (31, March, 2018),
>                 Date (31, March, 2020),
>                 Date (15, February, 2023),
>                                 Date (15, February, 2043)
>         };
>
>         Real couponRates[] = {
>                 0.25,
>                 0.375,
>                 0.750,
>                 1.125 ,
>                 2.0,
>                                 3.125
>         };
>
>         Real marketQuotes[] = {
>                 100.0391 ,
>                 100.1484 ,
>                 100.3594 ,
>                 100.1094 ,
>                 102.7422 ,
>                                 105.2422
>                 };
>
>                 //pointer to the quote
>         std::vector< boost::shared_ptr<SimpleQuote> > quote;
>         for (Size i=0; i<numberOfBonds; i++) {
>             boost::shared_ptr<SimpleQuote> cp(new
> SimpleQuote(marketQuotes[i]));
>             quote.push_back(cp);
>         }
>
>                 //pointer to the pointer of the quote
>         RelinkableHandle quoteHandle[numberOfBonds];
>         for (Size i=0; i<numberOfBonds; i++) {
>             quoteHandle[i].linkTo(quote[i]);
>         }
>
>         // Definition of the rate helpers
>         std::vector<boost::shared_ptr<FixedRateBondHelper> >
> bondsHelpers;
>
>                 //pointer to the bond instruments
>         for (Size i=0; i<numberOfBonds; i++) {
>
>             Schedule schedule(issueDates[i], maturities[i],
> Period(Semiannual), UnitedStates(UnitedStates::GovernmentBond),
>                     Unadjusted, Unadjusted, DateGeneration::Backward,
> false);
>
>             boost::shared_ptr<FixedRateBondHelper> bondHelper(new
> FixedRateBondHelper(
>                     quoteHandle[i],
>                     settlementDays,
>                     100.0,
>                     schedule,
>                     std::vector<Rate>(1,couponRates[i]),
>                     ActualActual(ActualActual::Bond),
>                     Unadjusted,
>                     redemption,
>                     issueDates[i]));
>
>             bondsHelpers.push_back(bondHelper);
>         }
>
>                  /*********************
>          **  CURVE BUILDING **
>          *********************/
>          // ActualActual::ISDA ensures that 30 years is 30.0
>          DayCounter termStructureDayCounter =
> ActualActual(ActualActual::ISDA);
>
>                  double tolerance = 1.0e-15;
>
>          // A depo-bond curve
>          std::vector<boost::shared_ptr<RateHelper> > bondInstruments;
> //covers all types of bond
>
>                  //add T-bills to the bondInstruments vector for 4-52 weeks
>          bondInstruments.push_back(zc4WK);
>          bondInstruments.push_back(zc13WK);
>          bondInstruments.push_back(zc26WK);
>                  bondInstruments.push_back(zc52WK);
>
>                  bondInstruments.push_back(bondsHelpers[0]);            //2 yr T-Note
>                  bondInstruments.push_back(bondsHelpers[1]);            //3 yr T-Note
>                  bondInstruments.push_back(bondsHelpers[2]);            //5 yr T-Note, resulting
> error
>
>                  //build the yieldtermstructure
>         boost::shared_ptr<YieldTermStructure> bondDiscountingTermStructure(
>                  new PiecewiseYieldCurve<Discount,LogLinear>(
>                          settlementDate, bondInstruments,
>                          termStructureDayCounter,
>                          tolerance));
>
> ----------------
> I saw in one thread that the similar problem solved by adding line
> yieldtermstructure->enableExtrapolation();
> I tried but it causes the error at line 88 of errors.cpp:
>         throw std::runtime_error(format(file, line, function,
>                                         "Boost assertion failed: " +
>                                         std::string(expr)));
>
> Here is the code for invoking enableExtrapolation:
>                  boost::shared_ptr<YieldTermStructure> bondDiscountingTermStructure;
>                  bondDiscountingTermStructure->enableExtrapolation();
>
>          bondDiscountingTermStructure =
> boost::shared_ptr<YieldTermStructure> (
>                  new PiecewiseYieldCurve<Discount,LogLinear>(settlementDate,
> bondInstruments,
>                          termStructureDayCounter,tolerance));
>
> I have stuck on this issue for about a week. Your help will be really
> appreciated.
>
>
>
>
> --
> View this message in context: http://quantlib.10058.n7.nabble.com/error-in-building-yieldtermstructure-using-T-Bills-T-Notes-and-T-Bond-tp14209.html
> Sent from the quantlib-users mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Precog is a next-generation analytics platform capable of advanced
> analytics on semi-structured data. The platform includes APIs for building
> apps and a phenomenal toolset for data science. Developers can use
> our toolset for easy data analysis & visualization. Get a free account!
> http://www2.precog.com/precogplatform/slashdotnewsletter
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users