interpolation.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2006, 2007, 2009, 2010 Ferdinando Ametrano
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 #ifndef qla_interpolation_hpp
21 #define qla_interpolation_hpp
22 
23 #include <qlo/extrapolator.hpp>
24 
25 #include <ql/math/interpolations/mixedinterpolation.hpp>
26 #include <ql/math/interpolations/cubicinterpolation.hpp>
27 #include <ql/math/interpolations/sabrinterpolation.hpp>
28 #include <ql/math/interpolations/abcdinterpolation.hpp>
29 
30 #include <ql/patterns/lazyobject.hpp>
31 #include <ql/quote.hpp>
32 #include <ql/types.hpp>
33 
34 namespace QuantLib {
35  class Extrapolator;
36  class EndCriteria;
37  class OptimizationMethod;
38 
39  template<class T>
40  class Handle;
41 
42  class Quote;
43 }
44 
45 namespace QuantLibAddin {
46 
47  class Interpolation : public Extrapolator, public QuantLib::LazyObject {
48  public:
49  QuantLib::Real operator()(QuantLib::Real x,
50  bool allowExtrapolation) const {
51  calculate();
52  return qlInterpolation_->operator()(x, allowExtrapolation);
53  }
54  QuantLib::Real primitive(QuantLib::Real x,
55  bool allowExtrapolation) const {
56  calculate();
57  return qlInterpolation_->primitive(x, allowExtrapolation);
58  }
59  QuantLib::Real derivative(QuantLib::Real x,
60  bool allowExtrapolation) const {
61  calculate();
62  return qlInterpolation_->derivative(x, allowExtrapolation);
63  }
64  QuantLib::Real secondDerivative(QuantLib::Real x,
65  bool allowExtrapolation) const {
66  calculate();
67  return qlInterpolation_->secondDerivative(x, allowExtrapolation);
68  }
69  void performCalculations() const;
70  protected:
71  Interpolation(const boost::shared_ptr<ObjectHandler::ValueObject>&,
72  const std::vector<QuantLib::Real>& x,
73  const std::vector<QuantLib::Handle<QuantLib::Quote> >& yh,
74  bool permanent);
75  QuantLib::Size n_;
76  std::vector<QuantLib::Real> x_;
77  std::vector<QuantLib::Handle<QuantLib::Quote> > yh_;
78  mutable std::vector<QuantLib::Real> y_;
79  boost::shared_ptr<QuantLib::Interpolation> qlInterpolation_;
80  };
81 
82  class GenericInterp : public Interpolation {
83  public:
84  GenericInterp(const boost::shared_ptr<ObjectHandler::ValueObject>&,
85  const std::string& type,
86  const std::vector<QuantLib::Real>& x,
87  const std::vector<QuantLib::Handle<QuantLib::Quote> >& y,
88  bool permanent);
89  };
90 
92  public:
94  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
95  const std::vector<QuantLib::Real>& x,
96  const std::vector<QuantLib::Handle<QuantLib::Quote> >& y,
97  QuantLib::Size n,
98  QuantLib::MixedInterpolation::Behavior behavior,
99  QuantLib::CubicInterpolation::DerivativeApprox da,
100  bool monotonic,
101  QuantLib::CubicInterpolation::BoundaryCondition leftCondition,
102  QuantLib::Real leftConditionValue,
103  QuantLib::CubicInterpolation::BoundaryCondition rightCondition,
104  QuantLib::Real rightConditionValue,
105  bool permanent);
106  //QuantLib::Size switchIndex() const {
107  // calculate();
108  // return qlMixedLinearCubicInterpolation_->switchIndex();
109  //}
110  protected:
111  boost::shared_ptr<QuantLib::MixedLinearCubicInterpolation> qlMixedLinearCubicInterpolation_;
112  };
113 
115  public:
117  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
118  const std::vector<QuantLib::Real>& x,
119  const std::vector<QuantLib::Handle<QuantLib::Quote> >& y,
120  QuantLib::CubicInterpolation::DerivativeApprox da,
121  bool monotonic,
122  QuantLib::CubicInterpolation::BoundaryCondition leftCondition,
123  QuantLib::Real leftConditionValue,
124  QuantLib::CubicInterpolation::BoundaryCondition rightCondition,
125  QuantLib::Real rightConditionValue,
126  bool permanent);
127  const std::vector<QuantLib::Real>& primitiveConstants() const {
128  calculate();
129  return qlCubicInterpolation_->primitiveConstants();
130  }
131  const std::vector<QuantLib::Real>& aCoefficients() const {
132  calculate();
133  return qlCubicInterpolation_->aCoefficients();
134  }
135  const std::vector<QuantLib::Real>& bCoefficients() const {
136  calculate();
137  return qlCubicInterpolation_->bCoefficients();
138  }
139  const std::vector<QuantLib::Real>& cCoefficients() const {
140  calculate();
141  return qlCubicInterpolation_->cCoefficients();
142  }
143  const std::vector<bool>& monotonicityAdjustments() const {
144  calculate();
145  return qlCubicInterpolation_->monotonicityAdjustments();
146  }
147  protected:
148  boost::shared_ptr<QuantLib::CubicInterpolation> qlCubicInterpolation_;
149  };
150 
152  public:
154  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
155  const std::vector<QuantLib::Real>& x,
156  const std::vector<QuantLib::Handle<QuantLib::Quote> >& y,
157  QuantLib::Real a,
158  QuantLib::Real b,
159  QuantLib::Real c,
160  QuantLib::Real d,
161  bool aIsFixed,
162  bool bIsFixed,
163  bool cIsFixed,
164  bool dIsFixed,
165  bool vegaWeighted,
166  const boost::shared_ptr<QuantLib::EndCriteria>& ec,
167  const boost::shared_ptr<QuantLib::OptimizationMethod>& om,
168  bool permanent);
169  QuantLib::Real a() const {
170  calculate();
171  return qlAbcdInterpolation_->a();
172  }
173  QuantLib::Real b() const {
174  calculate();
175  return qlAbcdInterpolation_->b();
176  }
177  QuantLib::Real c() const {
178  calculate();
179  return qlAbcdInterpolation_->c();
180  }
181  QuantLib::Real d() const {
182  calculate();
183  return qlAbcdInterpolation_->d();
184  }
185  QuantLib::Real rmsError() const {
186  calculate();
187  return qlAbcdInterpolation_->rmsError();
188  }
189  QuantLib::Real maxError() const {
190  calculate();
191  return qlAbcdInterpolation_->maxError();
192  }
193  QuantLib::EndCriteria::Type endCriteria() const {
194  calculate();
195  return qlAbcdInterpolation_->endCriteria();
196  }
197  protected:
198  boost::shared_ptr<QuantLib::AbcdInterpolation> qlAbcdInterpolation_;
199  };
200 
202  public:
204  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
205  const std::vector<QuantLib::Real>& x,
206  const std::vector<QuantLib::Handle<QuantLib::Quote> >& y,
207  QuantLib::Time t,
209  QuantLib::Real alpha,
210  QuantLib::Real beta,
211  QuantLib::Real nu,
212  QuantLib::Real rho,
213  bool alphaIsFixed,
214  bool betaIsFixed,
215  bool nuIsFixed,
216  bool rhoIsFixed,
217  bool vegaWeighted,
218  const boost::shared_ptr<QuantLib::EndCriteria>& ec,
219  const boost::shared_ptr<QuantLib::OptimizationMethod>& om,
220  bool permanent);
221  QuantLib::Real alpha() const {
222  calculate();
223  return qlSABRInterpolation_->alpha();
224  }
225  QuantLib::Real beta() const {
226  calculate();
227  return qlSABRInterpolation_->beta();
228  }
229  QuantLib::Real nu() const {
230  calculate();
231  return qlSABRInterpolation_->nu();
232  }
233  QuantLib::Real rho() const {
234  calculate();
235  return qlSABRInterpolation_->rho();
236  }
237  const std::vector<QuantLib::Real>& interpolationWeights() const {
238  calculate();
239  return qlSABRInterpolation_->interpolationWeights();
240  }
241  QuantLib::Real rmsError() const {
242  calculate();
243  return qlSABRInterpolation_->rmsError();
244  }
245  QuantLib::Real maxError() const {
246  calculate();
247  return qlSABRInterpolation_->maxError();
248  }
249  QuantLib::EndCriteria::Type endCriteria() const {
250  calculate();
251  return qlSABRInterpolation_->endCriteria();
252  }
253  void performCalculations() const {
254  forward_ = forwardh_->value();
256  }
257  protected:
259  mutable QuantLib::Real forward_;
260  boost::shared_ptr<QuantLib::SABRInterpolation> qlSABRInterpolation_;
261  };
262 
263 }
264 
265 #endif
const std::vector< QuantLib::Real > & aCoefficients() const
Definition: interpolation.hpp:131
QuantLib::Real maxError() const
Definition: interpolation.hpp:189
std::vector< QuantLib::Real > x_
Definition: interpolation.hpp:76
AbcdInterpolation(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, const std::vector< QuantLib::Real > &x, const std::vector< QuantLib::Handle< QuantLib::Quote > > &y, QuantLib::Real a, QuantLib::Real b, QuantLib::Real c, QuantLib::Real d, bool aIsFixed, bool bIsFixed, bool cIsFixed, bool dIsFixed, bool vegaWeighted, const boost::shared_ptr< QuantLib::EndCriteria > &ec, const boost::shared_ptr< QuantLib::OptimizationMethod > &om, bool permanent)
QuantLib::EndCriteria::Type endCriteria() const
Definition: interpolation.hpp:249
QuantLib::Real rho() const
Definition: interpolation.hpp:233
QuantLib::Real beta() const
Definition: interpolation.hpp:225
QuantLib::Real forward_
Definition: interpolation.hpp:259
QuantLib::Real primitive(QuantLib::Real x, bool allowExtrapolation) const
Definition: interpolation.hpp:54
boost::shared_ptr< QuantLib::Interpolation > qlInterpolation_
Definition: interpolation.hpp:79
SABRInterpolation(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, const std::vector< QuantLib::Real > &x, const std::vector< QuantLib::Handle< QuantLib::Quote > > &y, QuantLib::Time t, QuantLib::Handle< QuantLib::Quote > forward, QuantLib::Real alpha, QuantLib::Real beta, QuantLib::Real nu, QuantLib::Real rho, bool alphaIsFixed, bool betaIsFixed, bool nuIsFixed, bool rhoIsFixed, bool vegaWeighted, const boost::shared_ptr< QuantLib::EndCriteria > &ec, const boost::shared_ptr< QuantLib::OptimizationMethod > &om, bool permanent)
const std::vector< QuantLib::Real > & primitiveConstants() const
Definition: interpolation.hpp:127
std::vector< QuantLib::Real > y_
Definition: interpolation.hpp:78
void performCalculations() const
Definition: interpolation.hpp:253
QuantLib::Real maxError() const
Definition: interpolation.hpp:245
QuantLib::Real rmsError() const
Definition: interpolation.hpp:241
const std::vector< QuantLib::Real > & bCoefficients() const
Definition: interpolation.hpp:135
Definition: interpolation.hpp:47
QuantLib::Real operator()(QuantLib::Real x, bool allowExtrapolation) const
Definition: interpolation.hpp:49
boost::shared_ptr< QuantLib::SABRInterpolation > qlSABRInterpolation_
Definition: interpolation.hpp:260
boost::shared_ptr< QuantLib::AbcdInterpolation > qlAbcdInterpolation_
Definition: interpolation.hpp:198
QuantLib::Real nu() const
Definition: interpolation.hpp:229
QuantLib::Real derivative(QuantLib::Real x, bool allowExtrapolation) const
Definition: interpolation.hpp:59
QuantLib::Real c() const
Definition: interpolation.hpp:177
Definition: extrapolator.hpp:31
QuantLib::Real d() const
Definition: interpolation.hpp:181
QuantLib::Real alpha() const
Definition: interpolation.hpp:221
const std::vector< QuantLib::Real > & cCoefficients() const
Definition: interpolation.hpp:139
std::vector< QuantLib::Handle< QuantLib::Quote > > yh_
Definition: interpolation.hpp:77
Definition: interpolation.hpp:82
Definition: abcd.hpp:38
GenericInterp(const boost::shared_ptr< ObjectHandler::ValueObject > &, const std::string &type, const std::vector< QuantLib::Real > &x, const std::vector< QuantLib::Handle< QuantLib::Quote > > &y, bool permanent)
Definition: interpolation.hpp:91
QuantLib::EndCriteria::Type endCriteria() const
Definition: interpolation.hpp:193
const std::vector< bool > & monotonicityAdjustments() const
Definition: interpolation.hpp:143
QuantLib::Handle< QuantLib::Quote > forwardh_
Definition: interpolation.hpp:258
const std::vector< QuantLib::Real > & interpolationWeights() const
Definition: interpolation.hpp:237
Definition: interpolation.hpp:201
Definition: abcd.hpp:30
QuantLib::Real secondDerivative(QuantLib::Real x, bool allowExtrapolation) const
Definition: interpolation.hpp:64
QuantLib::Real a() const
Definition: interpolation.hpp:169
QuantLib::Size n_
Definition: interpolation.hpp:75
Definition: interpolation.hpp:151
CubicInterpolation(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, const std::vector< QuantLib::Real > &x, const std::vector< QuantLib::Handle< QuantLib::Quote > > &y, QuantLib::CubicInterpolation::DerivativeApprox da, bool monotonic, QuantLib::CubicInterpolation::BoundaryCondition leftCondition, QuantLib::Real leftConditionValue, QuantLib::CubicInterpolation::BoundaryCondition rightCondition, QuantLib::Real rightConditionValue, bool permanent)
MixedLinearCubicInterpolation(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, const std::vector< QuantLib::Real > &x, const std::vector< QuantLib::Handle< QuantLib::Quote > > &y, QuantLib::Size n, QuantLib::MixedInterpolation::Behavior behavior, QuantLib::CubicInterpolation::DerivativeApprox da, bool monotonic, QuantLib::CubicInterpolation::BoundaryCondition leftCondition, QuantLib::Real leftConditionValue, QuantLib::CubicInterpolation::BoundaryCondition rightCondition, QuantLib::Real rightConditionValue, bool permanent)
boost::shared_ptr< QuantLib::MixedLinearCubicInterpolation > qlMixedLinearCubicInterpolation_
Definition: interpolation.hpp:111
Interpolation(const boost::shared_ptr< ObjectHandler::ValueObject > &, const std::vector< QuantLib::Real > &x, const std::vector< QuantLib::Handle< QuantLib::Quote > > &yh, bool permanent)
QuantLib::Real rmsError() const
Definition: interpolation.hpp:185
boost::shared_ptr< QuantLib::CubicInterpolation > qlCubicInterpolation_
Definition: interpolation.hpp:148
Definition: interpolation.hpp:114
QuantLib::Real b() const
Definition: interpolation.hpp:173