File: smilesection.hpp

package info (click to toggle)
quantlib 0.9.0.20071224-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 22,216 kB
  • ctags: 34,951
  • sloc: cpp: 167,744; ansic: 21,483; sh: 8,947; makefile: 3,327; lisp: 86
file content (164 lines) | stat: -rw-r--r-- 6,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2006 Mario Pucci

 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
 <quantlib-dev@lists.sf.net>. 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 smilesection.hpp
    \brief Smile section base class
*/

#ifndef quantlib_smile_section_hpp
#define quantlib_smile_section_hpp

#include <ql/patterns/observable.hpp>
#include <ql/time/date.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/utilities/null.hpp>
#include <ql/quote.hpp>
#include <vector>

namespace QuantLib {

    class SpreadedSmileSection;

    //! interest rate volatility smile section
    /*! This abstract class provides volatility smile section interface */
    class SmileSection : public virtual Observable, public Observer {
      public:
        SmileSection(const Date& d,
                     const DayCounter& dc = DayCounter(),
                     const Date& referenceDate = Date());
        SmileSection(Time exerciseTime,
                     const DayCounter& dc = DayCounter());
        SmileSection() {}
        virtual void update();
        virtual ~SmileSection() {}

        virtual Real minStrike() const = 0;
        virtual Real maxStrike() const = 0;
        Real variance(Rate strike = Null<Rate>()) const;
        Volatility volatility(Rate strike = Null<Rate>()) const;
        virtual Real atmLevel() const = 0;
        const Date& exerciseDate() const { return exerciseDate_; }
        Time exerciseTime() const { return exerciseTime_; }
        const DayCounter& dayCounter() const { return dc_; }
        void initializeExerciseTime() const;
        friend class SpreadedSmileSection;
      protected:
        virtual Real varianceImpl(Rate strike) const = 0;
        virtual Volatility volatilityImpl(Rate strike) const = 0;
      private:
        bool isFloating_;
        mutable Date referenceDate_;
        Date exerciseDate_;
        DayCounter dc_;
        mutable Time exerciseTime_;
    };

    inline Real SmileSection::variance(Rate strike) const {
        if (strike==Null<Rate>())
            strike = atmLevel();
        return varianceImpl(strike);
    }

    inline Volatility SmileSection::volatility(Rate strike) const {
        if (strike==Null<Rate>())
            strike = atmLevel();
        return volatilityImpl(strike);
    }

    class FlatSmileSection : public SmileSection {
      public:
        FlatSmileSection(const Date& d,
                         Volatility vol,
                         const DayCounter& dc,
                         const Date& referenceDate = Date(),
                         Real atmLevel = Null<Rate>())
        : SmileSection(d, dc, referenceDate),
          vol_(vol),
          atmLevel_(atmLevel){}

        FlatSmileSection(Time exerciseTime,
                         Volatility vol,
                         const DayCounter& dc = Actual365Fixed())
        : SmileSection(exerciseTime, dc), vol_(vol) {}

        Real minStrike () const { return 0.0; }
        Real maxStrike () const { return QL_MAX_REAL; }
        Real atmLevel() const { return atmLevel_; }
      protected:
        Real varianceImpl(Rate) const { return vol_*vol_* exerciseTime(); }
        Volatility volatilityImpl(Rate) const { return vol_; }
      private:
        Volatility vol_;
        Real atmLevel_;
    };

    class SabrSmileSection : public SmileSection {
      public:
        SabrSmileSection(Time timeToExpiry,
                         Rate forward,
                         const std::vector<Real>& sabrParameters);
        SabrSmileSection(const Date& d,
                         Rate forward,
                         const std::vector<Real>& sabrParameters,
                         const DayCounter& dc = Actual365Fixed());
        Real minStrike () const { return 0.0; }
        Real maxStrike () const { return QL_MAX_REAL; }
        Real atmLevel() const { return forward_; }
      protected:
        Real varianceImpl(Rate strike) const;
        Volatility volatilityImpl(Rate strike) const;
      private:
        Real alpha_, beta_, nu_, rho_, forward_;
    };

    class SpreadedSmileSection : public SmileSection {
      public:
        SpreadedSmileSection(const boost::shared_ptr<SmileSection>& underlyingSection,
                             const Handle<Quote>& spread)
        : underlyingSection_(underlyingSection), spread_(spread) {
            registerWith(underlyingSection_);
            registerWith(spread_);
        }

        void update(){ notifyObservers(); }

        Real minStrike() const { return underlyingSection_->minStrike(); }
        Real maxStrike() const { return underlyingSection_->maxStrike(); }

        const Date& exerciseDate() const { return underlyingSection_->exerciseDate(); }
        Time exerciseTime() const { return underlyingSection_->exerciseTime(); }
        const DayCounter& dayCounter() const { return underlyingSection_->dayCounter(); }

        Real atmLevel() const { return underlyingSection_->atmLevel(); }
      protected:
        Real varianceImpl(Rate strike) const {
            Volatility vol = volatilityImpl(strike);
            return vol*vol*exerciseTime();
        }
        Volatility volatilityImpl(Rate strike) const {
            return underlyingSection_->volatilityImpl(strike)+spread_->value();
        }
      private:
        const boost::shared_ptr<SmileSection> underlyingSection_;
        const Handle<Quote> spread_;
    };

}

#endif