Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/termstructures/volatility/swaption/swaptionvolcube.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) 2006 Ferdinando Ametrano
5
 Copyright (C) 2015 Peter Caspers
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <http://quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
/*! \file swaptionvolcube.hpp
22
    \brief Swaption volatility cube
23
*/
24
25
#ifndef quantlib_swaption_volatility_cube_h
26
#define quantlib_swaption_volatility_cube_h
27
28
#include <ql/termstructures/volatility/swaption/swaptionvoldiscrete.hpp>
29
#include <ql/termstructures/volatility/smilesection.hpp>
30
31
namespace QuantLib {
32
33
    class SwapIndex;
34
    class Quote;
35
36
    //! swaption-volatility cube
37
    /*! \warning this class is not finalized and its interface might
38
                 change in subsequent releases.
39
    */
40
    class SwaptionVolatilityCube : public SwaptionVolatilityDiscrete {
41
      public:
42
        SwaptionVolatilityCube(const Handle<SwaptionVolatilityStructure>& atmVolStructure,
43
                               const std::vector<Period>& optionTenors,
44
                               const std::vector<Period>& swapTenors,
45
                               const std::vector<Spread>& strikeSpreads,
46
                               std::vector<std::vector<Handle<Quote> > > volSpreads,
47
                               ext::shared_ptr<SwapIndex> swapIndexBase,
48
                               ext::shared_ptr<SwapIndex> shortSwapIndexBase,
49
                               bool vegaWeightedSmileFit);
50
        //! \name TermStructure interface
51
        //@{
52
0
        DayCounter dayCounter() const override { return atmVol_->dayCounter(); }
53
0
        Date maxDate() const override { return atmVol_->maxDate(); }
54
0
        Time maxTime() const override { return atmVol_->maxTime(); }
55
0
        const Date& referenceDate() const override { return atmVol_->referenceDate(); }
56
0
        Calendar calendar() const override { return atmVol_->calendar(); }
57
0
        Natural settlementDays() const override { return atmVol_->settlementDays(); }
58
        //! \name VolatilityTermStructure interface
59
        //@{
60
0
        Rate minStrike() const override { return -QL_MAX_REAL; }
61
0
        Rate maxStrike() const override { return QL_MAX_REAL; }
62
        //@}
63
        //! \name SwaptionVolatilityStructure interface
64
        //@{
65
0
        const Period& maxSwapTenor() const override { return atmVol_->maxSwapTenor(); }
66
        //@}
67
        //! \name Other inspectors
68
        //@{
69
        Rate atmStrike(const Date& optionDate,
70
                       const Period& swapTenor) const;
71
        Rate atmStrike(const Period& optionTenor,
72
0
                       const Period& swapTenor) const {
73
0
            Date optionDate = optionDateFromTenor(optionTenor);
74
0
            return atmStrike(optionDate, swapTenor);
75
0
        }
76
0
    Handle<SwaptionVolatilityStructure> atmVol() const { return atmVol_; }
77
0
        const std::vector<Spread>& strikeSpreads() const { return strikeSpreads_; }
78
0
        const std::vector<std::vector<Handle<Quote> > >& volSpreads() const { return volSpreads_; }
79
0
        ext::shared_ptr<SwapIndex> swapIndexBase() const { return swapIndexBase_; }
80
0
        ext::shared_ptr<SwapIndex> shortSwapIndexBase() const { return shortSwapIndexBase_; }
81
0
        bool vegaWeightedSmileFit() const { return vegaWeightedSmileFit_; }
82
        //@}
83
        //! \name LazyObject interface
84
        //@{
85
0
        void performCalculations() const override {
86
0
            QL_REQUIRE(nStrikes_ >= requiredNumberOfStrikes(),
87
0
                       "too few strikes (" << nStrikes_
88
0
                                           << ") required are at least "
89
0
                                           << requiredNumberOfStrikes());
90
0
            SwaptionVolatilityDiscrete::performCalculations();
91
0
        }
92
        //@}
93
        VolatilityType volatilityType() const override;
94
95
      protected:
96
        void registerWithVolatilitySpread();
97
0
        virtual Size requiredNumberOfStrikes() const { return 2; }
98
        Volatility volatilityImpl(Time optionTime, Time swapLength, Rate strike) const override;
99
        Volatility
100
        volatilityImpl(const Date& optionDate, const Period& swapTenor, Rate strike) const override;
101
        Real shiftImpl(Time optionTime, Time swapLength) const override;
102
        Handle<SwaptionVolatilityStructure> atmVol_;
103
        Size nStrikes_;
104
        std::vector<Spread> strikeSpreads_;
105
        mutable std::vector<Rate> localStrikes_;
106
        mutable std::vector<Volatility> localSmile_;
107
        std::vector<std::vector<Handle<Quote> > > volSpreads_;
108
        ext::shared_ptr<SwapIndex> swapIndexBase_, shortSwapIndexBase_;
109
        bool vegaWeightedSmileFit_;
110
    };
111
112
    // inline
113
114
0
    inline VolatilityType SwaptionVolatilityCube::volatilityType() const {
115
0
        return atmVol_->volatilityType();
116
0
    }
117
118
    inline Volatility SwaptionVolatilityCube::volatilityImpl(
119
                                                        Time optionTime,
120
                                                        Time swapLength,
121
0
                                                        Rate strike) const {
122
0
        return smileSectionImpl(optionTime, swapLength)->volatility(strike);
123
0
    }
124
125
    inline Volatility SwaptionVolatilityCube::volatilityImpl(
126
                                                    const Date& optionDate,
127
                                                    const Period& swapTenor,
128
0
                                                    Rate strike) const {
129
0
        return smileSectionImpl(optionDate, swapTenor)->volatility(strike);
130
0
    }
131
132
    inline Real SwaptionVolatilityCube::shiftImpl(Time optionTime,
133
0
                                                  Time swapLength) const {
134
0
        return atmVol_->shift(optionTime, swapLength);
135
0
    }
136
}
137
138
#endif