/src/quantlib/ql/experimental/volatility/sviinterpolatedsmilesection.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2014 Peter Caspers |
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 sviinterpolatedsmilesection.hpp |
21 | | \brief svi interpolating smile section |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_svi_interpolated_smile_section_hpp |
25 | | #define quantlib_svi_interpolated_smile_section_hpp |
26 | | |
27 | | #include <ql/handle.hpp> |
28 | | #include <ql/patterns/lazyobject.hpp> |
29 | | #include <ql/termstructures/volatility/smilesection.hpp> |
30 | | #include <ql/experimental/volatility/sviinterpolation.hpp> |
31 | | #include <ql/time/daycounters/actual365fixed.hpp> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | class Quote; |
36 | | class SviInterpolatedSmileSection : public SmileSection, public LazyObject { |
37 | | public: |
38 | | //! \name Constructors |
39 | | //@{ |
40 | | //! all market data are quotes |
41 | | SviInterpolatedSmileSection( |
42 | | const Date& optionDate, |
43 | | Handle<Quote> forward, |
44 | | const std::vector<Rate>& strikes, |
45 | | bool hasFloatingStrikes, |
46 | | Handle<Quote> atmVolatility, |
47 | | const std::vector<Handle<Quote> >& volHandles, |
48 | | Real a, |
49 | | Real b, |
50 | | Real sigma, |
51 | | Real rho, |
52 | | Real m, |
53 | | bool aIsFixed, |
54 | | bool bIsFixed, |
55 | | bool sigmaIsFixed, |
56 | | bool rhoIsFixed, |
57 | | bool mIsFixed, |
58 | | bool vegaWeighted = true, |
59 | | ext::shared_ptr<EndCriteria> endCriteria = ext::shared_ptr<EndCriteria>(), |
60 | | ext::shared_ptr<OptimizationMethod> method = ext::shared_ptr<OptimizationMethod>(), |
61 | | const DayCounter& dc = Actual365Fixed()); |
62 | | //! no quotes |
63 | | SviInterpolatedSmileSection( |
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>& vols, |
70 | | Real a, |
71 | | Real b, |
72 | | Real sigma, |
73 | | Real rho, |
74 | | Real m, |
75 | | bool isAFixed, |
76 | | bool isBFixed, |
77 | | bool isSigmaFixed, |
78 | | bool isRhoFixed, |
79 | | bool isMFixed, |
80 | | bool vegaWeighted = true, |
81 | | ext::shared_ptr<EndCriteria> endCriteria = ext::shared_ptr<EndCriteria>(), |
82 | | ext::shared_ptr<OptimizationMethod> method = ext::shared_ptr<OptimizationMethod>(), |
83 | | const DayCounter& dc = Actual365Fixed()); |
84 | | //@} |
85 | | //! \name LazyObject interface |
86 | | //@{ |
87 | | void performCalculations() const override; |
88 | | void update() override; |
89 | | //@} |
90 | | //! \name SmileSection interface |
91 | | //@{ |
92 | | Real minStrike() const override; |
93 | | Real maxStrike() const override; |
94 | | Real atmLevel() const override; |
95 | | //@} |
96 | | Real varianceImpl(Rate strike) const override; |
97 | | Volatility volatilityImpl(Rate strike) const override; |
98 | | //! \name Inspectors |
99 | | //@{ |
100 | | Real a() const; |
101 | | Real b() const; |
102 | | Real sigma() const; |
103 | | Real rho() const; |
104 | | Real m() const; |
105 | | Real rmsError() const; |
106 | | Real maxError() const; |
107 | | EndCriteria::Type endCriteria() const; |
108 | | //@} |
109 | | |
110 | | protected: |
111 | | //! Creates the mutable SviInterpolation |
112 | | void createInterpolation() const; |
113 | | mutable ext::shared_ptr<SviInterpolation> sviInterpolation_; |
114 | | |
115 | | //! Market data |
116 | | const Handle<Quote> forward_; |
117 | | const Handle<Quote> atmVolatility_; |
118 | | std::vector<Handle<Quote> > volHandles_; |
119 | | mutable std::vector<Rate> strikes_; |
120 | | //! Only strikes corresponding to valid market data |
121 | | mutable std::vector<Rate> actualStrikes_; |
122 | | bool hasFloatingStrikes_; |
123 | | |
124 | | mutable Real forwardValue_; |
125 | | mutable std::vector<Volatility> vols_; |
126 | | //! Svi parameters |
127 | | Real a_, b_, sigma_, rho_, m_; |
128 | | //! Svi interpolation settings |
129 | | bool isAFixed_, isBFixed_, isSigmaFixed_, isRhoFixed_, isMFixed_; |
130 | | bool vegaWeighted_; |
131 | | const ext::shared_ptr<EndCriteria> endCriteria_; |
132 | | const ext::shared_ptr<OptimizationMethod> method_; |
133 | | }; |
134 | | |
135 | 0 | inline void SviInterpolatedSmileSection::update() { |
136 | 0 | LazyObject::update(); |
137 | 0 | SmileSection::update(); |
138 | 0 | } |
139 | | |
140 | 0 | inline Real SviInterpolatedSmileSection::volatilityImpl(Rate strike) const { |
141 | 0 | calculate(); |
142 | 0 | return (*sviInterpolation_)(strike, true); |
143 | 0 | } |
144 | | |
145 | 0 | inline Real SviInterpolatedSmileSection::a() const { |
146 | 0 | calculate(); |
147 | 0 | return sviInterpolation_->a(); |
148 | 0 | } |
149 | | |
150 | 0 | inline Real SviInterpolatedSmileSection::b() const { |
151 | 0 | calculate(); |
152 | 0 | return sviInterpolation_->b(); |
153 | 0 | } |
154 | | |
155 | 0 | inline Real SviInterpolatedSmileSection::sigma() const { |
156 | 0 | calculate(); |
157 | 0 | return sviInterpolation_->sigma(); |
158 | 0 | } |
159 | | |
160 | 0 | inline Real SviInterpolatedSmileSection::rho() const { |
161 | 0 | calculate(); |
162 | 0 | return sviInterpolation_->rho(); |
163 | 0 | } |
164 | | |
165 | 0 | inline Real SviInterpolatedSmileSection::m() const { |
166 | 0 | calculate(); |
167 | 0 | return sviInterpolation_->m(); |
168 | 0 | } |
169 | | |
170 | 0 | inline Real SviInterpolatedSmileSection::rmsError() const { |
171 | 0 | calculate(); |
172 | 0 | return sviInterpolation_->rmsError(); |
173 | 0 | } |
174 | | |
175 | 0 | inline Real SviInterpolatedSmileSection::maxError() const { |
176 | 0 | calculate(); |
177 | 0 | return sviInterpolation_->maxError(); |
178 | 0 | } |
179 | | |
180 | 0 | inline EndCriteria::Type SviInterpolatedSmileSection::endCriteria() const { |
181 | 0 | calculate(); |
182 | 0 | return sviInterpolation_->endCriteria(); |
183 | 0 | } |
184 | | |
185 | 0 | inline Real SviInterpolatedSmileSection::minStrike() const { |
186 | 0 | calculate(); |
187 | 0 | return actualStrikes_.front(); |
188 | 0 | } |
189 | | |
190 | 0 | inline Real SviInterpolatedSmileSection::maxStrike() const { |
191 | 0 | calculate(); |
192 | 0 | return actualStrikes_.back(); |
193 | 0 | } |
194 | | |
195 | 0 | inline Real SviInterpolatedSmileSection::atmLevel() const { |
196 | 0 | calculate(); |
197 | 0 | return forwardValue_; |
198 | 0 | } |
199 | | } |
200 | | |
201 | | #endif |