Coverage Report

Created: 2026-02-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/termstructures/volatility/optionlet/optionletvolatilitystructure.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2002, 2003 RiskMap srl
5
 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
6
 Copyright (C) 2015 Peter Caspers
7
 Copyright (C) 2015 Michael von den Driesch
8
9
 This file is part of QuantLib, a free-software/open-source library
10
 for financial quantitative analysts and developers - http://quantlib.org/
11
12
 QuantLib is free software: you can redistribute it and/or modify it
13
 under the terms of the QuantLib license.  You should have received a
14
 copy of the license along with this program; if not, please email
15
 <quantlib-dev@lists.sf.net>. The license is also available online at
16
 <https://www.quantlib.org/license.shtml>.
17
18
 This program is distributed in the hope that it will be useful, but WITHOUT
19
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20
 FOR A PARTICULAR PURPOSE.  See the license for more details.
21
*/
22
23
/*! \file optionletvolatilitystructure.hpp
24
    \brief optionlet (caplet/floorlet) volatility structure
25
*/
26
27
#ifndef quantlib_optionlet_volatility_structure_hpp
28
#define quantlib_optionlet_volatility_structure_hpp
29
30
#include <ql/termstructures/voltermstructure.hpp>
31
#include <ql/termstructures/volatility/optionlet/optionletstripper.hpp>
32
#include <ql/termstructures/volatility/volatilitytype.hpp>
33
34
namespace QuantLib {
35
36
    class SmileSection;
37
38
    //! Optionlet (caplet/floorlet) volatility structure
39
    /*! This class is purely abstract and defines the interface of
40
        concrete structures which will be derived from this one.
41
    */
42
    class OptionletVolatilityStructure : public VolatilityTermStructure {
43
      public:
44
        /*! \name Constructors
45
            See the TermStructure documentation for issues regarding
46
            constructors.
47
        */
48
        //@{
49
        //! default constructor
50
        /*! \warning term structures initialized by means of this
51
                     constructor must manage their own reference date
52
                     by overriding the referenceDate() method.
53
        */
54
        OptionletVolatilityStructure(BusinessDayConvention bdc = Following,
55
                                     const DayCounter& dc = DayCounter());
56
        //! initialize with a fixed reference date
57
        OptionletVolatilityStructure(const Date& referenceDate,
58
                                     const Calendar& cal,
59
                                     BusinessDayConvention bdc,
60
                                     const DayCounter& dc = DayCounter());
61
        //! calculate the reference date based on the global evaluation date
62
        OptionletVolatilityStructure(Natural settlementDays,
63
                                     const Calendar&,
64
                                     BusinessDayConvention bdc,
65
                                     const DayCounter& dc = DayCounter());
66
        //@}
67
0
        ~OptionletVolatilityStructure() override = default;
68
        //! \name Volatility and Variance
69
        //@{
70
        //! returns the volatility for a given option tenor and strike rate
71
        Volatility volatility(const Period& optionTenor,
72
                              Rate strike,
73
                              bool extrapolate = false) const;
74
        //! returns the volatility for a given option date and strike rate
75
        Volatility volatility(const Date& optionDate,
76
                              Rate strike,
77
                              bool extrapolate = false) const;
78
        //! returns the volatility for a given option time and strike rate
79
        Volatility volatility(Time optionTime,
80
                              Rate strike,
81
                              bool extrapolate = false) const;
82
83
        //! returns the Black variance for a given option tenor and strike rate
84
        Real blackVariance(const Period& optionTenor,
85
                           Rate strike,
86
                           bool extrapolate = false) const;
87
        //! returns the Black variance for a given option date and strike rate
88
        Real blackVariance(const Date& optionDate,
89
                           Rate strike,
90
                           bool extrapolate = false) const;
91
        //! returns the Black variance for a given option time and strike rate
92
        Real blackVariance(Time optionTime,
93
                           Rate strike,
94
                           bool extrapolate = false) const;
95
96
        //! returns the smile for a given option tenor
97
        ext::shared_ptr<SmileSection> smileSection(const Period& optionTenor,
98
                                                     bool extr = false) const;
99
        //! returns the smile for a given option date
100
        ext::shared_ptr<SmileSection> smileSection(const Date& optionDate,
101
                                                     bool extr = false) const;
102
        //! returns the smile for a given option time
103
        ext::shared_ptr<SmileSection> smileSection(Time optionTime,
104
                                                     bool extr = false) const;
105
        //@}
106
        virtual VolatilityType volatilityType() const;
107
        virtual Real displacement() const;
108
109
      protected:
110
        virtual ext::shared_ptr<SmileSection> smileSectionImpl(
111
                                                const Date& optionDate) const;
112
        //! implements the actual smile calculation in derived classes
113
        virtual ext::shared_ptr<SmileSection> smileSectionImpl(
114
                                                    Time optionTime) const = 0;
115
        virtual Volatility volatilityImpl(const Date& optionDate,
116
                                          Rate strike) const;
117
        //! implements the actual volatility calculation in derived classes
118
        virtual Volatility volatilityImpl(Time optionTime,
119
                                          Rate strike) const = 0;
120
    };
121
122
    // inline definitions
123
124
    // 1. Period-based methods convert Period to Date and then
125
    //    use the equivalent Date-based methods
126
    inline Volatility
127
    OptionletVolatilityStructure::volatility(const Period& optionTenor,
128
                                             Rate strike,
129
0
                                             bool extrapolate) const {
130
0
        Date optionDate = optionDateFromTenor(optionTenor);
131
0
        return volatility(optionDate, strike, extrapolate);
132
0
    }
133
134
    inline
135
    Real OptionletVolatilityStructure::blackVariance(const Period& optionTenor,
136
                                                     Rate strike,
137
0
                                                     bool extrapolate) const {
138
0
        Date optionDate = optionDateFromTenor(optionTenor);
139
0
        return blackVariance(optionDate, strike, extrapolate);
140
0
    }
141
142
    inline ext::shared_ptr<SmileSection>
143
    OptionletVolatilityStructure::smileSection(const Period& optionTenor,
144
0
                                               bool extrapolate) const {
145
0
        Date optionDate = optionDateFromTenor(optionTenor);
146
0
        return smileSection(optionDate, extrapolate);
147
0
    }
148
149
    // 2. blackVariance methods rely on volatility methods
150
    inline
151
    Real OptionletVolatilityStructure::blackVariance(const Date& optionDate,
152
                                                     Rate strike,
153
0
                                                     bool extrapolate) const {
154
0
        Volatility v = volatility(optionDate, strike, extrapolate);
155
0
        Time t = timeFromReference(optionDate);
156
0
        return v*v*t;
157
0
    }
158
159
    inline
160
    Real OptionletVolatilityStructure::blackVariance(Time optionTime,
161
                                                     Rate strike,
162
0
                                                     bool extrapolate) const {
163
0
        Volatility v = volatility(optionTime, strike, extrapolate);
164
0
        return v*v*optionTime;
165
0
    }
166
167
    // 3. relying on xxxImpl methods
168
    inline Volatility
169
    OptionletVolatilityStructure::volatility(const Date& optionDate,
170
                                             Rate strike,
171
0
                                             bool extrapolate) const {
172
0
        checkRange(optionDate, extrapolate);
173
0
        checkStrike(strike, extrapolate);
174
0
        return volatilityImpl(optionDate, strike);
175
0
    }
176
177
    inline Volatility
178
    OptionletVolatilityStructure::volatility(Time optionTime,
179
                                             Rate strike,
180
0
                                             bool extrapolate) const {
181
0
        checkRange(optionTime, extrapolate);
182
0
        checkStrike(strike, extrapolate);
183
0
        return volatilityImpl(optionTime, strike);
184
0
    }
185
186
    inline ext::shared_ptr<SmileSection>
187
    OptionletVolatilityStructure::smileSection(const Date& optionDate,
188
0
                                               bool extrapolate) const {
189
0
        checkRange(optionDate, extrapolate);
190
0
        return smileSectionImpl(optionDate);
191
0
    }
192
193
    inline ext::shared_ptr<SmileSection>
194
    OptionletVolatilityStructure::smileSection(Time optionTime,
195
0
                                               bool extrapolate) const {
196
0
        checkRange(optionTime, extrapolate);
197
0
        return smileSectionImpl(optionTime);
198
0
    }
199
200
    // 4. default implementation of Date-based xxxImpl methods
201
    //    relying on the equivalent Time-based methods
202
    inline ext::shared_ptr<SmileSection>
203
0
    OptionletVolatilityStructure::smileSectionImpl(const Date& optionDate) const {
204
0
        return smileSectionImpl(timeFromReference(optionDate));
205
0
    }
206
207
    inline Volatility
208
    OptionletVolatilityStructure::volatilityImpl(const Date& optionDate,
209
0
                                                 Rate strike) const {
210
0
        return volatilityImpl(timeFromReference(optionDate), strike);
211
0
    }
212
213
    inline VolatilityType
214
0
    OptionletVolatilityStructure::volatilityType() const {
215
0
        return ShiftedLognormal;
216
0
    }
217
218
0
    inline Real OptionletVolatilityStructure::displacement() const {
219
0
        return 0.0;
220
0
    }
221
}
222
223
#endif