Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#167 Modified Duration is Wrong QuantLib 1.4

None
closed-invalid
None
5
1 day ago
2014-04-04
Mauricio Bedoya
No

Relation between modified and Macaulay duration doesn't remain.(Source: QuantLib reference:link URL)

/******************************************
 Fixed Income Mathematics Four Edition
 pag (216)
 Bond Information:
 5-Years, 10% coupon, 10% Required Yield
 *****************************************/
#include <iostream>
#include <ql/quantlib.hpp>
#include <algorithm>

using namespace QuantLib;
using namespace std;

int main()
{
    try{
        /***************************************** Bond Information **********************************/
        Calendar calendar = NullCalendar();
        DayCounter DC = ActualActual(ActualActual::Bond);
        Natural SettlementDays = 3;
        Date Today = Date::todaysDate();
        Settings::instance().evaluationDate() = Today;
        Date Issue = Today;
        Date Maturity = Issue+ 5*Years;
        Rate Coupon = 0.10;
        InterestRate CouponRate(Coupon, ActualActual(ActualActual::Bond), Compounded, Semiannual);
        Real FaceValue = 100.0;
        std::vector`<InterestRate>` Coupons(10,CouponRate);

        RelinkableHandle<YieldTermStructure>FlatRate;
        RelinkableHandle<FixedRateBond>BondInstrument;

        /**************************************** Bond Schedule ****************************************/
        Schedule sch(Issue, Maturity, Period(Semiannual), calendar, Unadjusted, Unadjusted, DateGeneration::Backward, false);

        /************************************* Bond Constructor ****************************************/
        boost::shared_ptr<FixedRateBond> Bond1(new FixedRateBond(SettlementDays, FaceValue, sch, Coupons));
        BondInstrument.linkTo(Bond1);
        Rate rate = 0.10;
        Handle<Quote> Rate1(new SimpleQuote(rate));

        /********************************** TermStructure Constructor ***********************************/
        boost::shared_ptr<YieldTermStructure> TS(new FlatForward(Issue, Rate1, ActualActual(ActualActual::Bond),Compounded, Semiannual));
        FlatRate.linkTo(TS);
        boost::shared_ptr<PricingEngine> BondEngine(new DiscountingBondEngine(FlatRate));
        BondInstrument->setPricingEngine(BondEngine);

        /******************************************** Print *********************************************/
        cout << "Bond1 Results \n";
        cout << "Book Price: " << 100.0 << endl;
        cout << "QuantLib: " << BondInstrument->NPV() << "(Ok, is Par)" << endl;
        cout << "Duration (Simple):" << BondFunctions::duration(**BondInstrument, InterestRate(rate, DC, Compounded, Annual),Duration::Simple) << "  vs Book: 4.05 (Ok)" << endl;
        cout << "Duration (Macaulay):" << BondFunctions::duration(**BondInstrument, InterestRate(rate, DC, Compounded, Annual),Duration::Macaulay) << "  vs Book: 4.05 (Ok)"<< endl;
        cout << "Duration (Modified):" << BondFunctions::duration(**BondInstrument, InterestRate(rate, DC, Compounded, Annual),Duration::Modified) << "  vs Book: 3.86 (Wrong in QuantLib)" << endl;
    }

    catch (std::exception& e) {
    cerr << e.what() << endl;
    }
}

Related

Bugs: #167

Discussion

  • Luigi Ballabio
    Luigi Ballabio
    2014-04-16

    The relation only works if the compounding frequency for the yield matches that of the coupons. If you pass to the duration method an interest rate

    InterestRate(rate, DC, Compounded, Semiannual)
    

    then the results match those you expect.

     
    • Thanks for your promptly answer.
      Best regards
      Mauricio

      To: 167@bugs.quantlib.p.re.sf.net
      From: lballabio@users.sf.net
      Subject: [quantlib:bugs] #167 Modified Duration is Wrong QuantLib 1.4
      Date: Wed, 16 Apr 2014 14:05:24 +0000

      The relation only works if the compounding frequency for the yield matches that of the coupons. If you pass to the duration method an interest rate

      InterestRate(rate, DC, Compounded, Semiannual)

      then the results match those you expect.

      [bugs:#167] Modified Duration is Wrong QuantLib 1.4

      Status: open

      Group:

      Created: Fri Apr 04, 2014 11:00 AM UTC by Mauricio Bedoya

      Last Updated: Fri Apr 04, 2014 11:00 AM UTC

      Owner: Luigi Ballabio

      Relation between modified and Macaulay duration doesn't remain.(Source: QuantLib reference:link URL)

      /******
      Fixed Income Mathematics Four Edition
      pag (216)
      Bond Information:
      5-Years, 10% coupon, 10% Required Yield
      *******/

      include <iostream>

      include <ql quantlib.hpp="">

      include <algorithm>

      using namespace QuantLib;
      using namespace std;

      int main()
      {
      try{
      /******** Bond Information ******/
      Calendar calendar = NullCalendar();
      DayCounter DC = ActualActual(ActualActual::Bond);
      Natural SettlementDays = 3;
      Date Today = Date::todaysDate();
      Settings::instance().evaluationDate() = Today;
      Date Issue = Today;
      Date Maturity = Issue+ 5
      Years;
      Rate Coupon = 0.10;
      InterestRate CouponRate(Coupon, ActualActual(ActualActual::Bond), Compounded, Semiannual);
      Real FaceValue = 100.0;
      std::vector<InterestRate> Coupons(10,CouponRate);

          RelinkableHandle<YieldTermStructure>FlatRate;
          RelinkableHandle<FixedRateBond>BondInstrument;
      
          /**************************************** Bond Schedule ****************************************/
          Schedule sch(Issue, Maturity, Period(Semiannual), calendar, Unadjusted, Unadjusted, DateGeneration::Backward, false);
      
          /************************************* Bond Constructor ****************************************/
          boost::shared_ptr<FixedRateBond> Bond1(new FixedRateBond(SettlementDays, FaceValue, sch, Coupons));
          BondInstrument.linkTo(Bond1);
          Rate rate = 0.10;
          Handle<Quote> Rate1(new SimpleQuote(rate));
      
          /********************************** TermStructure Constructor ***********************************/
          boost::shared_ptr<YieldTermStructure> TS(new FlatForward(Issue, Rate1, ActualActual(ActualActual::Bond),Compounded, Semiannual));
          FlatRate.linkTo(TS);
          boost::shared_ptr<PricingEngine> BondEngine(new DiscountingBondEngine(FlatRate));
          BondInstrument->setPricingEngine(BondEngine);
      
          /******************************************** Print *********************************************/
          cout << "Bond1 Results \n";
          cout << "Book Price: " << 100.0 << endl;
          cout << "QuantLib: " << BondInstrument->NPV() << "(Ok, is Par)" << endl;
          cout << "Duration (Simple):" << BondFunctions::duration(**BondInstrument, InterestRate(rate, DC, Compounded, Annual),Duration::Simple) << "  vs Book: 4.05 (Ok)" << endl;
          cout << "Duration (Macaulay):" << BondFunctions::duration(**BondInstrument, InterestRate(rate, DC, Compounded, Annual),Duration::Macaulay) << "  vs Book: 4.05 (Ok)"<< endl;
          cout << "Duration (Modified):" << BondFunctions::duration(**BondInstrument, InterestRate(rate, DC, Compounded, Annual),Duration::Modified) << "  vs Book: 3.86 (Wrong in QuantLib)" << endl;
      }
      
      catch (std::exception& e) {
      cerr << e.what() << endl;
      }
      

      }

      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/quantlib/bugs/167/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       

      Related

      Bugs: #167

      Attachments
  • Luigi Ballabio
    Luigi Ballabio
    2014-04-17

    • status: open --> closed-invalid
    • Group: -->