1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2003 Ferdinando Ametrano
5 
6  This file is part of QuantLib, a free-software/open-source library
7  for financial quantitative analysts and developers - http://quantlib.org/
8 
9  QuantLib is free software: you can redistribute it and/or modify it
10  under the terms of the QuantLib license.  You should have received a
11  copy of the license along with this program; if not, please email
12  <quantlib-dev@lists.sf.net>. The license is also available online at
13  <http://quantlib.org/license.shtml>.
14 
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the license for more details.
18 */
19 
20 /*! \file impliedvoltermstructure.hpp
21     \brief Implied Black Vol Term Structure
22 */
23 
24 #ifndef quantlib_implied_vol_term_structure_hpp
25 #define quantlib_implied_vol_term_structure_hpp
26 
27 #include <ql/termstructures/volatility/equityfx/blackvoltermstructure.hpp>
28 
29 namespace QuantLib {
30 
31     //! Implied vol term structure at a given date in the future
32     /*! The given date will be the implied reference date.
33         \note This term structure will remain linked to the original
34               structure, i.e., any changes in the latter will be reflected
35               in this structure as well.
36 
37         \warning It doesn't make financial sense to have an
38                  asset-dependant implied Vol Term Structure.  This
39                  class should be used with term structures that are
40                  time dependant only.
41     */
42     class ImpliedVolTermStructure : public BlackVarianceTermStructure {
43       public:
44         ImpliedVolTermStructure(const Handle<BlackVolTermStructure>& origTS,
45                                 const Date& referenceDate);
46         //! \name TermStructure interface
47         //@{
dayCounter() const48         DayCounter dayCounter() const { return originalTS_->dayCounter(); }
49         Date maxDate() const;
50         //@}
51         //! \name VolatilityTermStructure interface
52         //@{
53         Real minStrike() const;
54         Real maxStrike() const;
55         //@}
56         //! \name Visitability
57         //@{
58         virtual void accept(AcyclicVisitor&);
59         //@}
60       protected:
61         virtual Real blackVarianceImpl(Time t, Real strike) const;
62       private:
63         Handle<BlackVolTermStructure> originalTS_;
64     };
65 
66 
67     // inline definitions
68 
ImpliedVolTermStructure(const Handle<BlackVolTermStructure> & originalTS,const Date & referenceDate)69     inline ImpliedVolTermStructure::ImpliedVolTermStructure(
70                               const Handle<BlackVolTermStructure>& originalTS,
71                               const Date& referenceDate)
72     : BlackVarianceTermStructure(referenceDate), originalTS_(originalTS) {
73         registerWith(originalTS_);
74     }
75 
maxDate() const76     inline Date ImpliedVolTermStructure::maxDate() const {
77         return originalTS_->maxDate();
78     }
79 
minStrike() const80     inline Real ImpliedVolTermStructure::minStrike() const {
81         return originalTS_->minStrike();
82     }
83 
maxStrike() const84     inline Real ImpliedVolTermStructure::maxStrike() const {
85         return originalTS_->maxStrike();
86     }
87 
accept(AcyclicVisitor & v)88     inline void ImpliedVolTermStructure::accept(AcyclicVisitor& v) {
89         Visitor<ImpliedVolTermStructure>* v1 =
90             dynamic_cast<Visitor<ImpliedVolTermStructure>*>(&v);
91         if (v1 != 0)
92             v1->visit(*this);
93         else
94             BlackVarianceTermStructure::accept(v);
95     }
96 
blackVarianceImpl(Time t,Real strike) const97     inline Real ImpliedVolTermStructure::blackVarianceImpl(Time t,
98                                                            Real strike) const {
99         /* timeShift (and/or variance) variance at evaluation date
100            cannot be cached since the original curve could change
101            between invocations of this method */
102         Time timeShift =
103             dayCounter().yearFraction(originalTS_->referenceDate(),
104                                       referenceDate());
105         /* t is relative to the current reference date
106            and needs to be converted to the time relative
107            to the reference date of the original curve */
108         return originalTS_->blackForwardVariance(timeShift,
109                                                  timeShift+t,
110                                                  strike,
111                                                  true);
112     }
113 
114 }
115 
116 #endif
117