Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/termstructures/volatility/optionlet/strippedoptionletadapter.cpp
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) 2007 Giorgio Facchinetti
5
 Copyright (C) 2007 Katiuscia Manzoni
6
 Copyright (C) 2015 Peter Caspers
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <http://quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/termstructures/volatility/optionlet/strippedoptionletadapter.hpp>
23
#include <ql/termstructures/volatility/optionlet/optionletstripper.hpp>
24
#include <ql/termstructures/volatility/capfloor/capfloortermvolsurface.hpp>
25
#include <ql/math/interpolations/linearinterpolation.hpp>
26
#include <ql/math/interpolations/sabrinterpolation.hpp>
27
#include <ql/termstructures/volatility/interpolatedsmilesection.hpp>
28
#include <ql/math/interpolations/cubicinterpolation.hpp>
29
30
namespace QuantLib {
31
32
    StrippedOptionletAdapter::StrippedOptionletAdapter(
33
                const ext::shared_ptr<StrippedOptionletBase>& s)
34
0
    : OptionletVolatilityStructure(s->settlementDays(),
35
0
                                   s->calendar(),
36
0
                                   s->businessDayConvention(),
37
0
                                   s->dayCounter()),
38
0
      optionletStripper_(s),
39
0
      nInterpolations_(s->optionletMaturities()),
40
0
      strikeInterpolations_(nInterpolations_) {
41
0
        registerWith(optionletStripper_);
42
0
    }
Unexecuted instantiation: QuantLib::StrippedOptionletAdapter::StrippedOptionletAdapter(boost::shared_ptr<QuantLib::StrippedOptionletBase> const&)
Unexecuted instantiation: QuantLib::StrippedOptionletAdapter::StrippedOptionletAdapter(boost::shared_ptr<QuantLib::StrippedOptionletBase> const&)
43
44
    ext::shared_ptr<SmileSection>
45
0
    StrippedOptionletAdapter::smileSectionImpl(Time t) const {
46
0
        std::vector< Rate > optionletStrikes =
47
0
            optionletStripper_->optionletStrikes(
48
0
                0); // strikes are the same for all times ?!
49
0
        std::vector< Real > stddevs;
50
0
        stddevs.reserve(optionletStrikes.size());
51
0
        for (Real optionletStrike : optionletStrikes) {
52
0
            stddevs.push_back(volatilityImpl(t, optionletStrike) * std::sqrt(t));
53
0
        }
54
        // Extrapolation may be a problem with splines, but since minStrike()
55
        // and maxStrike() are set, we assume that no one will use stddevs for
56
        // strikes outside these strikes
57
0
        CubicInterpolation::BoundaryCondition bc =
58
0
            optionletStrikes.size() >= 4 ? CubicInterpolation::Lagrange
59
0
                                         : CubicInterpolation::SecondDerivative;
60
0
        return ext::make_shared< InterpolatedSmileSection< Cubic > >(
61
0
            t, optionletStrikes, stddevs, Null< Real >(),
62
0
            Cubic(CubicInterpolation::Spline, false, bc, 0.0, bc, 0.0),
63
0
            Actual365Fixed(), volatilityType(), displacement());
64
0
    }
65
66
    Volatility StrippedOptionletAdapter::volatilityImpl(Time length,
67
0
                                                        Rate strike) const {
68
0
        calculate();
69
70
0
        std::vector<Volatility> vol(nInterpolations_);
71
0
        for (Size i=0; i<nInterpolations_; ++i)
72
0
            vol[i] = (*strikeInterpolations_[i])(strike, true);
73
74
0
        const std::vector<Time>& optionletTimes =
75
0
                                    optionletStripper_->optionletFixingTimes();
76
0
        ext::shared_ptr<LinearInterpolation> timeInterpolator(new
77
0
            LinearInterpolation(optionletTimes.begin(), optionletTimes.end(),
78
0
                                vol.begin()));
79
0
        return (*timeInterpolator)(length, true);
80
0
    }
81
82
0
    void StrippedOptionletAdapter::performCalculations() const {
83
84
        //const std::vector<Rate>& atmForward = optionletStripper_->atmOptionletRate();
85
        //const std::vector<Time>& optionletTimes = optionletStripper_->optionletTimes();
86
87
0
        for (Size i=0; i<nInterpolations_; ++i) {
88
0
            const std::vector<Rate>& optionletStrikes =
89
0
                optionletStripper_->optionletStrikes(i);
90
0
            const std::vector<Volatility>& optionletVolatilities =
91
0
                optionletStripper_->optionletVolatilities(i);
92
            //strikeInterpolations_[i] = ext::shared_ptr<SABRInterpolation>(new
93
            //            SABRInterpolation(optionletStrikes.begin(), optionletStrikes.end(),
94
            //                              optionletVolatilities.begin(),
95
            //                              optionletTimes[i], atmForward[i],
96
            //                              0.02,0.5,0.2,0.,
97
            //                              false, true, false, false
98
            //                              //alphaGuess_, betaGuess_,
99
            //                              //nuGuess_, rhoGuess_,
100
            //                              //isParameterFixed_[0],
101
            //                              //isParameterFixed_[1],
102
            //                              //isParameterFixed_[2],
103
            //                              //isParameterFixed_[3]
104
            //                              ////,
105
            //                              //vegaWeightedSmileFit_,
106
            //                              //endCriteria_,
107
            //                              //optMethod_
108
            //                              ));
109
0
            strikeInterpolations_[i] = ext::make_shared<LinearInterpolation>(optionletStrikes.begin(),
110
0
                                    optionletStrikes.end(),
111
0
                                    optionletVolatilities.begin());
112
113
            //QL_ENSURE(strikeInterpolations_[i]->endCriteria()!=EndCriteria::MaxIterations,
114
            //          "section calibration failed: "
115
            //          "option time " << optionletTimes[i] <<
116
            //          ": " <<
117
            //              ", alpha " <<  strikeInterpolations_[i]->alpha()<<
118
            //              ", beta "  <<  strikeInterpolations_[i]->beta() <<
119
            //              ", nu "    <<  strikeInterpolations_[i]->nu()   <<
120
            //              ", rho "   <<  strikeInterpolations_[i]->rho()  <<
121
            //              ", error " <<  strikeInterpolations_[i]->interpolationError()
122
            //              );
123
124
0
        }
125
0
    }
126
127
0
    Rate StrippedOptionletAdapter::minStrike() const {
128
0
        return optionletStripper_->optionletStrikes(0).front(); //FIX
129
0
    }
130
131
0
    Rate StrippedOptionletAdapter::maxStrike() const {
132
0
        return optionletStripper_->optionletStrikes(0).back(); //FIX
133
0
    }
134
135
0
    Date StrippedOptionletAdapter::maxDate() const {
136
0
        return optionletStripper_->optionletFixingDates().back();
137
0
    }
138
139
0
    VolatilityType StrippedOptionletAdapter::volatilityType() const {
140
0
        return optionletStripper_->volatilityType();
141
0
    }
142
143
0
    Real StrippedOptionletAdapter::displacement() const {
144
0
        return optionletStripper_->displacement();
145
0
    }
146
}