/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2006 Ferdinando Ametrano Copyright (C) 2006 François du Vignaud Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb Copyright (C) 2006, 2007 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 . The license is also available online at . 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 capfloor.hpp \brief cap and floor class */ #ifndef quantlib_instruments_capfloor_hpp #define quantlib_instruments_capfloor_hpp #include #include #include #include #include #include namespace QuantLib { class YieldTermStructure; //! Base class for cap-like instruments /*! \ingroup instruments \test - the correctness of the returned value is tested by checking that the price of a cap (resp. floor) decreases (resp. increases) with the strike rate. - the relationship between the values of caps, floors and the resulting collars is checked. - the put-call parity between the values of caps, floors and swaps is checked. - the correctness of the returned implied volatility is tested by using it for reproducing the target value. - the correctness of the returned value is tested by checking it against a known good value. */ class CapFloor : public Instrument { public: enum Type { Cap, Floor, Collar }; class arguments; class engine; CapFloor(Type type, const Leg& floatingLeg, const std::vector& capRates, const std::vector& floorRates, const Handle& termStructure, const boost::shared_ptr& engine); CapFloor(Type type, const Leg& floatingLeg, const std::vector& strikes, const Handle& termStructure, const boost::shared_ptr& engine); //! \name Instrument interface //@{ bool isExpired() const; void setupArguments(PricingEngine::arguments*) const; //@} //! \name Inspectors //@{ Type type() const { return type_; } const Leg& leg() const { return floatingLeg_; } const std::vector& capRates() const { return capRates_; } const std::vector& floorRates() const { return floorRates_; } const Leg& floatingLeg() const { return floatingLeg_; } Rate atmRate() const; Date startDate() const; Date maturityDate() const; Date lastFixingDate() const; //@} //! implied term volatility Volatility impliedVolatility(Real price, Real accuracy = 1.0e-4, Size maxEvaluations = 100, Volatility minVol = 1.0e-7, Volatility maxVol = 4.0) const; private: Type type_; Leg floatingLeg_; std::vector capRates_; std::vector floorRates_; Handle termStructure_; // helper class for implied volatility calculation class ImpliedVolHelper { public: ImpliedVolHelper(const CapFloor&, const Handle&, Real targetValue); Real operator()(Volatility x) const; Real derivative(Volatility x) const; private: boost::shared_ptr engine_; Handle termStructure_; Real targetValue_; boost::shared_ptr vol_; const Instrument::results* results_; }; }; //! Concrete cap class /*! \ingroup instruments */ class Cap : public CapFloor { public: Cap(const Leg& floatingLeg, const std::vector& exerciseRates, const Handle& termStructure, const boost::shared_ptr& engine) : CapFloor(CapFloor::Cap, floatingLeg, exerciseRates, std::vector(), termStructure, engine) {} }; //! Concrete floor class /*! \ingroup instruments */ class Floor : public CapFloor { public: Floor(const Leg& floatingLeg, const std::vector& exerciseRates, const Handle& termStructure, const boost::shared_ptr& engine) : CapFloor(CapFloor::Floor, floatingLeg, std::vector(), exerciseRates, termStructure, engine) {} }; //! Concrete collar class /*! \ingroup instruments */ class Collar : public CapFloor { public: Collar(const Leg& floatingLeg, const std::vector& capRates, const std::vector& floorRates, const Handle& termStructure, const boost::shared_ptr& engine) : CapFloor(CapFloor::Collar, floatingLeg, capRates, floorRates, termStructure, engine) {} }; //! %Arguments for cap/floor calculation class CapFloor::arguments : public virtual PricingEngine::arguments { public: arguments() : type(CapFloor::Type(-1)) {} CapFloor::Type type; std::vector