Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/termstructures/volatility/inflation/yoyinflationoptionletvolatilitystructure.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2009 Chris Kenyon
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
 <https://www.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 yoyoptionletvolatilitystructures.cpp
21
 \brief yoy inflation volatility structures
22
 */
23
24
#include <ql/quotes/simplequote.hpp>
25
#include <ql/termstructures/inflationtermstructure.hpp>
26
#include <ql/termstructures/volatility/inflation/yoyinflationoptionletvolatilitystructure.hpp>
27
#include <utility>
28
29
namespace QuantLib {
30
31
    YoYOptionletVolatilitySurface::
32
    YoYOptionletVolatilitySurface(Natural settlementDays,
33
                                  const Calendar &cal,
34
                                  BusinessDayConvention bdc,
35
                                  const DayCounter& dc,
36
                                  const Period& observationLag,
37
                                  Frequency frequency,
38
                                  bool indexIsInterpolated,
39
                                  VolatilityType volType,
40
                                  Real displacement)
41
0
    : VolatilityTermStructure(settlementDays, cal, bdc, dc),
42
0
      baseLevel_(Null<Volatility>()), observationLag_(observationLag),
43
0
      frequency_(frequency), indexIsInterpolated_(indexIsInterpolated),
44
0
      volType_(volType), displacement_(displacement) {
45
0
        QL_REQUIRE(close_enough(displacement, 0.0) || close_enough(displacement, 1.0),
46
0
                   "YoYOptionletVolatilitySurface: displacement (" << displacement
47
0
                                                                   << ") must be 0 or 1");
48
0
    }
49
50
51
    Date
52
0
    YoYOptionletVolatilitySurface::baseDate() const {
53
54
        // Depends on interpolation, or not, of observed index
55
        // and observation lag with which it was built.
56
        // We want this to work even if the index does not
57
        // have a yoy term structure.
58
0
        if (indexIsInterpolated()) {
59
0
            return referenceDate() - observationLag();
60
0
        } else {
61
0
            return inflationPeriod(referenceDate() - observationLag(),
62
0
                                   frequency()).first;
63
0
        }
64
0
    }
65
66
67
    void YoYOptionletVolatilitySurface::checkRange(const Date& d, Rate strike,
68
0
                                            bool extrapolate) const {
69
0
        QL_REQUIRE(d >= baseDate(),
70
0
                   "date (" << d << ") is before base date");
71
0
        QL_REQUIRE(extrapolate || allowsExtrapolation() || d <= maxDate(),
72
0
                   "date (" << d << ") is past max curve date ("
73
0
                   << maxDate() << ")");
74
0
        QL_REQUIRE(extrapolate || allowsExtrapolation() ||
75
0
                   (strike >= minStrike() && strike <= maxStrike()),
76
0
                   "strike (" << strike << ") is outside the curve domain ["
77
0
                   << minStrike() << "," << maxStrike()<< "]] at date = " << d);
78
0
    }
79
80
81
    void YoYOptionletVolatilitySurface::checkRange(Time t, Rate strike,
82
0
                                            bool extrapolate) const {
83
0
        QL_REQUIRE(t >= timeFromReference(baseDate()),
84
0
                   "time (" << t << ") is before base date");
85
0
        QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
86
0
                   "time (" << t << ") is past max curve time ("
87
0
                   << maxTime() << ")");
88
0
        QL_REQUIRE(extrapolate || allowsExtrapolation() ||
89
0
                   (strike >= minStrike() && strike <= maxStrike()),
90
0
                   "strike (" << strike << ") is outside the curve domain ["
91
0
                   << minStrike() << "," << maxStrike()<< "] at time = " << t);
92
0
    }
93
94
95
    Volatility
96
    YoYOptionletVolatilitySurface::volatility(const Date& maturityDate,
97
                                              Rate strike,
98
                                              const Period &obsLag,
99
0
                                              bool extrapolate) const {
100
101
0
        Period useLag = obsLag;
102
0
        if (obsLag==Period(-1,Days)) {
103
0
            useLag = observationLag();
104
0
        }
105
106
0
        if (indexIsInterpolated()) {
107
0
            YoYOptionletVolatilitySurface::checkRange(maturityDate-useLag, strike, extrapolate);
108
0
            Time t = timeFromReference(maturityDate-useLag);
109
0
            return volatilityImpl(t,strike);
110
0
        } else {
111
0
            std::pair<Date,Date> dd = inflationPeriod(maturityDate-useLag, frequency());
112
0
            YoYOptionletVolatilitySurface::checkRange(dd.first, strike, extrapolate);
113
0
            Time t = timeFromReference(dd.first);
114
0
            return volatilityImpl(t,strike);
115
0
        }
116
0
    }
117
118
119
    Volatility
120
    YoYOptionletVolatilitySurface::volatility(const Period& optionTenor,
121
                                              Rate strike,
122
                                              const Period &obsLag,
123
0
                                              bool extrapolate) const {
124
0
        Date maturityDate = optionDateFromTenor(optionTenor);
125
0
        return volatility(maturityDate, strike, obsLag, extrapolate);
126
0
    }
127
128
0
    Volatility YoYOptionletVolatilitySurface::volatility(Time time, Rate strike) const {
129
0
        return volatilityImpl(time, strike);
130
0
    }
131
132
    //! needed for total variance calculations
133
    Time
134
    YoYOptionletVolatilitySurface::timeFromBase(const Date &maturityDate,
135
0
                                                const Period& obsLag) const {
136
137
0
        Period useLag = obsLag;
138
0
        if (obsLag==Period(-1,Days)) {
139
0
            useLag = observationLag();
140
0
        }
141
142
0
        Date useDate;
143
0
        if (indexIsInterpolated()) {
144
0
            useDate = maturityDate - useLag;
145
0
        } else {
146
0
            useDate = inflationPeriod(maturityDate - useLag,
147
0
                                      frequency()).first;
148
0
        }
149
150
        // This assumes that the inflation term structure starts
151
        // as late as possible given the inflation index definition,
152
        // which is the usual case.
153
0
        return dayCounter().yearFraction(baseDate(), useDate);
154
0
    }
155
156
157
    Volatility
158
    YoYOptionletVolatilitySurface::totalVariance(const Date& maturityDate,
159
                                                 Rate strike,
160
                                                 const Period &obsLag,
161
0
                                                 bool extrapolate) const {
162
163
0
        Volatility vol = volatility(maturityDate, strike, obsLag, extrapolate);
164
0
        Time t = timeFromBase(maturityDate, obsLag);
165
0
        return vol*vol*t;
166
0
    }
167
168
169
    Volatility
170
    YoYOptionletVolatilitySurface::totalVariance(const Period& tenor,
171
                                                 Rate strike,
172
                                                 const Period &obsLag,
173
0
                                                 bool extrap) const {
174
0
        Date maturityDate = optionDateFromTenor(tenor);
175
0
        return totalVariance(maturityDate, strike, obsLag, extrap);
176
0
    }
177
178
179
180
    //========================================================================
181
    // constant yoy vol surface
182
    //========================================================================
183
184
    ConstantYoYOptionletVolatility::
185
    ConstantYoYOptionletVolatility(const Volatility v,
186
                                   Natural settlementDays,
187
                                   const Calendar& cal,
188
                                   BusinessDayConvention bdc,
189
                                   const DayCounter& dc,
190
                                   const Period &observationLag,
191
                                   Frequency frequency,
192
                                   bool indexIsInterpolated,
193
                                   Rate minStrike,
194
                                   Rate maxStrike,
195
                                   VolatilityType volType,
196
                                   Real displacement)
197
0
    : YoYOptionletVolatilitySurface(settlementDays, cal, bdc, dc,
198
0
                                    observationLag, frequency, indexIsInterpolated,
199
0
                                    volType, displacement),
200
0
      volatility_(ext::make_shared<SimpleQuote>(v)), minStrike_(minStrike), maxStrike_(maxStrike) {}
Unexecuted instantiation: QuantLib::ConstantYoYOptionletVolatility::ConstantYoYOptionletVolatility(double, unsigned int, QuantLib::Calendar const&, QuantLib::BusinessDayConvention, QuantLib::DayCounter const&, QuantLib::Period const&, QuantLib::Frequency, bool, double, double, QuantLib::VolatilityType, double)
Unexecuted instantiation: QuantLib::ConstantYoYOptionletVolatility::ConstantYoYOptionletVolatility(double, unsigned int, QuantLib::Calendar const&, QuantLib::BusinessDayConvention, QuantLib::DayCounter const&, QuantLib::Period const&, QuantLib::Frequency, bool, double, double, QuantLib::VolatilityType, double)
201
202
    ConstantYoYOptionletVolatility::ConstantYoYOptionletVolatility(Handle<Quote> v,
203
                                                                   Natural settlementDays,
204
                                                                   const Calendar& cal,
205
                                                                   BusinessDayConvention bdc,
206
                                                                   const DayCounter& dc,
207
                                                                   const Period& observationLag,
208
                                                                   Frequency frequency,
209
                                                                   bool indexIsInterpolated,
210
                                                                   Rate minStrike,
211
                                                                   Rate maxStrike,
212
                                                                   VolatilityType volType,
213
                                                                   Real displacement)
214
0
    : YoYOptionletVolatilitySurface(settlementDays,
215
0
                                    cal,
216
0
                                    bdc,
217
0
                                    dc,
218
0
                                    observationLag,
219
0
                                    frequency,
220
0
                                    indexIsInterpolated,
221
0
                                    volType,
222
0
                                    displacement),
223
0
      volatility_(std::move(v)), minStrike_(minStrike), maxStrike_(maxStrike) {}
Unexecuted instantiation: QuantLib::ConstantYoYOptionletVolatility::ConstantYoYOptionletVolatility(QuantLib::Handle<QuantLib::Quote>, unsigned int, QuantLib::Calendar const&, QuantLib::BusinessDayConvention, QuantLib::DayCounter const&, QuantLib::Period const&, QuantLib::Frequency, bool, double, double, QuantLib::VolatilityType, double)
Unexecuted instantiation: QuantLib::ConstantYoYOptionletVolatility::ConstantYoYOptionletVolatility(QuantLib::Handle<QuantLib::Quote>, unsigned int, QuantLib::Calendar const&, QuantLib::BusinessDayConvention, QuantLib::DayCounter const&, QuantLib::Period const&, QuantLib::Frequency, bool, double, double, QuantLib::VolatilityType, double)
224
225
0
    Volatility ConstantYoYOptionletVolatility::volatilityImpl(Time, Rate) const {
226
0
        return volatility_->value();
227
0
    }
228
229
} // namespace QuantLib