Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
strippedoptionletadapter2.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21
22#include <ql/math/interpolations/cubicinterpolation.hpp>
23#include <ql/math/interpolations/linearinterpolation.hpp>
24#include <ql/math/interpolations/sabrinterpolation.hpp>
25#include <ql/termstructures/volatility/capfloor/capfloortermvolsurface.hpp>
26#include <ql/termstructures/volatility/interpolatedsmilesection.hpp>
27#include <ql/termstructures/volatility/optionlet/optionletstripper.hpp>
28
29using namespace QuantLib;
30using std::max;
31using std::min;
32using std::vector;
33
34namespace QuantExt {
35
36StrippedOptionletAdapter2::StrippedOptionletAdapter2(const QuantLib::ext::shared_ptr<StrippedOptionletBase>& s,
37 const bool flatExtrapolation)
38 : OptionletVolatilityStructure(s->settlementDays(), s->calendar(), s->businessDayConvention(), s->dayCounter()),
39 optionletStripper_(s), nInterpolations_(s->optionletMaturities()), strikeInterpolations_(nInterpolations_),
40 flatExtrapolation_(flatExtrapolation) {
41 registerWith(optionletStripper_);
42}
43
44QuantLib::ext::shared_ptr<SmileSection> StrippedOptionletAdapter2::smileSectionImpl(Time t) const {
45 std::vector<Rate> optionletStrikes =
46 optionletStripper_->optionletStrikes(0); // strikes are the same for all times ?!
47 std::vector<Real> stddevs;
48 Real tEff = flatExtrapolation_ ? std::min(t, optionletStripper_->optionletFixingTimes().back()) : t;
49 for (Size i = 0; i < optionletStrikes.size(); i++) {
50 stddevs.push_back(volatilityImpl(tEff, optionletStrikes[i]) * std::sqrt(tEff));
51 }
52 // Use a linear interpolated smile section.
53 // TODO: possibly make this configurable?
55 return QuantLib::ext::make_shared<InterpolatedSmileSection<LinearFlat> >(t, optionletStrikes, stddevs, Null<Real>(),
56 LinearFlat(), Actual365Fixed(),
58 else
59 return QuantLib::ext::make_shared<InterpolatedSmileSection<Linear> >(
60 t, optionletStrikes, stddevs, Null<Real>(), Linear(), Actual365Fixed(), volatilityType(), displacement());
61}
62
63Volatility StrippedOptionletAdapter2::volatilityImpl(Time length, Rate strike) const {
64
65 calculate();
66
67 vector<Volatility> vol(nInterpolations_);
68 for (Size i = 0; i < nInterpolations_; ++i)
69 vol[i] = strikeInterpolations_[i]->operator()(strike, true);
70
71 vector<Time> optionletTimes = optionletStripper_->optionletFixingTimes();
72 LinearInterpolation timeInterpolator(optionletTimes.begin(), optionletTimes.end(), vol.begin());
73
74 // If flat extrapolation is turned on, extrapolate flat after last expiry _and_ before first expiry
76 length = max(min(length, optionletTimes.back()), optionletTimes.front());
77 }
78
79 return timeInterpolator(length, true);
80}
81
83
84 // const std::vector<Rate>& atmForward = optionletStripper_->atmOptionletRate();
85 // const std::vector<Time>& optionletTimes = optionletStripper_->optionletTimes();
86
87 for (Size i = 0; i < nInterpolations_; ++i) {
88 const std::vector<Rate>& optionletStrikes = optionletStripper_->optionletStrikes(i);
89 const std::vector<Volatility>& optionletVolatilities = optionletStripper_->optionletVolatilities(i);
90 // strikeInterpolations_[i] = QuantLib::ext::shared_ptr<SABRInterpolation>(new
91 // SABRInterpolation(optionletStrikes.begin(), optionletStrikes.end(),
92 // optionletVolatilities.begin(),
93 // optionletTimes[i], atmForward[i],
94 // 0.02,0.5,0.2,0.,
95 // false, true, false, false
96 // //alphaGuess_, betaGuess_,
97 // //nuGuess_, rhoGuess_,
98 // //isParameterFixed_[0],
99 // //isParameterFixed_[1],
100 // //isParameterFixed_[2],
101 // //isParameterFixed_[3]
102 // ////,
103 // //vegaWeightedSmileFit_,
104 // //endCriteria_,
105 // //optMethod_
106 // ));
107 QuantLib::ext::shared_ptr<Interpolation> tmp = QuantLib::ext::shared_ptr<LinearInterpolation>(
108 new LinearInterpolation(optionletStrikes.begin(), optionletStrikes.end(), optionletVolatilities.begin()));
110 strikeInterpolations_[i] = QuantLib::ext::make_shared<FlatExtrapolation>(tmp);
111 else
112 strikeInterpolations_[i] = tmp;
113
114 // QL_ENSURE(strikeInterpolations_[i]->endCriteria()!=EndCriteria::MaxIterations,
115 // "section calibration failed: "
116 // "option time " << optionletTimes[i] <<
117 // ": " <<
118 // ", alpha " << strikeInterpolations_[i]->alpha()<<
119 // ", beta " << strikeInterpolations_[i]->beta() <<
120 // ", nu " << strikeInterpolations_[i]->nu() <<
121 // ", rho " << strikeInterpolations_[i]->rho() <<
122 // ", error " << strikeInterpolations_[i]->interpolationError()
123 // );
124 }
125}
126
128 return optionletStripper_->optionletStrikes(0).front(); // FIX
129}
130
132 return optionletStripper_->optionletStrikes(0).back(); // FIX
133}
134
135Date StrippedOptionletAdapter2::maxDate() const { return optionletStripper_->optionletFixingDates().back(); }
136
137VolatilityType StrippedOptionletAdapter2::volatilityType() const { return optionletStripper_->volatilityType(); }
138
139Real StrippedOptionletAdapter2::displacement() const { return optionletStripper_->displacement(); }
140} // namespace QuantExt
Linear-interpolation and flat extrapolation factory and traits
QuantLib::Volatility volatilityImpl(QuantLib::Time length, QuantLib::Rate strike) const override
QuantLib::Rate maxStrike() const override
QuantLib::Rate minStrike() const override
QuantLib::ext::shared_ptr< QuantLib::SmileSection > smileSectionImpl(QuantLib::Time optionTime) const override
QuantLib::VolatilityType volatilityType() const override
const QuantLib::ext::shared_ptr< QuantLib::StrippedOptionletBase > optionletStripper_
QuantLib::Real displacement() const override
std::vector< QuantLib::ext::shared_ptr< QuantLib::Interpolation > > strikeInterpolations_
StrippedOptionletAdapter2(const QuantLib::ext::shared_ptr< QuantLib::StrippedOptionletBase > &, const bool flatExtrapolation=false)
flat interpolation decorator
CompiledFormula min(CompiledFormula x, const CompiledFormula &y)
CompiledFormula max(CompiledFormula x, const CompiledFormula &y)
StrippedOptionlet Adapter (with a deeper update method, linear interpolation and optional flat extrap...