Posted by
Irakli Machabeli-2 on
URL: http://quantlib.414.s1.nabble.com/Bootstrapping-US-Treasury-Curve-with-fake-zero-coupon-bonds-tp8072p8074.html
Luigi,
I get this error if I try to bootstrap curve from c# code
1st iteration: could not bootstrap the 1st instrument, maturity March 19th,
2009: root not bracketed: f[2.22045e-016,3] ->
[-9.996249e+001,-4.730857e+001]
I made sample using C++ too. Now in C++ sample I'm actually getting memory
corruption error in some cases.
If you exclude 1st 3 bonds in source code all tests pass smoothly (it works
also from c# side).
With those 3 bonds included you get an error
One year zero=0.558764 % Actual/Actual (ISDA) simple compounding
done fitCurve<Discount,LogLinear,IterativeBootstrap>
failed fitCurve<ForwardRate,Cubic,IterativeBootstrap>
1st iteration: could not bootstrap the 3rd instrument, maturity November
19th, 2
009: root not bracketed: f[2.22045e-016,3] -> [4.166294e-001,3.803735e+001]
One year zero=0.525826 % Actual/Actual (ISDA) simple compounding
done fitCurve<ZeroYield,Cubic,IterativeBootstrap>
One year zero=0.547035 % Actual/Actual (ISDA) simple compounding
done fitCurve<ForwardRate,ConvexMonotone,IterativeBootstrap>
If you do not catch exception in code debugger shows that there is memory
corruption : Unhandled exception at 0x004b8ee0 in
mytestsValuation-vc80-mt-sgd.exe: 0xC0000005: Access violation reading
location 0xfeeefef2.
so I guess "root not bracketed" is not a real issue
By the way is you change frequency for 1st 3 bonds from Annual to Monthly
( which should nor really matter since coupon is really small one more test
fails)
Anyway attached is the sample :
#include <iostream>
#include <ql/quantlib.hpp>
using namespace std;
using namespace QuantLib;
#if defined(QL_ENABLE_SESSIONS)
namespace QuantLib {
Integer sessionId() { return 0; }
}
#endif
#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
struct BondData {
Date issueDate;
Date redemptionDate;
Frequency frequency;
Rate coupon;
Real price;
};
BondData bondData[] = {
{Date(18,(Month)9,2008),Date(19,(Month)3,2009), Annual,0.01,99.9600},
{Date(18,(Month)12,2008),Date(18,(Month)6,2009), Annual,0.01,99.7350},
{Date(20,(Month)11,2008),Date(19,(Month)11,2009), Annual,0.01,99.5300},
{Date(1,(Month)12,2008),Date(30,(Month)11,2010),
Semiannual,1.25,100.9531},
{Date(1,(Month)12,2008),Date(30,(Month)11,2013), Semiannual,2,102.3281},
{Date(17,(Month)11,2008),Date(15,(Month)11,2018),
Semiannual,3.75,110.7813},
{Date(15,(Month)8,2008),Date(15,(Month)5,2038), Semiannual,4.5,130.5000}
};
Date today=Date(16,(Month)12,2008);//calendar.adjust(Date::todaysDate());
template <class T, class I, template<class C> class B>
void fitCurve(const I& interpolator = I(),
Real tolerance = 1.0e-9)
{
Size bondCount = LENGTH(bondData);
Calendar calendar= JointCalendar(
UnitedStates(UnitedStates::Market::GovernmentBond ),
UnitedKingdom(UnitedKingdom::Market::Settlement ),
JointCalendarRule::JoinHolidays );
//Natural settlementDays;
std::vector<boost::shared_ptr<SimpleQuote> >prices(bondCount);
std::vector<Schedule> schedules(bondCount);
Settings::instance().evaluationDate() = today;
Natural bondSettlementDays=2;
Date settlementDay= calendar.advance(today,bondSettlementDays,Days);
DayCounter bondDayCounter= ActualActual();
BusinessDayConvention bondConvention= Following;
Real bondRedemption=100;
std::vector<boost::shared_ptr<RateHelper> >
bondHelpers(bondCount );
for (Size i=0; i<bondCount ; i++)
{
prices[i]= boost::shared_ptr<SimpleQuote>( new
SimpleQuote(bondData[i].price) );
std::vector<Rate> coupons(1, bondData[i].coupon/100.0);
schedules[i] = Schedule( bondData[i].issueDate,
bondData[i].redemptionDate ,
Period(bondData[i].frequency),
calendar,
bondConvention, bondConvention,
DateGeneration::Backward, false);
Handle<Quote> p(prices[i]);
bondHelpers[i] = boost::shared_ptr<RateHelper>(new
FixedRateBondHelper(p,
bondSettlementDays,
bondRedemption, schedules[i],
coupons, bondDayCounter,
bondConvention,
bondRedemption,
bondData[i].issueDate));
}
boost::shared_ptr<YieldTermStructure> yc(
new PiecewiseYieldCurve<T,I,B>(bondSettlementDays,
calendar,
bondHelpers,
bondDayCounter));
InterestRate r= yc->zeroRate(1, Compounding::Simple );
Date maxD= yc->maxDate();
cout<< "One year zero="<<r<<endl;// <<" maxDate= "<<maxD <<endl;
}
int main(int, char* [])
{
//fitCurve<ForwardRate,Cubic,IterativeBootstrap>();
try
{
fitCurve<Discount,LogLinear,IterativeBootstrap>();
cout<<"done fitCurve<Discount,LogLinear,IterativeBootstrap>";
}
catch( QuantLib::Error& e )
{
cout<<"failed
fitCurve<Discount,LogLinear,IterativeBootstrap>"<<endl<<e.what();
}
cout<<endl<<endl;
try
{
fitCurve<ForwardRate,Cubic,IterativeBootstrap>();
cout<<"done fitCurve<ForwardRate,Cubic,IterativeBootstrap>";
}
catch( QuantLib::Error& e )
{
cout<<"failed
fitCurve<ForwardRate,Cubic,IterativeBootstrap>"<<endl<<e.what();
}
cout<<endl<<endl;
try
{
fitCurve<ZeroYield,Cubic,IterativeBootstrap>();
cout<<"done fitCurve<ZeroYield,Cubic,IterativeBootstrap>";
}
catch( QuantLib::Error& e )
{
cout<<"failed
fitCurve<ZeroYield,Cubic,IterativeBootstrap>"<<endl<<e.what();
}
cout<<endl<<endl;
try
{
fitCurve<ForwardRate,ConvexMonotone,IterativeBootstrap>();
cout<<"done
fitCurve<ForwardRate,ConvexMonotone,IterativeBootstrap>"<<endl<<endl;
}
catch( QuantLib::Error& e )
{
cout<<"failed
fitCurve<ForwardRate,ConvexMonotone,IterativeBootstrap>"<<endl<<e.what();
}
cout<<endl<<endl;
char c;
cin>>c;
}
--------------------------------------------------
From: "Luigi Ballabio" <
[hidden email]>
Sent: Wednesday, December 17, 2008 10:15 AM
To: "Irakli Machabeli" <
[hidden email]>
Cc: <
[hidden email]>
Subject: Re: [Quantlib-users] Bootstrapping US Treasury Curve with fake zero
coupon bonds
> On Wed, 2008-11-19 at 09:38 -0500, Irakli Machabeli wrote:
>> I'm trying to bootstrap treasury curve with cubic spline including
>> t-bill( short dated with no coupon)
>> Since quantlib doesn't support zeros I add fixed rate bond helper with
>> real price and very small coupon.
>> During bootstrap I get an error
>
> Apologies for the delay---can you post the code that triggers the
> problem?
>
> Luigi
>
>
> --
>
> There's no sense in being precise when you don't even know what you're
> talking about.
> -- John von Neumann
>
>
>
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users