Shocking the Zero Curve

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

Shocking the Zero Curve

rob.philipp
The PiecewiseZeroSpreadedTermStructure class does not shock correctly when the compounding is different from continuous. In particular, if you shock the zero curve (compounded, annual for example) by 50 bps and then take the difference between the base and the shocked zero curves, the difference is not 50 bps as you would expect. Instead the difference ranges from about 50 bps to 52 bps.

I believe that the way forward rates are shocked is incorrect. In the code, the spread (shock) is simply added to the forward curve of the base. The forward curve should be based on the shocked zero curve instead. Has anyone fixed that? If not, I'll try to fix that as well.

Not sure how best to contribute the code. Is there a staging area for checking in the code?

I marked the areas changed, and commented out code from the base version. Following code (QuanlLib-1.0) is also attached:
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2006 Roland Lichters
 Copyright (C) 2006, 2008 StatPro Italia srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 [hidden email]. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

/*! \file piecewisezerospreadedtermstructure.hpp
    \brief Piecewise-zero-spreaded term structure
*/

#ifndef quantlib_piecewise_zero_spreaded_term_structure_hpp
#define quantlib_piecewise_zero_spreaded_term_structure_hpp

#include <ql/termstructures/yield/zeroyieldstructure.hpp>
#include <ql/quote.hpp>
#include <vector>

namespace QuantLib {

  //! Term structure with an added vector of spreads on the zero-yield rate
  /*! The zero-yield spread at any given date is linearly interpolated
      between the input data.

      \note This term structure will remain linked to the original
            structure, i.e., any changes in the latter will be
            reflected in this structure as well.

      \ingroup yieldtermstructures
  */
  class PiecewiseZeroSpreadedTermStructure : public ZeroYieldStructure {
    public:
      PiecewiseZeroSpreadedTermStructure(
                                   const Handle<YieldTermStructure>&,
                                   const std::vector<Handle<Quote> >& spreads,
                                   const std::vector<Date>& dates,
                                   // added (RP, 2010.08.23)
                                   Compounding comp = Continuous,
                                   Frequency freq = NoFrequency,
                                   const DayCounter& dc = DayCounter());
      //! \name YieldTermStructure interface
      //@{
      DayCounter dayCounter() const;
      Natural settlementDays() const;
      Calendar calendar() const;
      const Date& referenceDate() const;
      Date maxDate() const;
      //@}
    protected:
      //! returns the spreaded zero yield rate
      Rate zeroYieldImpl(Time) const;
      // added (RP, 20100823)
      //! returns the spreaded forward rate
      Rate forwardImpl(Time) const;
      void update();
    private:
      void updateTimes();
      const double calcSpread( Time t ) const;
      Handle<YieldTermStructure> originalCurve_;
      std::vector<Handle<Quote> > spreads_;
      std::vector<Date> dates_;
      std::vector<Time> times_;
      // added (RP, 2010.08.23)
      Compounding comp_;
      Frequency freq_;
      DayCounter dc_;
  };


    // inline definitions

    inline
    PiecewiseZeroSpreadedTermStructure::PiecewiseZeroSpreadedTermStructure(
                                   const Handle<YieldTermStructure>& h,
                                   const std::vector<Handle<Quote> >& spreads,
                                   const std::vector<Date>& dates,
                                   // added (RP, 2010.08.23)
                                   Compounding comp,
                                   Frequency freq,
                                   const DayCounter& dc)
    : originalCurve_(h), spreads_(spreads), dates_(dates),
      times_(dates_.size()),
      // added (RP, 2010.08.23)
      comp_(comp), freq_(freq), dc_(dc) {
        QL_REQUIRE(!spreads_.empty(), "no spreads given");
        QL_REQUIRE(spreads_.size() == dates_.size(),
                   "spread and date vector have different sizes");
        registerWith(originalCurve_);
        for (Size i = 0; i < spreads_.size(); i++)
            registerWith(spreads_[i]);
        updateTimes();
    }

    inline DayCounter PiecewiseZeroSpreadedTermStructure::dayCounter() const {
        return originalCurve_->dayCounter();
    }

    inline Calendar PiecewiseZeroSpreadedTermStructure::calendar() const {
        return originalCurve_->calendar();
    }

    inline Natural PiecewiseZeroSpreadedTermStructure::settlementDays() const {
        return originalCurve_->settlementDays();
    }

    inline const Date&
    PiecewiseZeroSpreadedTermStructure::referenceDate() const {
        return originalCurve_->referenceDate();
    }

    inline Date PiecewiseZeroSpreadedTermStructure::maxDate() const {
        return std::min(originalCurve_->maxDate(), dates_.back());
    }

    inline Rate
    PiecewiseZeroSpreadedTermStructure::zeroYieldImpl(Time t) const {
        //Rate z = originalCurve_->zeroRate(t, Continuous, NoFrequency, true);
        //if (t <= times_.front()) {
        //    return z + spreads_.front()->value();
        //} else if (t >= times_.back()) {
        //    return z + spreads_.back()->value();
        //} else {
        //    Size i;
        //    for (i = 0; i < times_.size(); i++)
        //        if (times_[i] > t) break;
        //    Time dt = times_[i] - times_[i-1];
        //    return z + spreads_[i]->value() * (t - times_[i-1]) / dt
        //        + spreads_[i-1]->value() * (times_[i] - t) / dt;
        //}

        // added (RP, 20100823)
        double spread = calcSpread( t );
        InterestRate zeroRate = originalCurve_->zeroRate(t, comp_, freq_, true);
        InterestRate spreadedRate(zeroRate + spread,
                                  zeroRate.dayCounter(),
                                  zeroRate.compounding(),
                                  zeroRate.frequency());
        return spreadedRate.equivalentRate(Continuous, NoFrequency, t);
    }

    inline const double
    PiecewiseZeroSpreadedTermStructure::calcSpread( Time t ) const {
        double spread = 0.0;
        if (t <= times_.front()) {
            spread = spreads_.front()->value();
        } else if (t >= times_.back()) {
            spread = spreads_.back()->value();
        } else {
            Size i;
            for (i = 0; i < times_.size(); i++)
                if (times_[i] > t) break;
            Time dt = times_[i] - times_[i-1];
            spread = spreads_[i]->value() * (t - times_[i-1]) / dt
                + spreads_[i-1]->value() * (times_[i] - t) / dt;
        }

        return spread;
    }

    inline void PiecewiseZeroSpreadedTermStructure::update() {
        updateTimes();
        ZeroYieldStructure::update();
    }

    inline void PiecewiseZeroSpreadedTermStructure::updateTimes() {
        for (Size i = 0; i < dates_.size(); i++)
            times_[i] = timeFromReference(dates_[i]);
    }

}


#endif

50 bps Key Rate Shock (before):


50 bps Key Rate Shock (after fix):

-- 
Robert Philipp
Synapse Financial Engineering
703.623.4063 (mobile)
703.573.0119 (fax)

[hidden email]
www.synapsefe.com

------------------------------------------------------------------------------
Sell apps to millions through the Intel(R) Atom(Tm) Developer Program
Be part of this innovative community and reach millions of netbook users
worldwide. Take advantage of special opportunities to increase revenue and
speed time-to-market. Join now, and jumpstart your future.
http://p.sf.net/sfu/intel-atom-d2d
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev

piecewisezerospreadedtermstructure.hpp (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Shocking the Zero Curve

Luigi Ballabio
On Mon, 2010-08-23 at 13:00 -0400, Robert Philipp wrote:
> The PiecewiseZeroSpreadedTermStructure class does not shock correctly
> when the compounding is different from continuous.

Yes, the curve assumed that the spread was applied to the continuous
rates. Thanks for the generalization; it will make it into next release.

> I believe that the way forward rates are shocked is incorrect. In the
> code, the spread (shock) is simply added to the forward curve of the
> base. The forward curve should be based on the shocked zero curve
> instead. Has anyone fixed that? If not, I'll try to fix that as well.

You're talking of the ForwardSpreadedTermStructure class now, right? I'm
not sure what the behavior should be in this case. The name of the class
seems to imply that we want to add a spread to the forwards, though.

> Not sure how best to contribute the code. Is there a staging area for
> checking in the code?

Posting here is ok.  Next time, you might consider providing a patch
instead of the modified file (i.e., the output of running the diff
program between the original file and your file.)  But it's no big deal.

Just one question on your code: you added a declaration for
  Rate forwardImpl(Time) const;
to the header, but no definition afterwards. Is there a method body you
didn't post, or should I just remove the declaration?

Thanks,
        Luigi

P.S. Make that two questions: to whom should I assign the copyright of
the modified code? (Robert Philipp, Synapsefe, both?)



--

Green's Law of Debate:
Anything is possible if you don't know what you're talking about.



------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev