/src/quantlib/ql/termstructures/volatility/sabrinterpolatedsmilesection.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2007 Cristina Duminuco |
5 | | Copyright (C) 2006 François du Vignaud |
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 | | <https://www.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/quotes/simplequote.hpp> |
23 | | #include <ql/settings.hpp> |
24 | | #include <ql/termstructures/volatility/sabrinterpolatedsmilesection.hpp> |
25 | | #include <utility> |
26 | | |
27 | | namespace QuantLib { |
28 | | |
29 | | SabrInterpolatedSmileSection::SabrInterpolatedSmileSection( |
30 | | const Date& optionDate, |
31 | | Handle<Quote> forward, |
32 | | const std::vector<Rate>& strikes, |
33 | | bool hasFloatingStrikes, |
34 | | Handle<Quote> atmVolatility, |
35 | | const std::vector<Handle<Quote> >& volHandles, |
36 | | Real alpha, |
37 | | Real beta, |
38 | | Real nu, |
39 | | Real rho, |
40 | | bool isAlphaFixed, |
41 | | bool isBetaFixed, |
42 | | bool isNuFixed, |
43 | | bool isRhoFixed, |
44 | | bool vegaWeighted, |
45 | | ext::shared_ptr<EndCriteria> endCriteria, |
46 | | ext::shared_ptr<OptimizationMethod> method, |
47 | | const DayCounter& dc, |
48 | | const Real shift) |
49 | 0 | : SmileSection(optionDate, dc, Date(), ShiftedLognormal, shift), forward_(std::move(forward)), |
50 | 0 | atmVolatility_(std::move(atmVolatility)), volHandles_(volHandles), strikes_(strikes), |
51 | 0 | actualStrikes_(strikes), hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()), |
52 | 0 | alpha_(alpha), beta_(beta), nu_(nu), rho_(rho), isAlphaFixed_(isAlphaFixed), |
53 | 0 | isBetaFixed_(isBetaFixed), isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed), |
54 | 0 | vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)), method_(std::move(method)), |
55 | 0 | evaluationDate_(Settings::instance().evaluationDate()) { |
56 | |
|
57 | 0 | LazyObject::registerWith(forward_); |
58 | 0 | LazyObject::registerWith(atmVolatility_); |
59 | 0 | for (auto& volHandle : volHandles_) |
60 | 0 | LazyObject::registerWith(volHandle); |
61 | 0 | } Unexecuted instantiation: QuantLib::SabrInterpolatedSmileSection::SabrInterpolatedSmileSection(QuantLib::Date const&, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<double, std::__1::allocator<double> > const&, bool, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<QuantLib::Handle<QuantLib::Quote>, std::__1::allocator<QuantLib::Handle<QuantLib::Quote> > > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&, double) Unexecuted instantiation: QuantLib::SabrInterpolatedSmileSection::SabrInterpolatedSmileSection(QuantLib::Date const&, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<double, std::__1::allocator<double> > const&, bool, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<QuantLib::Handle<QuantLib::Quote>, std::__1::allocator<QuantLib::Handle<QuantLib::Quote> > > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&, double) |
62 | | |
63 | | SabrInterpolatedSmileSection::SabrInterpolatedSmileSection( |
64 | | const Date& optionDate, |
65 | | const Rate& forward, |
66 | | const std::vector<Rate>& strikes, |
67 | | bool hasFloatingStrikes, |
68 | | const Volatility& atmVolatility, |
69 | | const std::vector<Volatility>& volHandles, |
70 | | Real alpha, |
71 | | Real beta, |
72 | | Real nu, |
73 | | Real rho, |
74 | | bool isAlphaFixed, |
75 | | bool isBetaFixed, |
76 | | bool isNuFixed, |
77 | | bool isRhoFixed, |
78 | | bool vegaWeighted, |
79 | | ext::shared_ptr<EndCriteria> endCriteria, |
80 | | ext::shared_ptr<OptimizationMethod> method, |
81 | | const DayCounter& dc, |
82 | | const Real shift) |
83 | 0 | : SmileSection(optionDate, dc, Date(), ShiftedLognormal, shift), |
84 | 0 | forward_(Handle<Quote>(ext::make_shared<SimpleQuote>(forward))), |
85 | 0 | atmVolatility_(Handle<Quote>(ext::make_shared<SimpleQuote>(atmVolatility))), |
86 | 0 | volHandles_(volHandles.size()), strikes_(strikes), actualStrikes_(strikes), |
87 | 0 | hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()), alpha_(alpha), beta_(beta), |
88 | 0 | nu_(nu), rho_(rho), isAlphaFixed_(isAlphaFixed), isBetaFixed_(isBetaFixed), |
89 | 0 | isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed), vegaWeighted_(vegaWeighted), |
90 | 0 | endCriteria_(std::move(endCriteria)), method_(std::move(method)), |
91 | 0 | evaluationDate_(Settings::instance().evaluationDate()) { |
92 | |
|
93 | 0 | for (Size i = 0; i < volHandles_.size(); ++i) |
94 | 0 | volHandles_[i] = Handle<Quote>(ext::make_shared<SimpleQuote>(volHandles[i])); |
95 | 0 | } Unexecuted instantiation: QuantLib::SabrInterpolatedSmileSection::SabrInterpolatedSmileSection(QuantLib::Date const&, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&, double) Unexecuted instantiation: QuantLib::SabrInterpolatedSmileSection::SabrInterpolatedSmileSection(QuantLib::Date const&, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&, double) |
96 | | |
97 | 0 | void SabrInterpolatedSmileSection::createInterpolation() const { |
98 | 0 | auto tmp = ext::make_shared<SABRInterpolation>( |
99 | 0 | actualStrikes_.begin(), actualStrikes_.end(), vols_.begin(), |
100 | 0 | exerciseTime(), forwardValue_, alpha_, beta_, nu_, rho_, |
101 | 0 | isAlphaFixed_, isBetaFixed_, isNuFixed_, isRhoFixed_, vegaWeighted_, |
102 | 0 | endCriteria_, method_, 0.0020, false, 50, shift()); |
103 | 0 | swap(tmp, sabrInterpolation_); |
104 | 0 | } |
105 | | |
106 | 0 | void SabrInterpolatedSmileSection::performCalculations() const { |
107 | 0 | forwardValue_ = forward_->value(); |
108 | 0 | vols_.clear(); |
109 | 0 | actualStrikes_.clear(); |
110 | | // we populate the volatilities, skipping the invalid ones |
111 | 0 | for (Size i=0; i<volHandles_.size(); ++i) { |
112 | 0 | if (volHandles_[i]->isValid()) { |
113 | 0 | if (hasFloatingStrikes_) { |
114 | 0 | actualStrikes_.push_back(forwardValue_ + strikes_[i]); |
115 | 0 | vols_.push_back(atmVolatility_->value() + volHandles_[i]->value()); |
116 | 0 | } else { |
117 | 0 | actualStrikes_.push_back(strikes_[i]); |
118 | 0 | vols_.push_back(volHandles_[i]->value()); |
119 | 0 | } |
120 | 0 | } |
121 | 0 | } |
122 | | // we are recreating the sabrinterpolation object unconditionnaly to |
123 | | // avoid iterator invalidation |
124 | 0 | createInterpolation(); |
125 | 0 | sabrInterpolation_->update(); |
126 | 0 | } |
127 | | |
128 | 0 | Real SabrInterpolatedSmileSection::varianceImpl(Real strike) const { |
129 | 0 | calculate(); |
130 | 0 | Real v = (*sabrInterpolation_)(strike, true); |
131 | 0 | return v*v*exerciseTime(); |
132 | 0 | } |
133 | | |
134 | | } |