Posted by
hudsoncity on
URL: http://quantlib.414.s1.nabble.com/error-in-building-yieldtermstructure-using-T-Bills-T-Notes-and-T-Bond-tp14209.html
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<Quote> TB4WKsRate(new SimpleQuote(TB4WKsQuote));
boost::shared_ptr<Quote> TB13WKsRate(new SimpleQuote(TB13WKsQuote));
boost::shared_ptr<Quote> TB26WKSRate(new SimpleQuote(TB26WKSQuote));
boost::shared_ptr<Quote> 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<Quote>(TB4WKsRate),
4*Weeks, fixingDays,
calendar, ModifiedFollowing,
true, zcBondsDayCounter));
boost::shared_ptr<RateHelper> zc13WK(new DepositRateHelper(
Handle<Quote>(TB13WKsRate),
13*Weeks, fixingDays,
calendar, ModifiedFollowing,
true, zcBondsDayCounter));
boost::shared_ptr<RateHelper> zc26WK(new DepositRateHelper(
Handle<Quote>(TB26WKSRate),
26*Weeks, fixingDays,
calendar, ModifiedFollowing,
true, zcBondsDayCounter));
boost::shared_ptr<RateHelper> zc52WK(new DepositRateHelper(
Handle<Quote>(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<Quote> 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.