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  <http://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, const Handle<Quote> &forward,
43         const std::vector<Rate> &strikes, bool hasFloatingStrikes,
44         const Handle<Quote> &atmVolatility,
45         const std::vector<Handle<Quote> > &volHandles, Real a, Real b,
46         Real sigma, Real rho, Real m, bool aIsFixed, bool bIsFixed,
47         bool sigmaIsFixed, bool rhoIsFixed, bool mIsFixed,
48         bool vegaWeighted = true,
49         const ext::shared_ptr<EndCriteria> &endCriteria =
50             ext::shared_ptr<EndCriteria>(),
51         const ext::shared_ptr<OptimizationMethod> &method =
52             ext::shared_ptr<OptimizationMethod>(),
53         const DayCounter &dc = Actual365Fixed());
54     //! no quotes
55     SviInterpolatedSmileSection(
56         const Date &optionDate, const Rate &forward,
57         const std::vector<Rate> &strikes, bool hasFloatingStrikes,
58         const Volatility &atmVolatility, const std::vector<Volatility> &vols,
59         Real a, Real b, Real sigma, Real rho, Real m, bool isAFixed,
60         bool isBFixed, bool isSigmaFixed, bool isRhoFixed, bool isMFixed,
61         bool vegaWeighted = true,
62         const ext::shared_ptr<EndCriteria> &endCriteria =
63             ext::shared_ptr<EndCriteria>(),
64         const ext::shared_ptr<OptimizationMethod> &method =
65             ext::shared_ptr<OptimizationMethod>(),
66         const DayCounter &dc = Actual365Fixed());
67     //@}
68     //! \name LazyObject interface
69     //@{
70     virtual void performCalculations() const;
71     virtual void update();
72     //@}
73     //! \name SmileSection interface
74     //@{
75     Real minStrike() const;
76     Real maxStrike() const;
77     Real atmLevel() const;
78     //@}
79     Real varianceImpl(Rate strike) const;
80     Volatility volatilityImpl(Rate strike) const;
81     //! \name Inspectors
82     //@{
83     Real a() const;
84     Real b() const;
85     Real sigma() const;
86     Real rho() const;
87     Real m() const;
88     Real rmsError() const;
89     Real maxError() const;
90     EndCriteria::Type endCriteria() const;
91     //@}
92 
93   protected:
94     //! Creates the mutable SviInterpolation
95     void createInterpolation() const;
96     mutable ext::shared_ptr<SviInterpolation> sviInterpolation_;
97 
98     //! Market data
99     const Handle<Quote> forward_;
100     const Handle<Quote> atmVolatility_;
101     std::vector<Handle<Quote> > volHandles_;
102     mutable std::vector<Rate> strikes_;
103     //! Only strikes corresponding to valid market data
104     mutable std::vector<Rate> actualStrikes_;
105     bool hasFloatingStrikes_;
106 
107     mutable Real forwardValue_;
108     mutable std::vector<Volatility> vols_;
109     //! Svi parameters
110     Real a_, b_, sigma_, rho_, m_;
111     //! Svi interpolation settings
112     bool isAFixed_, isBFixed_, isSigmaFixed_, isRhoFixed_, isMFixed_;
113     bool vegaWeighted_;
114     const ext::shared_ptr<EndCriteria> endCriteria_;
115     const ext::shared_ptr<OptimizationMethod> method_;
116 };
117 
update()118 inline void SviInterpolatedSmileSection::update() {
119     LazyObject::update();
120     SmileSection::update();
121 }
122 
volatilityImpl(Rate strike) const123 inline Real SviInterpolatedSmileSection::volatilityImpl(Rate strike) const {
124     calculate();
125     return (*sviInterpolation_)(strike, true);
126 }
127 
a() const128 inline Real SviInterpolatedSmileSection::a() const {
129     calculate();
130     return sviInterpolation_->a();
131 }
132 
b() const133 inline Real SviInterpolatedSmileSection::b() const {
134     calculate();
135     return sviInterpolation_->b();
136 }
137 
sigma() const138 inline Real SviInterpolatedSmileSection::sigma() const {
139     calculate();
140     return sviInterpolation_->sigma();
141 }
142 
rho() const143 inline Real SviInterpolatedSmileSection::rho() const {
144     calculate();
145     return sviInterpolation_->rho();
146 }
147 
m() const148 inline Real SviInterpolatedSmileSection::m() const {
149     calculate();
150     return sviInterpolation_->m();
151 }
152 
rmsError() const153 inline Real SviInterpolatedSmileSection::rmsError() const {
154     calculate();
155     return sviInterpolation_->rmsError();
156 }
157 
maxError() const158 inline Real SviInterpolatedSmileSection::maxError() const {
159     calculate();
160     return sviInterpolation_->maxError();
161 }
162 
endCriteria() const163 inline EndCriteria::Type SviInterpolatedSmileSection::endCriteria() const {
164     calculate();
165     return sviInterpolation_->endCriteria();
166 }
167 
minStrike() const168 inline Real SviInterpolatedSmileSection::minStrike() const {
169     calculate();
170     return actualStrikes_.front();
171 }
172 
maxStrike() const173 inline Real SviInterpolatedSmileSection::maxStrike() const {
174     calculate();
175     return actualStrikes_.back();
176 }
177 
atmLevel() const178 inline Real SviInterpolatedSmileSection::atmLevel() const {
179     calculate();
180     return forwardValue_;
181 }
182 }
183 
184 #endif
185