Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/experimental/callablebonds/callablebondvolstructure.hpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Allen Kuo
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 callablebondvolstructure.hpp
21
    \brief Callable-bond volatility structure
22
*/
23
24
#ifndef quantlib_callable_bond_volatility_structure_hpp
25
#define quantlib_callable_bond_volatility_structure_hpp
26
27
#include <ql/termstructure.hpp>
28
#include <ql/math/interpolations/linearinterpolation.hpp>
29
#include <ql/termstructures/volatility/smilesection.hpp>
30
31
namespace QuantLib {
32
33
    //! Callable-bond volatility structure
34
    /*! This class is purely abstract and defines the interface of
35
        concrete callable-bond volatility structures which will be
36
        derived from this one.
37
    */
38
    class CallableBondVolatilityStructure : public TermStructure {
39
      public:
40
        /*! \name Constructors
41
            See the TermStructure documentation for issues regarding
42
            constructors.
43
        */
44
        //@{
45
        //! default constructor
46
        /*! \warning term structures initialized by means of this
47
                     constructor must manage their own reference date
48
                     by overriding the referenceDate() method.
49
        */
50
        CallableBondVolatilityStructure(const DayCounter& dc = DayCounter(),
51
                                        BusinessDayConvention bdc = Following);
52
        //! initialize with a fixed reference date
53
        CallableBondVolatilityStructure(const Date& referenceDate,
54
                                        const Calendar& calendar = Calendar(),
55
                                        const DayCounter& dc = DayCounter(),
56
                                        BusinessDayConvention bdc = Following);
57
        //! calculate the reference date based on the global evaluation date
58
        CallableBondVolatilityStructure(Natural settlementDays,
59
                                        const Calendar&,
60
                                        const DayCounter& dc = DayCounter(),
61
                                        BusinessDayConvention bdc = Following);
62
        //@}
63
0
        ~CallableBondVolatilityStructure() override = default;
64
        //! \name Volatility, variance and smile
65
        //@{
66
        //! returns the volatility for a given option time and bondLength
67
        Volatility volatility(Time optionTime,
68
                              Time bondLength,
69
                              Rate strike,
70
                              bool extrapolate = false) const;
71
        //! returns the Black variance for a given option time and bondLength
72
        Real blackVariance(Time optionTime,
73
                           Time bondLength,
74
                           Rate strike,
75
                           bool extrapolate = false) const;
76
77
        //! returns the volatility for a given option date and bond tenor
78
        Volatility volatility(const Date& optionDate,
79
                              const Period& bondTenor,
80
                              Rate strike,
81
                              bool extrapolate = false) const;
82
        //! returns the Black variance for a given option date and bond tenor
83
        Real blackVariance(const Date& optionDate,
84
                           const Period& bondTenor,
85
                           Rate strike,
86
                           bool extrapolate = false) const;
87
        virtual ext::shared_ptr<SmileSection> smileSection(
88
                                              const Date& optionDate,
89
0
                                              const Period& bondTenor) const {
90
0
            const std::pair<Time, Time> p = convertDates(optionDate, bondTenor);
91
0
            return smileSectionImpl(p.first, p.second);
92
0
        }
93
94
        //! returns the volatility for a given option tenor and bond tenor
95
        Volatility volatility(const Period& optionTenor,
96
                              const Period& bondTenor,
97
                              Rate strike,
98
                              bool extrapolate = false) const;
99
        //! returns the Black variance for a given option tenor and bond tenor
100
        Real blackVariance(const Period& optionTenor,
101
                           const Period& bondTenor,
102
                           Rate strike,
103
                           bool extrapolate = false) const;
104
        ext::shared_ptr<SmileSection> smileSection(
105
                                               const Period& optionTenor,
106
                                               const Period& bondTenor) const;
107
        //@}
108
        //! \name Limits
109
        //@{
110
        //! the largest length for which the term structure can return vols
111
        virtual const Period& maxBondTenor() const = 0;
112
        //! the largest bondLength for which the term structure can return vols
113
        virtual Time maxBondLength() const;
114
        //! the minimum strike for which the term structure can return vols
115
        virtual Rate minStrike() const = 0;
116
        //! the maximum strike for which the term structure can return vols
117
        virtual Rate maxStrike() const = 0;
118
        //@}
119
        //! implements the conversion between dates and times
120
        virtual std::pair<Time,Time> convertDates(
121
                                               const Date& optionDate,
122
                                               const Period& bondTenor) const;
123
        //! the business day convention used for option date calculation
124
        virtual BusinessDayConvention businessDayConvention() const;
125
        //! implements the conversion between optionTenors and optionDates
126
        Date optionDateFromTenor(const Period& optionTenor) const;
127
    protected:
128
129
        //! return smile section
130
        virtual ext::shared_ptr<SmileSection> smileSectionImpl(
131
                                                   Time optionTime,
132
                                                   Time bondLength) const = 0;
133
134
        //! implements the actual volatility calculation in derived classes
135
        virtual Volatility volatilityImpl(Time optionTime,
136
                                          Time bondLength,
137
                                          Rate strike) const = 0;
138
        virtual Volatility volatilityImpl(const Date& optionDate,
139
                                          const Period& bondTenor,
140
0
                                          Rate strike) const {
141
0
            const std::pair<Time, Time> p = convertDates(optionDate, bondTenor);
142
0
            return volatilityImpl(p.first, p.second, strike);
143
0
        }
144
        void checkRange(Time, Time, Rate strike, bool extrapolate) const;
145
        void checkRange(const Date& optionDate,
146
                        const Period& bondTenor,
147
                        Rate strike, bool extrapolate) const;
148
      private:
149
        BusinessDayConvention bdc_;
150
    };
151
152
153
    // inline definitions
154
155
    inline BusinessDayConvention
156
0
    CallableBondVolatilityStructure::businessDayConvention() const {
157
0
        return bdc_;
158
0
    }
159
160
    inline Date CallableBondVolatilityStructure::optionDateFromTenor(
161
0
                                            const Period& optionTenor) const {
162
0
        return calendar().advance(referenceDate(),
163
0
                                  optionTenor,
164
0
                                  businessDayConvention());
165
0
    }
166
167
    inline Volatility CallableBondVolatilityStructure::volatility(
168
                                                     Time optionTime,
169
                                                     Time bondLength,
170
                                                     Rate strike,
171
0
                                                     bool extrapolate) const {
172
0
        checkRange(optionTime, bondLength, strike, extrapolate);
173
0
        return volatilityImpl(optionTime, bondLength, strike);
174
0
    }
175
176
177
    inline Real CallableBondVolatilityStructure::blackVariance(
178
                                                     Time optionTime,
179
                                                     Time bondLength,
180
                                                     Rate strike,
181
0
                                                     bool extrapolate) const {
182
0
        checkRange(optionTime, bondLength, strike, extrapolate);
183
0
        Volatility vol = volatilityImpl(optionTime, bondLength, strike);
184
0
        return vol*vol*optionTime;
185
0
    }
186
187
188
    inline Volatility CallableBondVolatilityStructure::volatility(
189
                                                     const Date& optionDate,
190
                                                     const Period& bondTenor,
191
                                                     Rate strike,
192
0
                                                     bool extrapolate) const {
193
0
        checkRange(optionDate, bondTenor, strike, extrapolate);
194
0
        return volatilityImpl(optionDate, bondTenor, strike);
195
0
    }
196
197
    inline Real CallableBondVolatilityStructure::blackVariance(
198
                                                     const Date& optionDate,
199
                                                     const Period& bondTenor,
200
                                                     Rate strike,
201
0
                                                     bool extrapolate) const {
202
0
        Volatility vol =
203
0
            volatility(optionDate, bondTenor, strike, extrapolate);
204
0
        const std::pair<Time, Time> p = convertDates(optionDate, bondTenor);
205
0
        return vol*vol*p.first;
206
0
    }
207
208
    inline Volatility CallableBondVolatilityStructure::volatility(
209
                                                    const Period& optionTenor,
210
                                                    const Period& bondTenor,
211
                                                    Rate strike,
212
0
                                                    bool extrapolate) const {
213
0
        Date optionDate = optionDateFromTenor(optionTenor);
214
0
        return volatility(optionDate, bondTenor, strike, extrapolate);
215
0
    }
216
217
    inline Real CallableBondVolatilityStructure::blackVariance(
218
                                                    const Period& optionTenor,
219
                                                    const Period& bondTenor,
220
                                                    Rate strike,
221
0
                                                    bool extrapolate) const {
222
0
        Date optionDate = optionDateFromTenor(optionTenor);
223
0
        Volatility vol =
224
0
            volatility(optionDate, bondTenor, strike, extrapolate);
225
0
        const std::pair<Time, Time> p = convertDates(optionDate, bondTenor);
226
0
        return vol*vol*p.first;
227
0
    }
228
229
230
    inline ext::shared_ptr<SmileSection>
231
    CallableBondVolatilityStructure::smileSection(
232
                                              const Period& optionTenor,
233
0
                                              const Period& bondTenor) const {
234
0
        Date optionDate = optionDateFromTenor(optionTenor);
235
0
        return smileSection(optionDate, bondTenor);
236
0
    }
237
238
239
    inline void CallableBondVolatilityStructure::checkRange(
240
0
        Time optionTime, Time bondLength, Rate k, bool extrapolate) const {
241
0
        TermStructure::checkRange(optionTime, extrapolate);
242
0
        QL_REQUIRE(bondLength >= 0.0,
243
0
                   "negative bondLength (" << bondLength << ") given");
244
0
        QL_REQUIRE(extrapolate || allowsExtrapolation() ||
245
0
                   bondLength <= maxBondLength(),
246
0
                   "bondLength (" << bondLength << ") is past max curve bondLength ("
247
0
                   << maxBondLength() << ")");
248
0
        QL_REQUIRE(extrapolate || allowsExtrapolation() ||
249
0
                   (k >= minStrike() && k <= maxStrike()),
250
0
                   "strike (" << k << ") is outside the curve domain ["
251
0
                   << minStrike() << "," << maxStrike()<< "]");
252
0
    }
253
254
}
255
256
#endif