PiecewiseYieldCurve ZeroRate

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

PiecewiseYieldCurve ZeroRate

George Cowie

  I feel like this flavor of question has been asked before on this forum, but I can't seem to figure out what I'm doing wrong.  I would expect the following code to return pretty much the same rates I input into the RateHelpers, but that's not the case.  

  Am I incorrect about the expected functionality, or do I have something wrong with my code?

Thanks,
George

Output:

Period,Input Rate,Curve Rate
1W,0.0012075,0.00120749
1M,0.001515,0.00138513
2M,0.001935,0.00176613
3M,0.0022535,0.00209296
6M,0.00323,0.00306812
1Y,0.00549,0.005284

Code:

#include <ql/quantlib.hpp>

using namespace std ;
using namespace QuantLib ;

int main(int argc, char** argv)
{
  vector<Rate> liborRates ;
  liborRates.push_back(0.12075 / 100.0) ; //1W
  liborRates.push_back(0.1515 / 100.0) ; //1M
  liborRates.push_back(0.1935 / 100.0) ; //2M
  liborRates.push_back(0.22535 / 100.0) ; //3M
  liborRates.push_back(0.323 / 100.0) ; //6M
  liborRates.push_back(0.549 / 100.0) ; //12M

  vector<Period> periods ;
  periods.push_back(Period(1,Weeks)) ;
  periods.push_back(Period(1,Months)) ;
  periods.push_back(Period(2,Months)) ;
  periods.push_back(Period(3,Months)) ;
  periods.push_back(Period(6,Months)) ;
  periods.push_back(Period(12,Months)) ;

  vector<boost::shared_ptr<RateHelper> > instruments ;

  for (vector<int>::size_type i = 0 ; i < liborRates.size() ; i++)
    {
      boost::shared_ptr<Quote> liborQuote(new SimpleQuote(liborRates[i])) ;
      Handle<Quote> liborHandle(liborQuote) ;
      boost::shared_ptr<IborIndex> liborIndex(new USDLibor(periods[i])) ;
      boost::shared_ptr<RateHelper> rateHelper(new DepositRateHelper(liborHandle,liborIndex)) ;
      instruments.push_back(rateHelper) ;
    }

  Calendar calendar = TARGET ();
  Date today (29 , Apr ,2014);
  Natural settlementDays = 2;
  Date settlement = calendar.advance ( today , settlementDays , Days );
  DayCounter dc = Actual360 ();
  boost::shared_ptr <YieldTermStructure> yieldCurve ;
  yieldCurve = boost::shared_ptr <YieldTermStructure>(new
     PiecewiseYieldCurve < ZeroYield , Linear >( settlement , instruments , dc ));

  cout << "Period,Input Rate,Curve Rate" << endl ;
  for(vector<int>::size_type i = 0 ; i < periods.size() ; i++)
    {
      Rate curveRate = Rate(yieldCurve->zeroRate(settlement + periods[i], dc, Continuous)) ;
      cout << periods[i] << "," << liborRates[i] << "," << curveRate << endl ;
    }

  return 0 ;
}


------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: PiecewiseYieldCurve ZeroRate

Luigi Ballabio
Hi,
    it was a combination of things:
1) once you've chosen today's date, you should also add
    Settings::instance().evaluationDate() = today;
to set it as the evaluation date, otherwise some calculations might
not be consistent;
2) Libor rates are simply compounded, but you're asking the curve for
continuously compounded rates. You should use
    yieldCurve->zeroRate(d, dc, Simple);
3) you're using TARGET to calculate the spot date, but the USD libor
uses the US calendar. Use
    Calendar calendar = UnitedStates();
instead;
4) if you just ask for the rates at settlement + periods[i], you might
end up on a holiday or a weekend. Use
    calendar.adjust(settlement + periods[i])
instead, where calendar is as above.

Modifying your program as above, I get the correct rates.

Luigi

On Wed, May 7, 2014 at 2:02 PM, George Cowie <[hidden email]> wrote:

>
>   I feel like this flavor of question has been asked before on this forum,
> but I can't seem to figure out what I'm doing wrong.  I would expect the
> following code to return pretty much the same rates I input into the
> RateHelpers, but that's not the case.
>
>   Am I incorrect about the expected functionality, or do I have something
> wrong with my code?
>
> Thanks,
> George
>
> Output:
>
> Period,Input Rate,Curve Rate
> 1W,0.0012075,0.00120749
> 1M,0.001515,0.00138513
> 2M,0.001935,0.00176613
> 3M,0.0022535,0.00209296
> 6M,0.00323,0.00306812
> 1Y,0.00549,0.005284
>
> Code:
>
> #include <ql/quantlib.hpp>
>
> using namespace std ;
> using namespace QuantLib ;
>
> int main(int argc, char** argv)
> {
>   vector<Rate> liborRates ;
>   liborRates.push_back(0.12075 / 100.0) ; //1W
>   liborRates.push_back(0.1515 / 100.0) ; //1M
>   liborRates.push_back(0.1935 / 100.0) ; //2M
>   liborRates.push_back(0.22535 / 100.0) ; //3M
>   liborRates.push_back(0.323 / 100.0) ; //6M
>   liborRates.push_back(0.549 / 100.0) ; //12M
>
>   vector<Period> periods ;
>   periods.push_back(Period(1,Weeks)) ;
>   periods.push_back(Period(1,Months)) ;
>   periods.push_back(Period(2,Months)) ;
>   periods.push_back(Period(3,Months)) ;
>   periods.push_back(Period(6,Months)) ;
>   periods.push_back(Period(12,Months)) ;
>
>   vector<boost::shared_ptr<RateHelper> > instruments ;
>
>   for (vector<int>::size_type i = 0 ; i < liborRates.size() ; i++)
>     {
>       boost::shared_ptr<Quote> liborQuote(new SimpleQuote(liborRates[i])) ;
>       Handle<Quote> liborHandle(liborQuote) ;
>       boost::shared_ptr<IborIndex> liborIndex(new USDLibor(periods[i])) ;
>       boost::shared_ptr<RateHelper> rateHelper(new
> DepositRateHelper(liborHandle,liborIndex)) ;
>       instruments.push_back(rateHelper) ;
>     }
>
>   Calendar calendar = TARGET ();
>   Date today (29 , Apr ,2014);
>   Natural settlementDays = 2;
>   Date settlement = calendar.advance ( today , settlementDays , Days );
>   DayCounter dc = Actual360 ();
>   boost::shared_ptr <YieldTermStructure> yieldCurve ;
>   yieldCurve = boost::shared_ptr <YieldTermStructure>(new
>      PiecewiseYieldCurve < ZeroYield , Linear >( settlement , instruments ,
> dc ));
>
>   cout << "Period,Input Rate,Curve Rate" << endl ;
>   for(vector<int>::size_type i = 0 ; i < periods.size() ; i++)
>     {
>       Rate curveRate = Rate(yieldCurve->zeroRate(settlement + periods[i],
> dc, Continuous)) ;
>       cout << periods[i] << "," << liborRates[i] << "," << curveRate << endl
> ;
>     }
>
>   return 0 ;
> }
>
>
> ------------------------------------------------------------------------------
> Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
> &#149; 3 signs your SCM is hindering your productivity
> &#149; Requirements for releasing software faster
> &#149; Expert tips and advice for migrating your SCM now
> http://p.sf.net/sfu/perforce
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>



--
<https://implementingquantlib.blogspot.com>
<https://twitter.com/lballabio>

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: PiecewiseYieldCurve ZeroRate

George Cowie
Thanks Luigi, this worked great!  Here's the corrected output/code:


Output:

Period,Input Rate,Curve Rate

1W,0.0012075,0.0012075

1M,0.001515,0.001515

2M,0.001935,0.001935

3M,0.0022535,0.0022535

6M,0.00323,0.00323

1Y,0.00549,0.00549


Code:

#include <ql/quantlib.hpp>


using namespace std ;

using namespace QuantLib ;


int main(int argc, char** argv)                                                                                                            

{                                                                                                                                          

  vector<Rate> liborRates ;                                                                                                                

  liborRates.push_back(0.12075 / 100.0) ; //1W                                                                                             

  liborRates.push_back(0.1515 / 100.0) ; //1M                                                                                              

  liborRates.push_back(0.1935 / 100.0) ; //2M                                                                                              

  liborRates.push_back(0.22535 / 100.0) ; //3M                                                                                             

  liborRates.push_back(0.323 / 100.0) ; //6M                                                                                               

  liborRates.push_back(0.549 / 100.0) ; //12M                                                                                              

                                                                                                                                           

  vector<Period> periods ;                                                                                                                 

  periods.push_back(Period(1,Weeks)) ;                                                                                                     

  periods.push_back(Period(1,Months)) ;                                                                                                    

  periods.push_back(Period(2,Months)) ;                                                                                                    

  periods.push_back(Period(3,Months)) ;                                                                                                    

  periods.push_back(Period(6,Months)) ;                                                                                                    

  periods.push_back(Period(12,Months)) ;                                                                                                   

                                                                                                                                           

  vector<boost::shared_ptr<RateHelper> > instruments ;                                                                                     

                                                                                                                                           

  for (vector<int>::size_type i = 0 ; i < liborRates.size() ; i++)                                                                         

    {                                                                                                                                      

      boost::shared_ptr<Quote> liborQuote(new SimpleQuote(liborRates[i])) ;                                                                

      Handle<Quote> liborHandle(liborQuote) ;                                                                                              

      boost::shared_ptr<IborIndex> liborIndex(new USDLibor(periods[i])) ;                                                                  

      boost::shared_ptr<RateHelper> rateHelper(new DepositRateHelper(liborHandle,liborIndex)) ;                                            

      instruments.push_back(rateHelper) ;                                                                                                  

    }                                                                                                                                      

                                                                                                                                           

  Calendar calendar = UnitedStates();                                                                                                      

  Date today (29 , Apr ,2014);                                                                                                             

  Settings::instance().evaluationDate() = today ;                                                                                          

  Natural settlementDays = 2;                                                                                                              

  Date settlement = calendar.advance ( today , settlementDays , Days );                                                                    

  DayCounter dc = Actual360 ();                                                                                                            

  boost::shared_ptr <YieldTermStructure> yieldCurve ;                                                                                      

  yieldCurve = boost::shared_ptr <YieldTermStructure>(new                                                                                  

                                                      PiecewiseYieldCurve < ZeroYield , Linear >( settlement , instruments , dc ));        

                                                                                                                                           

  cout << "Period,Input Rate,Curve Rate" << endl ;                                                                                         

  for(vector<int>::size_type i = 0 ; i < periods.size() ; i++)                                                                             

    {                                                                                                                                      

      Rate curveRate = Rate(yieldCurve->zeroRate(calendar.adjust(settlement + periods[i]), dc, Simple)) ;                                  

      cout << periods[i] << "," << liborRates[i] << "," << curveRate << endl ;                                                             

    }                                                                                                                                      

                                                                                                                                           

  return 0 ;                                                                                                                               

}



On Wed, May 7, 2014 at 7:26 AM, Luigi Ballabio <[hidden email]> wrote:
Hi,
    it was a combination of things:
1) once you've chosen today's date, you should also add
    Settings::instance().evaluationDate() = today;
to set it as the evaluation date, otherwise some calculations might
not be consistent;
2) Libor rates are simply compounded, but you're asking the curve for
continuously compounded rates. You should use
    yieldCurve->zeroRate(d, dc, Simple);
3) you're using TARGET to calculate the spot date, but the USD libor
uses the US calendar. Use
    Calendar calendar = UnitedStates();
instead;
4) if you just ask for the rates at settlement + periods[i], you might
end up on a holiday or a weekend. Use
    calendar.adjust(settlement + periods[i])
instead, where calendar is as above.

Modifying your program as above, I get the correct rates.

Luigi

On Wed, May 7, 2014 at 2:02 PM, George Cowie <[hidden email]> wrote:
>
>   I feel like this flavor of question has been asked before on this forum,
> but I can't seem to figure out what I'm doing wrong.  I would expect the
> following code to return pretty much the same rates I input into the
> RateHelpers, but that's not the case.
>
>   Am I incorrect about the expected functionality, or do I have something
> wrong with my code?
>
> Thanks,
> George
>
> Output:
>
> Period,Input Rate,Curve Rate
> 1W,0.0012075,0.00120749
> 1M,0.001515,0.00138513
> 2M,0.001935,0.00176613
> 3M,0.0022535,0.00209296
> 6M,0.00323,0.00306812
> 1Y,0.00549,0.005284
>
> Code:
>
> #include <ql/quantlib.hpp>
>
> using namespace std ;
> using namespace QuantLib ;
>
> int main(int argc, char** argv)
> {
>   vector<Rate> liborRates ;
>   liborRates.push_back(0.12075 / 100.0) ; //1W
>   liborRates.push_back(0.1515 / 100.0) ; //1M
>   liborRates.push_back(0.1935 / 100.0) ; //2M
>   liborRates.push_back(0.22535 / 100.0) ; //3M
>   liborRates.push_back(0.323 / 100.0) ; //6M
>   liborRates.push_back(0.549 / 100.0) ; //12M
>
>   vector<Period> periods ;
>   periods.push_back(Period(1,Weeks)) ;
>   periods.push_back(Period(1,Months)) ;
>   periods.push_back(Period(2,Months)) ;
>   periods.push_back(Period(3,Months)) ;
>   periods.push_back(Period(6,Months)) ;
>   periods.push_back(Period(12,Months)) ;
>
>   vector<boost::shared_ptr<RateHelper> > instruments ;
>
>   for (vector<int>::size_type i = 0 ; i < liborRates.size() ; i++)
>     {
>       boost::shared_ptr<Quote> liborQuote(new SimpleQuote(liborRates[i])) ;
>       Handle<Quote> liborHandle(liborQuote) ;
>       boost::shared_ptr<IborIndex> liborIndex(new USDLibor(periods[i])) ;
>       boost::shared_ptr<RateHelper> rateHelper(new
> DepositRateHelper(liborHandle,liborIndex)) ;
>       instruments.push_back(rateHelper) ;
>     }
>
>   Calendar calendar = TARGET ();
>   Date today (29 , Apr ,2014);
>   Natural settlementDays = 2;
>   Date settlement = calendar.advance ( today , settlementDays , Days );
>   DayCounter dc = Actual360 ();
>   boost::shared_ptr <YieldTermStructure> yieldCurve ;
>   yieldCurve = boost::shared_ptr <YieldTermStructure>(new
>      PiecewiseYieldCurve < ZeroYield , Linear >( settlement , instruments ,
> dc ));
>
>   cout << "Period,Input Rate,Curve Rate" << endl ;
>   for(vector<int>::size_type i = 0 ; i < periods.size() ; i++)
>     {
>       Rate curveRate = Rate(yieldCurve->zeroRate(settlement + periods[i],
> dc, Continuous)) ;
>       cout << periods[i] << "," << liborRates[i] << "," << curveRate << endl
> ;
>     }
>
>   return 0 ;
> }
>
>
> ------------------------------------------------------------------------------
> Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
> &#149; 3 signs your SCM is hindering your productivity
> &#149; Requirements for releasing software faster
> &#149; Expert tips and advice for migrating your SCM now
> http://p.sf.net/sfu/perforce
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>



--
<https://implementingquantlib.blogspot.com>
<https://twitter.com/lballabio>


------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users